Question

According to Rosetta Code, there are two idiomatic ways of creating identity matrix in APL:

1. ID←{∘.=/⍳¨ ⍵ ⍵}
2. ID←{⍵ ⍵ ρ 1, ⍵ρ0}

How does the (2) work? Why is this better than the (1), which uses Outer Product that is considered idiomatic approach in APL?

Was it helpful?

Solution 2

If you compare the performance of the two expressions, 2 clearly wins:

cmpx'{∘.=/⍳¨ ⍵ ⍵}1000'  '{⍵ ⍵ ⍴ 1, ⍵⍴0}1000'       
  {∘.=/⍳¨ ⍵ ⍵}1000   → 2.4E¯3 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
* {⍵ ⍵ ⍴ 1, ⍵⍴0}1000 → 5.7E¯5 | -98% ⎕                                       

If you consider what the interpreter has to do when processing the two expressions, (2) is also far less work: catenate a scalar to a vec and reshape the result, whereas in (1) it has to create two vectors, build an outer product with an equal-comparison. Plus it involves "each" which is (by some) not considered "pure APL"... And obviously if you think about the ideas implemented by the two algorithms, (2) is much nicer and more elegant. But that's only my opinion ;)

OTHER TIPS

1,⍵⍴0 creates a vector that consists of a 1 followed by zeros. So, the length of this vector is ⍵+1.

⍵ ⍵ ⍴ covers an -by- matrix. Copies of the vector will be fit left to right and top to bottom. The first copy will cover the entire first row and overflow to the second row, e.g. for ⍵=5:

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

Now, the second copy will come in with a little indent on the second row:

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

and so forth until we cover all of the matrix. It's not necessarily an exact cover, the last copy may be cut off. If you picture this process further, you can see that the 1-s will land on the main diagonal.

I don't know why this should be a better approach than the one using outer product. Either seems fine.

{⍵ ⍵⍴(⍵+1)↑1} ... fast

{∘.=⍨⍳⍵} ... nice

;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top