LU decomposition: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed statements, comments, and whitespace, used templates for the output sections.)
Line 2,823: Line 2,823:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
er: say; say '***error!***'; say; say arg(1); say; exit 13
er: say; say '***error***'; say; say arg(1); say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeMat: ?=words(x); do N=1 for ?; if N**2==? then leave; end /*N*/
makeMat: ?=words(x); do N=1 for ?; if N**2==? then leave; end /*N*/
if N**2\==? then call er 'not correct number of elements entered: ' ?
if N**2 \==? then call er 'not correct number of elements entered: ' ?


do r=1 for N /*build the "A" matrix from the input*/
do r=1 for N /*build the "A" matrix from the input*/
do c=1 for N; #=#+1; _=word(x,#); A.r.c=_
do c=1 for N; #=#+1; _=word(x,#); A.r.c=_
if \datatype(_,'N') then call er "element isn't numeric: " _
if \datatype(_, 'N') then call er "element isn't numeric: " _
end /*c*/
end /*c*/
end /*r*/
end /*r*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manLmat: parse arg ? /*manufacture L (lower) matrix.*/
manLmat: parse arg ? /*manufacture L (lower) matrix.*/
Line 2,840: Line 2,839:
if c\==? | r==c | c>r then iterate
if c\==? | r==c | c>r then iterate
_=PA.r.c
_=PA.r.c
do k=1 for c-1; _=_-U.k.c*L.r.k; end /*k*/
do k=1 for c-1; _=_ - U.k.c*L.r.k; end /*k*/
L.r.c=_/U.c.c
L.r.c=_ / U.c.c
end /*c*/
end /*c*/
end /*r*/
end /*r*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manPmat: c=N; do r=N by -1 for N /*manufacture P (permutation). */
manPmat: c=N; do r=N by -1 for N /*manufacture P (permutation). */
P.r.c=1; c=c+1; if c>N then c=N%2; if c==N then c=1
P.r.c=1; c=c+1; if c>N then c=N % 2; if c==N then c=1
end /*r*/
end /*r*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manUmat: parse arg ? /*manufacture U (upper) matrix.*/
manUmat: parse arg ? /*manufacture U (upper) matrix.*/
Line 2,855: Line 2,852:
do c=1 for N; if c<r then iterate
do c=1 for N; if c<r then iterate
_=PA.r.c
_=PA.r.c
do k=1 for r-1; _=_-U.k.c*L.r.k; end /*k*/
do k=1 for r-1; _=_ - U.k.c*L.r.k; end /*k*/
U.r.c=_/1
U.r.c=_ / 1
end /*c*/
end /*c*/
end /*r*/
end /*r*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
multMat: do i=1 for N /*multiply matrix P & A ──► PA */
multMat: do i=1 for N /*multiply matrix P & A ──► PA */
do j=1 for N
do j=1 for N
do k=1 for N; pa.i.j=(pa.i.j + p.i.k * a.k.j) / 1
do k=1 for N; pa.i.j=(pa.i.j + p.i.k * a.k.j) / 1
end /*k*/
end /*k*/
end /*j*/
end /*j*/
end /*i*/
end /*i*/; return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: parse arg mat,rows,cols; w=0; cols=word(cols rows,1); say
showMat: parse arg mat,rows,cols; w=0; cols=word(cols rows,1); say
do r=1 for rows
do r=1 for rows
do c=1 for cols; w=max(w, length( value( mat'.'r"."c ) ) )
do c=1 for cols; w=max(w, length( value( mat'.'r"."c ) ) )
end /*c*/
end /*c*/
end /*r*/
end /*r*/
say center(mat 'matrix',cols*(w+1)+7,"─")
say center(mat 'matrix', cols * (w + 1) + 7, "─")
do r=1 for rows; _=
do r=1 for rows; _=
do c=1 for cols; _=_ right(value(mat'.'r'.'c),w+1); end /*c*/
do c=1 for cols; _=_ right(value(mat'.'r"."c), w + 1)
end /*c*/
say _
say _
end /*r*/
end /*r*/; return</lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 3 5 &nbsp; 2 4 7 &nbsp; 1 1 0 </tt>}}
return</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 3 5 &nbsp; 2 4 7 &nbsp; 1 1 0 </tt>
<pre>
<pre>
──A matrix───
──A matrix───
Line 2,907: Line 2,902:
0 0 -2
0 0 -2
</pre>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 11 9 24 2 &nbsp; 1 5 2 6 &nbsp; 3 17 18 1 &nbsp; 2 5 7 1 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 11 9 24 2 &nbsp; 1 5 2 6 &nbsp; 3 17 18 1 &nbsp; 2 5 7 1 </tt>}}
<pre>
<pre>
─────A matrix──────
─────A matrix──────