Identity matrix: Difference between revisions

From The K Language Wiki
Content added Content deleted
(include identity matrix)
 
mNo edit summary
Line 1: Line 1:
{{Primitive}}
Given a positive integer argument ''n'', '''identity matrix''' returns a ''n''×''n'' matrix with ones in the main diagonal and zeros elsewhere.<syntaxhighlight>
Given a positive integer argument ''n'', '''identity matrix''' returns a ''n''×''n'' matrix with ones in the main diagonal and zeros elsewhere.<syntaxhighlight lang=text>
=5
=5
(1 0 0 0 0
(1 0 0 0 0
Line 12: Line 13:
=0
=0
()
()
</syntaxhighlight>This primitive is not available in all dialects, It can be implemented in K as:<syntaxhighlight>
</syntaxhighlight>This primitive is not available in all dialects, It can be implemented in K as:<syntaxhighlight lang=text>
im:{(2#x)#1,&x}
im:{(2#x)#1,&x}
im[4]
im[4]
Line 19: Line 20:
0 0 1 0
0 0 1 0
0 0 0 1)
0 0 0 1)

</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:03, 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.

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