But that overhead is constant factor, more or less anything you can express well in matlab can be expressed straightforwardly in APL, too, if you have the right numerical routines. That's not true in the other direction though: there's a lot of stuff in APL you cannot express adequately in matlab at all. For example J (and these days Dyalog as well IIRC) have an operation called under which basically does this: u(f,g) = x => f^-1(g(f(x)). So you can write geometric_mean = u(mean, log).
It is completely impossible to implement something like "under" in matlab. Admittedly the J implementation at least of deriving a generalized inverse for an arbitrary function f is a somewhat ill-defined hack, but this is still something that is both conceptually and practically quite powerful. Also, whilst Matlab is really clunky for anything that is not a 2D array and hardcodes matrix multiplication as the one inner-product, APL has more powerful abstractions for manipulating arbitrary rank arrays and a more general concept of inner products.
Also, APL has some really dumb but cherished-by-the-community ideas that make the language less expressive and much more awkward to learn, e.g. the idea of replicating the terrible defect of normal mathematical notation where - is overloaded for negation and subtraction to every other function.
Have you seen the version used by dzaima/apl[1]? The equivalent of '(-&.:{:) i.5' works and results in 0 1 2 3 _4.
> APL has some really dumb but cherished-by-the-community ideas that make the language less expressive and much more awkward to learn, e.g. the idea of replicating the terrible defect of normal mathematical notation where - is overloaded for negation and subtraction to every other function
Klong[2] is a partial attempt to resolve this. I won't repeat the arguments in favour of ambivalent functions, as I guess you've heard them a dozen times before
> u(f,g) = x => f^-1(g(f(x)).
Other way round; it's g^-1(f(g(x)))
I’m a little curious about this. Does J have a notion of the relationship between certain functions and their inverse? What is it that enables “under” in J which makes it impossible in Matlab?
Yes. Many built-in words have inverses assigned, and you can assign inverse functions to your own words with :. https://code.jsoftware.com/wiki/Vocabulary/codot
EDIT: and here's a table with predefined inverses: https://code.jsoftware.com/wiki/Vocabulary/Inverses
But interesting nonetheless.
f=:(1+*&2)
f 1 2 3 4
3 5 7 9
(f^:_1)f 1 2 3 4
1 2 3 4
(f^:_1) 1 2 3 4
0 0.5 1 1.5
Now obviously not every function is bijective or even if bijective, trivial to invert -- and J doesn't (or at least didn't) have a super well specified way of computing those generalized inverses. But still: "under" is actually pretty cool, even just conceptually I find it quite valuable. >> syms x
>> f = @(x) log(sqrt(x)).^2
f = function_handle with value:
@(x)log(sqrt(x)).^2
>> f(x)
ans = log(x^(1/2))^2
>> finverse(f(x))
ans = exp(2*x^(1/2))
And to implement under: function u = under(f, g)
syms x
g_inv = matlabFunction(finverse(g(x)));
u = @(x) g_inv(f(g(x)));
end function y=odddouble(a,b)
y=2*x+1
endfunction
you can do >>> h = under(@(x) 1/x, odddouble)
>>> h(3)
?
If so, yeah, I agree you can implement under in matlab (as long as you have the symbolic toolbox as well); in which case it's probably one of very few non-CAS systems where you can define it.I hadn't seen Dzaima's APL, thanks! I like that he made a processing binding; APL always seemed like it would be such an obvious choice for doing dweet style graphics code golfing that I wondered why no one seemed to be doing it. A web-based APL would be a better choice though.
In that case you'll be wanting ngn/apl[1], which runs in a browser and compiles to js.
> ambivalent
The arguments are mostly linguistic. Natural language is also context-sensitive, so we are well-equipped to parse such formations; and they allow us to reuse information. The monadic and dyadic forms of '-' are related, so it's less cognitive overhead to recognize its meaning.
It is definitely not as elegant as the built-in facility in J, but definitely doable and usable in Matlab. In fact, I think any language with flexible enough function overloading should be able to implement such a feature.
dzaima/APL being written in Java means getting it to run in a browser would be a bit hard, and ngn has given up on ngn/apl, but BQN[0] could definitely get a web canvas based graphics interface.
Somewhat interesting to add to the conversation about Under is that, in my impl, calling a function, calling its inverse, or doing something under it (i.e. structural under) are all equally valid ways to "use" a function, it's just a "coincidence" that there's direct syntax for invoking only one. (Dyalog does not yet have under, but it definitely is planned.)