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

General APL language issues

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

Postby jmosk on Sun Feb 25, 2024 7:38 pm

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
jmosk
 
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

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

Postby Phil Last on Mon Feb 26, 2024 9:42 am

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
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

Postby yaemmanuelli on Mon Feb 26, 2024 11:10 am

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
yaemmanuelli
 
Posts: 16
Joined: Sat Aug 01, 2020 6:29 pm
Location: Recloses, France

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

Postby Adam|Dyalog on Sun Mar 03, 2024 5:43 am

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:
  1. As with ⌿ you can make ⊥ work along the trailing axis with ⍤1:
    Code: Select all
          2(⊥⍤1)matrix
    0 6 3 7
User avatar
Adam|Dyalog
 
Posts: 143
Joined: Thu Jun 25, 2015 1:13 pm


Return to Language

Who is online

Users browsing this forum: Google [Bot] and 1 guest