Identity matrix: Difference between revisions

From The K Language Wiki
Content added Content deleted
m (Add a "Works in" for example code)
(Add a "Try it" link)
Line 2: Line 2:
Given a positive integer argument ''n'', '''identity matrix''' returns a ''n''×''n'' matrix with ones in the main diagonal and zeros elsewhere.
Given a positive integer argument ''n'', '''identity matrix''' returns a ''n''×''n'' matrix with ones in the main diagonal and zeros elsewhere.


[https://ngn.bitbucket.io/k#eJyzNeWyNeSyNQAABlwBYg== Try it!]
<syntaxhighlight lang=text>
<syntaxhighlight lang=text>
=5
=5

Revision as of 08:06, 7 July 2021

Identity matrix
{{{1}}}

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

Try it!

 =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
()
Works in: ngn/k

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)