The most idiomatic way of creating identity matrix in APL

≡放荡痞女 提交于 2019-12-01 22:30:49

问题


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?


回答1:


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




回答2:


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.




回答3:


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

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

;)



来源:https://stackoverflow.com/questions/16385783/the-most-idiomatic-way-of-creating-identity-matrix-in-apl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!