I want to compress ⌿ several vectors/matrices at the same time.
Here's my data:
txt←(3 3⍴'a')(3 3⍴'b')(3 3⍴'c')(3 3⍴'d')
b←(0 1 0)(1 1 1)(1 1 0)(0 0 0)
Now, I want to compress each matrix in txt by corresponding Boolean vector in b:
b⌿¨txt
Here's what I expect to get back:
┌───┬───┬───┬───┐
│aaa│bbb│ccc│ │
│ │bbb│ccc│ │
│ │bbb│ │ │
└───┴───┴───┴───┘
I've tried this over at tryapl.org and it behaves as I expect, but in APL2 I get a DOMAIN ERROR pointing at b and the Each glyph.
Are there any APL2 hackers here that could help me with this? Is there another way of doing this without an explicit loop? Inner/Outer product?
The reason this fails in APL2 is that ⌿ is purely an operator in APL2, whereas in Dyalog APL it is a hybrid function/operator in that it acts like a function if it has an array on its left, but as an operator if it has a function on its left.
In Dyalog APL, since ⌿ acts as a normal function, the items of b and txt will be paired up by ¨ as you expect.
In APL2, b will be bound to to the monadic operator ⌿ as its sole operand and then the entire derived function b⌿ will be applied with ¨ to each item of txt. Needless to say, the entire b (a vector of vectors) is not an appropriate left argument for a single application of ⌿.
You can get around this problem by wrapping ⌿ in a function, and then using ¨ on that wrapper function. The wrapper is as simple as:
r←x R y
r←x⌿y
In GNU APL (an APL2 derivative) you can put parentheses around ⌿ or around ⌿¨ :
txt←(3 3⍴'a')(3 3⍴'b')(3 3⍴'c')(3 3⍴'d')
b←(0 1 0)(1 1 1)(1 1 0)(0 0 0)
b(⌿)¨txt
aaa bbb ccc
bbb ccc
bbb
b(⌿¨)txt
aaa bbb ccc
bbb ccc
bbb
In original IBM APL2 (PC version) none of these seem to work. The reasons might be that ⌿ is ambiguous (it could mean "function compress" as well as "operator reduce") and the ISO standard (which is implemented by both IBM APL2 and by GNU APL) did not say how to resolve this ambiguity. The parentheses force ¨ to take ⌿ as a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With