You may already know that you can define your own inverses. Verbs have them.
Not modifiers. But you can make a verb from a common modifier use. example:
cut1 =: <;.1 :. ;
(,~ -@:(+/))each&.cut1 0 1 1 1 0 1 0 1 1
_3 0 1 1 1 _1 0 1 _2 0 1 1
for cut_1, we can't really know an inverse for the verb because we don't know
what to fill back in, but there's 2 choices
make a similar verb to cut in profile (without removing a:'s)
or we can make an undercut_1 adverb
cut_1 =: ' '&$: :(<;._1@,) :. (}.@:;@:(, each))
+: each&.(0&cut_1) 0 1 2 3 0 1 2 0 3 4 5
0 2 4 6 0 2 4 0 6 8 10 +: each&.(1&cut_1) 0 1 2 3 0 1 2 0 3 4 5 0
0 1 4 6 0 1 4 0 6 8 10 0
undercut_1 =: 1 : '{. ;@:(, each) [: u each <;._1'
+:undercut_1 0 1 2 3 0 1 2 0 3 4 5
0 2 4 6 0 2 4 0 6 8 10
+:undercut_1 1 2 3 0 1 2 0 3 4 5
1 4 6 0 1 4 0 6 8 10
a useful inverse to cut is probably joinstring
0 joinstring 0 cut 0 1 2 3 0 1 2 0 3 4 5
1 2 3 0 1 2 0 3 4 5
----- Original Message -----
From: Louis de Forcrand <[email protected]>
To: [email protected]
Sent: Wednesday, March 2, 2016 7:39 PM
Subject: [Jprogramming] Inverses
The way J handles inverses to functions is quite unique and, while not
always very useful, can lead to compact but very descriptive code:
+/&.:*: NB. distance
+&.^. NB. multiplication
u&.(a.&i.) NB. u applied to y’s indices in a., then indexed out of a.
etc.
However, some less easily invertible functions, such as , (ravel), {: and
related,
A., m&{ , and others, don’t have a monadic inverse because they are “lossy”
functions,
in that there is no function u such that
y -: u@v y NB. where v is a lossy function
for an arbitrary y.
However, while there are other lossy functions which I have not cited, the ones
I
did are particular in that they are invertible with trains of related dyadic
functions:
u&., y <—> ($ $ u@,) y
u&.(m&{.) y <—> (m&}. , u@(m&{.)) y NB. extends to others like }. etc.
NB. although {. already has an inverse
u&.A. y <—> (A.~ u@A.) y
These are all simply “cosmetic”, in that you can obviously write them out
by hand, but then again, &. is basically cosmetic too. Nevertheless, it leads
to more elegant and concise code (and sometimes leads to new ways of
thinking about a problem). However, one inverse that could possibly
lead to (possibly big?) gains in speed with special code would be:
u&.(m&{) y <—> (m&}~ u@(m&{)) y
This might already be implemented for the fork case.
I’ve checked, and m&{ already has an inverse, although I don’t see what it’s
useful for.
On a side note, I’ve noticed that <;.1 has an inverse according to b._1, but
using it yields a nonce error (which means it hasn’t been implemented yet,
right?). <;._1@(m&,) could be recognised as a whole as well, so that
u&.(<;._1@(m&,)) y <—> ({. ;@, m ,&.> }.)@u@(<;._1@(m&,))
Or it could be added as an obverse to the cut standard library verb.
Again, this can all be done by hand, of course.
Louis
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm