Reduced row echelon form: Difference between revisions

Content added Content deleted
m (Use "strformat".)
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 3,505: Line 3,505:
''Reduced Row Echelon Form''   (a.k.a.   ''row canonical form'')   of a matrix, with optimization added.
''Reduced Row Echelon Form''   (a.k.a.   ''row canonical form'')   of a matrix, with optimization added.
<lang rexx>/*REXX pgm performs Reduced Row Echelon Form (RREF), AKA row canonical form on a matrix)*/
<lang rexx>/*REXX pgm performs Reduced Row Echelon Form (RREF), AKA row canonical form on a matrix)*/
cols=0; w=0; @.=0 /*max cols in a row; max width; matrix.*/
cols= 0; w= 0; @. =0 /*max cols in a row; max width; matrix.*/
mat.=; mat.1= ' 1 2 -1 -4 '
mat.=; mat.1= ' 1 2 -1 -4 '
mat.2= ' 2 3 -1 -11 '
mat.2= ' 2 3 -1 -11 '
mat.3= ' -2 0 -3 22 '
mat.3= ' -2 0 -3 22 '
do r=1 until mat.r==''; _=mat.r /*build @.row.col from (matrix) mat.X*/
do r=1 until mat.r==''; _=mat.r /*build @.row.col from (matrix) mat.X*/
do c=1 until _=''; parse var _ @.r.c _
do c=1 until _=''; parse var _ @.r.c _
w=max(w, length(@.r.c) + 1) /*find the maximum width of an element.*/
w= max(w, length(@.r.c) + 1) /*find the maximum width of an element.*/
end /*c*/
end /*c*/
cols=max(cols, c) /*save the maximum number of columns. */
cols= max(cols, c) /*save the maximum number of columns. */
end /*r*/
end /*r*/
rows=r - 1 /*adjust the row count (from DO loop).*/
rows= r-1 /*adjust the row count (from DO loop). */
call showMat 'original matrix' /*display the original matrix to screen*/
call showMat 'original matrix' /*display the original matrix──►screen.*/
!=1 /*set the working column pointer to 1.*/
!= 1 /*set the working column pointer to 1.*/
/* ┌──────────────────────◄────────────────◄──── Reduced Row Echelon Form on matrix.*/
/* ┌──────────────────────◄────────────────◄──── Reduced Row Echelon Form on matrix.*/
do r=1 for rows while cols>! /*begin to perform the heavy lifting. */
do r=1 for rows while cols>! /*begin to perform the heavy lifting. */
j=r /*use a subsitute index for the DO loop*/
j= r /*use a subsitute index for the DO loop*/
do while @.j.!==0; j=j + 1
do while @.j.!==0; j= j + 1
if j==rows then do; j=r; !=! + 1; if cols==! then leave r; end
if j==rows then do; j= r; != ! + 1; if cols==! then leave r; end
end /*while*/
end /*while*/
/* [↓] swap rows J,R (but not if same)*/
/* [↓] swap rows J,R (but not if same)*/
do _=1 for cols while j\==r; parse value @.r._ @.j._ with @.j._ @._._
do _=1 for cols while j\==r; parse value @.r._ @.j._ with @.j._ @._._
end /*_*/
end /*_*/
?=@.r.!
?= @.r.!
do d=1 for cols while ?\=1; @.r.d= @.r.d / ?
do d=1 for cols while ?\=1; @.r.d= @.r.d / ?
end /*d*/ /* [↑] divide row J by @.r.p ──unless≡1*/
end /*d*/ /* [↑] divide row J by @.r.p ──unless≡1*/
Line 3,535: Line 3,535:
end /*s*/
end /*s*/
end /*k*/ /* [↑] for the rest of numbers in row.*/
end /*k*/ /* [↑] for the rest of numbers in row.*/
!=! + 1 /*bump the column pointer. */
!= !+1 /*bump the working column pointer. */
end /*r*/
end /*r*/


Line 3,541: Line 3,541:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: parse arg title; say; say center(title, 3 + (cols+1) * w, '─'); say
showMat: parse arg title; say; say center(title, 3 + (cols+1) * w, '─'); say
do r=1 for rows; _=
do r=1 for rows; _=
do c=1 for cols
do c=1 for cols
if @.r.c=='' then do; say "***error*** matrix element isn't defined:"
if @.r.c=='' then do; say "***error*** matrix element isn't defined:"
say 'row' r", column" c'.'; exit 13
say 'row' r", column" c'.'; exit 13
end
end
_=_ right(@.r.c, w)
_= _ right(@.r.c, w)
end /*c*/
end /*c*/
say _ /*display a matrix row to the terminal.*/
say _ /*display a matrix row to the terminal.*/
end /*r*/; return</lang>
end /*r*/; return</lang>
{{out|output|text=&nbsp; when using the default (internal) input:}}
{{out|output|text=&nbsp; when using the default (internal) input:}}
<pre>
<pre>