Why does decode of a matrix work by column and not row

General APL language issues
Post Reply
User avatar
jmosk
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Why does decode of a matrix work by column and not row

Post by jmosk »

I have matrix ← 4 3 ⍴ 0 0 0 1 1 0 0 1 1 1 1 1

Code: Select all

0 0 0
1 1 0
0 1 1
1 1 1
I wish to convert each row from binary to decimal with the ⊥ primitive. The result is

Code: Select all

2⊥ matrix
5 7 3
I thought/expected to get 0 6 3 7. I had to transpose the matrix to get what I thought.

Code: Select all

2⊥⍉matrix
0 6 3 7
Yet if I sum reduce the matrix, it operates by rows

Code: Select all

+/matrix
0 2 2 3
So why does the ⊥ seem to function on rank 2, while the / functions on rank 1? There is a separate ⌿ function to operate on rank 2.

Code: Select all

+⌿matrix
2 3 2
This seems inconsistent between the two primitives with one operating column-wise and the other row-wise.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
User avatar
Phil Last
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Why does decode of a matrix work by column and not row

Post by Phil Last »

In fact decode works not on rank 2 but on the first dimension. The fact that reduction works on the last dimension is a mistake or oversight made in the earliest impementations and only corrected by Ken and Roger when they implemented J.

So the why is history.
User avatar
yaemmanuelli
Posts: 16
Joined: Sat Aug 01, 2020 6:29 pm
Location: Recloses, France
Contact:

Re: Why does decode of a matrix work by column and not row

Post by yaemmanuelli »

Hello,
Decode syntax is derived from internal product syntax which operates on last dimension of left argument and first dimension of right argument.
Regards.
--
Yves-Antoine Emmanuelli
--
Yves-Antoine Emmanuelli
User avatar
Adam|Dyalog
Posts: 143
Joined: Thu Jun 25, 2015 1:13 pm

Re: Why does decode of a matrix work by column and not row

Post by Adam|Dyalog »

To summarise previous posts:
  1. Regard ⌿ as the primary form and works along the leading axis, and / is convenient name for ⌿⍤1 which works along the trailing axis
  2. ⊥ is a convenient wrapper on +.× namely {⍵+.×⍨⌽×\1,⍺⍴⍨¯1+≢⍵} making it work along the leading axis
And to fix your issue:
  • As with ⌿ you can make ⊥ work along the trailing axis with ⍤1:

    Code: Select all

          2(⊥⍤1)matrix
    0 6 3 7
    
Post Reply