Identity matrix: Difference between revisions

Content deleted Content added
Laurence (talk | contribs)
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 matrixesmatrices as such, so the problem is largely how to display the "matrix".
 
The code to display the matrices was kept as a stand-alone general-purpose (square) matrix display subroutine,
<br>subroutine, &nbsp; which, in part, finds&nbsp; thedetermines maximum widths ofif the integersquare andmatrix decimalis fractionindeed partsa (ifsquare any)matrix &nbsp;based andon uses themthe to
<br>number of elements given.
<br>align &nbsp; (right-justify according to the [possibly implied] decimal point) &nbsp; 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 subroutine──────────*/
identity_matrix: procedure; parse arg n; $=
 
It also finds the maximum widths of the integer and decimal fraction parts &nbsp; (if any) &nbsp; and uses those widths
do r=1 for n /*build identity matrix, by row,*/
<br>to align &nbsp; (right-justify according to the [possibly implied] decimal point) &nbsp; the columns of the square matrix.
do c=1 for n /* ··· and by col.*/
$=$ (r==c) /*append zero or one (if on diag)*/
end /*c*/
end /*r*/
 
It also tries to display a centered (and easier to read) matrix, &nbsp; along with a title.
call showMatrix 'identity matrix of size' n, $
<lang rexx>/*REXX program creates and displays any sized identity matrix. (centered, with title).*/
return
do k=3 to 6 /* [↓] build &and display a sq. matrix.*/
/*──────────────────────────────────SHOWMATRIX subroutine───────────────*/
call identity_matrixident_mat k /*build and& display a kxkKxK square matrix. */
showMatrix: procedure; parse arg hdr,x; #=words(x) /*#: # of elements*/
dp=0 end /*k*/ /*DP: [↑] use decgeneral─purpose fractiondisplay width.sub*/
w=0exit /*W:stick a fork in integerit, part widthwe're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
do n=1 until n*n>=#; _=word(x,n) /*find matrix order (size).*/
identity_matrixident_mat: procedure; parse arg n; $=
parse var _ y '.' f; w=max(w, length(y)); dp=max(dp, length(f))
do do r=1 for n /*build identity matrix, by row, and col*/
end /*n*/ /* [↑] idiomatically find widths to align output*/
w do c=w+1 for n; $= $ (r==c) /*append zero or one (if on diag). */
end /*c*/
say; say center(hdr, max(length(hdr)+6, (w+1)*#%n), '─'); say
#=0 end /*#: element #r*/
do call showMat 'identity matrix of row=1size' for n; _=left('',n+w) /*indentation.*/$
return
do col=1 for n; #=#+1 /*bump element*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
_=_ right(format(word(x, #), , dp)/1, w)
showMatrixshowMat: procedure; parse arg hdr,x; #=words(x) /*#: #is the number of matrix elements. */
end /*col*/ /* [↑] division by 1 normalizes #*/
dp= 0 say _ /*displayDP: onemax linewidth of thedecimal matrixfractions. */
w= 0 end /*rowW: max width of integer part. */
do n=1 until n*n>=#; _= word(x,n) /*finddetermine the matrix order. (size).*/
return</lang>
parse var _ y '.' f; w= max(w, length(y)); dp= max(dp, length(f) )
{{out}} using the default sizes &nbsp; (3 ──► 6) &nbsp; for generating four matrices:
end /*n*/ /* [↑] idiomatically find the widths. to align output*/
w= w +1
say; say center(hdr, max(length(hdr)+68, (w+1)*#%n), '─'); say
#= 0 /*#: element #.*/
do col row=1 for n; #_=# left('', n+1w) /*bumpindentation. element*/
do ccol=1 for n; #= # + 1 /* ··· and by col /*bump element.*/
_=_ right(format(word(x, #), , dp)/1, w)
end /*kcol*/ /* [↑] usedivision by unity general─purposenormalizes disp#.*/
exit say _ /*stickdisplay a forksingle inline it,of we'rethe donematrix. */
end end /*rrow*/
return</lang>
{{out}}|output|text=&nbsp; when using the default sizes &nbsp; (3 ──► 6) &nbsp; for generating four matrices:}}
<pre>
───identity────identity matrix of size 3───3────
 
1 0 0
Line 2,551 ⟶ 2,553:
0 0 1
 
───identity────identity matrix of size 4───4────
 
1 0 0 0
Line 2,558 ⟶ 2,560:
0 0 0 1
 
───identity────identity matrix of size 5───5────
 
1 0 0 0 0
Line 2,566 ⟶ 2,568:
0 0 0 0 1
 
───identity────identity matrix of size 6───6────
 
1 0 0 0 0 0