jsoftware / jsource

J engine source mirror
Other
650 stars 90 forks source link

Support inverse for I. y #51

Open tangentstorm opened 2 years ago

tangentstorm commented 2 years ago

Marshall suggests that:

The verb ((#/.~@])`(~.@])`[}) with an appropriate left argument is an left inverse to I. ignoring trailing zeros, and a right inverse to I. ignoring ordering. It counts, for each number in (i.n) where n is the length of the left argument, how many elements on the right are equal to it.

   x =. 4 7 2 4 1 7 8
   (10$0) (#/.~@])`(~.@])`[}"1 x
0 1 1 0 2 0 0 2 1 0
   I. (10$0) (#/.~@])`(~.@])`[}"1 x
1 2 4 4 7 7 8
   (/:~x) -: I. (10$0) (#/.~@])`(~.@])`[}"1 x
1

In that form, the inverse would be a dyad, which would prevent its use in duals. Perhaps a better inverse is ((#/.~)`~.`(0#~>:@(>./))}) .

-- @HenryHRich 22:38, 27 June 2017 (UTC)

moon-chilled commented 1 year ago

the 'better inverse' doesn't work; u`v`w} y is (v y)} w y, not (u y) (v y)} w y. An alternative: <:@#/.\~@(,\~ i.@>:@(>./))

moon-chilled commented 1 year ago

(or parenthesise #/.~ for juicier numbers—w/e)

0racle commented 1 year ago

An alternative: <:@#/.\~@(,~ i.@>:@(>./))

This is nice, and appears to mirror what BQN (/⁼) does.

Dyalog (⍸⍣¯1) will DOMAIN ERROR on non-monotonic lists, which is less helpful (but likely allows them to optimise for the sorted case)