Identity matrix: Difference between revisions
Content deleted Content added
Adding Fōrmulæ |
m →version 1: added/changed comments and whitespace, used a template for the output section. |
||
Line 2,505:
=={{header|REXX}}==
===version 1===
The REXX language doesn't have
The code to display the matrices was kept as a stand-alone general-purpose (square) matrix display
<br>subroutine, which, in part,
<br>number of elements given.
<br>align (right-justify according to the [possibly implied] decimal point) the columns of the square matrix. ▼
<lang rexx>/*REXX program creates and displays any sized identity matrix. */▼
do k=3 to 6 /* [↓] build & display a matrix.*/▼
call identity_matrix k /*build and display a kxk matrix.*/▼
end /*k*/ /* [↑] use general─purpose disp.*/▼
exit /*stick a fork in it, we're done.*/▼
identity_matrix: procedure; parse arg n; $=▼
It also finds the maximum widths of the integer and decimal fraction parts (if any) and uses those widths
do r=1 for n /*build identity matrix, by row,*/▼
▲<br>to align (right-justify according to the [possibly implied] decimal point) the columns of the square matrix.
do c=1 for n /* ··· and by col.*/▼
end /*c*/▼
end /*r*/▼
It also tries to display a centered (and easier to read) matrix, along with a title.
▲<lang rexx>/*REXX program creates
showMatrix: procedure; parse arg hdr,x; #=words(x) /*#: # of elements*/▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
do n=1 until n*n>=#; _=word(x,n) /*find matrix order (size).*/▼
parse var _ y '.' f; w=max(w, length(y)); dp=max(dp, length(f))▼
end /*n*/ /* [↑] idiomatically find widths to align output*/▼
▲ end /*c*/
say; say center(hdr, max(length(hdr)+6, (w+1)*#%n), '─'); say▼
return
do col=1 for n; #=#+1 /*bump element*/▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
_=_ right(format(word(x, #), , dp)/1, w)▼
▲
dp= 0
w= 0
return</lang>▼
▲ parse var _ y '.' f; w= max(w, length(y)); dp= max(dp, length(f) )
{{out}} using the default sizes (3 ──► 6) for generating four matrices:▼
w= w +1
#= 0 /*#: element #.*/
▲ return</lang>
▲{{out
<pre>
1 0 0
Line 2,551 ⟶ 2,553:
0 0 1
1 0 0 0
Line 2,558 ⟶ 2,560:
0 0 0 1
1 0 0 0 0
Line 2,566 ⟶ 2,568:
0 0 0 0 1
1 0 0 0 0 0
|