Identity matrix: Difference between revisions

From The K Language Wiki
Content added Content deleted
mNo edit summary
("Add all appropriate categories to each verb")
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{primitive|=i}}
{{Primitive}}

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>
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>
=5
=5
(1 0 0 0 0
(1 0 0 0 0
Line 13: Line 17:
=0
=0
()
()
</syntaxhighlight>{{Works in|ngn/k}}
</syntaxhighlight>This primitive is not available in all dialects, It can be implemented in K as:<syntaxhighlight lang=text>

im:{(2#x)#1,&x}
This primitive is not available in all dialects. There are multiple ways to implement it in K:<syntaxhighlight lang=text>
im:{t=/:t:!x}
im[4]
im[4]
(1 0 0 0
(1 0 0 0
Line 20: Line 26:
0 0 1 0
0 0 1 0
0 0 0 1)
0 0 0 1)

</syntaxhighlight>
im:{(2#x)#1,&x}
im[2]
(1 0
0 1)

im:{(x-1)(0 :':)\~!x} / fast
im[3]
(1 0 0
0 1 0
0 0 1)
</syntaxhighlight>

[[Category:Operator verbs]]
[[Category:Verbs]]
[[Category:Primitives]]

Latest revision as of 03:15, 9 July 2022

Identity matrix
=i

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. There are multiple ways to implement it in K:

 im:{t=/:t:!x}
 im[4]
(1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1)

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

 im:{(x-1)(0 :':)\~!x}   / fast
 im[3]
(1 0 0
 0 1 0
 0 0 1)