Identity matrix

From The K Language Wiki
Revision as of 21:43, 6 July 2021 by Traws (talk | contribs) (include identity matrix)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Given a positive integer argument n, identity matrix returns a n×n matrix with ones in the main diagonal and zeros elsewhere.

 =5
(1 0 0 0 0
 0 1 0 0 0
 0 0 1 0 0
 0 0 0 1 0
 0 0 0 0 1)

 =1
,,1

 =0
()

This primitive is not available in all dialects, It can be implemented in K as:

 im:{(2#x)#1,&x}
 im[4]
(1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1)