4

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?

syntagma
  • 23,346
  • 16
  • 78
  • 134

3 Answers3

6

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.

ngn
  • 7,763
  • 6
  • 26
  • 35
  • By the way, the "rho" in your question is Unicode character `U+03C1 GREEK SMALL LETTER RHO` while it should be `U+2374 APL FUNCTIONAL SYMBOL RHO`. – ngn May 06 '13 at 06:57
  • The reshape approach might be considered better as it does not involve any computation, but rather just copies data. – Lobachevsky Mar 30 '15 at 14:58
3

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

MBaas
  • 7,248
  • 6
  • 44
  • 61
3

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

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

;)

Francesco
  • 31
  • 1