Search code examples
j

J Language rank of power function


t=:1
test=: monad define
    t=.y
    t=. t, 0
)   
testloop=: monad def'test^:y t'
testloop 1
1 0
testloop 2
1 0 0
testloop 10
1 0 0 0 0 0 0 0 0 0 0

In order to simplify this

(testloop 0),(testloop 1), (testloop 2), ...

110100100010000...

I tried

, testloop"0 (i.10)

but it gives

1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0...

It seems like I have a problem with a rank, I can't figure out which one to use. I would be grateful if you could help me on this issue. Thank you!


Solution

  • This is not so much a rank problem as the fact that the results are padded with zeros so that the row lengths match.

       testloop 1
    1 0
       testloop 2
    1 0 0
       testloop"0 [ 1 2 
    1 0 0
    1 0 0
       testloop"0 [ 1 2 3
    1 0 0 0
    1 0 0 0
    1 0 0 0
    

    If I redefine your test and testloop to add a different appending digit, we can see how the padding is working.

       test2 =: 3 : 0
    ​t=. y
    ​t=. t,2
    ​)
       test2loop=: monad def'test2^:y t'
       test2loop"0 [1
    1 2
       test2loop"0 [2
    1 2 2
       test2loop"0 [ 1 2 NB. 0 padded in first row
    1 2 0
    1 2 2
       test2loop"0 [ 1 2 3  NB. 0's padded in first two rows
    1 2 0 0
    1 2 2 0
    1 2 2 2
    

    To get around the padding issue I will use each=: &.> so that the results are boxed before combining to avoid the padding.

       testloop each 1 2 3
    +---+-----+-------+
    |1 0|1 0 0|1 0 0 0|
    +---+-----+-------+
       testloop each i. 10
    +-+---+-----+-------+---------+-----------+-------------+---------------+-----------------+-------------------+
    |1|1 0|1 0 0|1 0 0 0|1 0 0 0 0|1 0 0 0 0 0|1 0 0 0 0 0 0|1 0 0 0 0 0 0 0|1 0 0 0 0 0 0 0 0|1 0 0 0 0 0 0 0 0 0|
    +-+---+-----+-------+---------+-----------+-------------+---------------+-----------------+-------------------+
    

    using ; to unbox and ravel the results

       ; testloop each i. 10
    1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    

    To be honest I would be more inclined to use the fact that complex numbers used as the left argument of # introduce 0's for padding. The number of 0's depends on the imaginary value of the complex number.

       1j0 # 1
    1
       1j1 # 1
    1 0
       1j2 # 1
    1 0 0
       test3=: monad def '(1 j. y)#1'
       test3 1
    1 0
       test3 2
    1 0 0
       test3 1 2
    1 0 1 0 0
       test3 i. 10
    1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0