Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding J From

Tags:

j

In J:

a =: 2 3 $ 1 2 3 4 5 6

Gives:

1 2 3
4 5 6

Which is a 2 3 shaped array.

If I do:

0 1 { a

I (noting that 0 1 is a 2 shaped list) expected to have back:

1 2 3 4 5 6

But got the following instead:

1 2 3
4 5 6

Reading the documentation I was expecting the shape of the index to kinda govern the shape of the answer.

Can someone clarify what I am missing here?

like image 672
jima Avatar asked Dec 06 '25 15:12

jima


1 Answers

Higher-dimensional arrays may help make this clear. An array with n dimensions has items with n-1 dimensions. When you select an item from ({) a three-dimensional array, your result is a two-dimensional array:

   1 { i. 5 3 4
12 13 14 15
16 17 18 19
20 21 22 23

When you select multiple items from an array, the items are assembled into a new array, using each atom of x to select a item of y. This might be where you picked up the idea that the shape of x affects the shape of the result.

   2 1 0 2 { 'set'
test
   $ 2 1 0 2
4
   $ 'test'
4

The dimensions of the result is equal to the dimensions of x plus the dimensions of the items of y. So, if you have a two-dimensional x taking two-dimensional items from a three-dimensional y, you will have a four-dimensional result:

   (2 2 $ 1 1 0 1) { i. 5 3 4
12 13 14 15
16 17 18 19
20 21 22 23

12 13 14 15
16 17 18 19
20 21 22 23


 0  1  2  3
 4  5  6  7
 8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23
   $ (2 2 $ 1 1 0 1) { i. 5 3 4
2 2 3 4

One final note: the monadic Ravel (,) will reduce the result to a list (one-dimensional array).

   , 0 1 { 2 3 $ 1 2 3 4 5 6
1 2 3 4 5 6
   , i. 2 2 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
like image 170
Dane Avatar answered Dec 10 '25 04:12

Dane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!