zlacker

[parent] [thread] 7 comments
1. henrik+(OP)[view] [source] 2020-11-30 11:34:52
> It is completely impossible to implement something like "under" in matlab.

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?

replies(2): >>kliber+66 >>patrec+cd
2. kliber+66[view] [source] 2020-11-30 12:38:36
>>henrik+(OP)
> Does J have a notion of the relationship between certain functions and their inverse?

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

replies(1): >>henrik+ad
◧◩
3. henrik+ad[view] [source] [discussion] 2020-11-30 13:44:03
>>kliber+66
That can definitely be implemented in Matlab with the symbolic math toolbox.

But interesting nonetheless.

replies(1): >>patrec+le
4. patrec+cd[view] [source] 2020-11-30 13:44:13
>>henrik+(OP)
Here is toy example: I define a function f(x):=1+x2. J will automatically work out the inverse for me, as you can see below:

       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.
◧◩◪
5. patrec+le[view] [source] [discussion] 2020-11-30 13:53:25
>>henrik+ad
I have used matlab quite a bit in the past, but not the symbolic math toolbox: can you make a "normal" function definition symbolic retroactively? I thought you need to define the function explicitly as symbolic to start with, or am I wrong?
replies(1): >>henrik+Vx
◧◩◪◨
6. henrik+Vx[view] [source] [discussion] 2020-11-30 15:40:54
>>patrec+le
Yes, the symbolic functions can be made "retroactively". The various conventional functions are overloaded with the symbolic input.

    >> 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
replies(1): >>patrec+LB
◧◩◪◨⬒
7. patrec+LB[view] [source] [discussion] 2020-11-30 16:00:26
>>henrik+Vx
So if you have odddouble.m with

    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.
replies(1): >>henrik+TI2
◧◩◪◨⬒⬓
8. henrik+TI2[view] [source] [discussion] 2020-12-01 07:38:33
>>patrec+LB
Just tested it and it worked fine. Note that your function takes too many parameters in its definition and that functions can't be passed as parameters by name.

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.

[go to top]