LU decomposition: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
(→‎{{header|Java}}: added Java)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 2,557:
 
=={{header|REXX}}==
<lang rexx>/*REXX pgmprogram makescreates a matrix from console input, performs/shows LU decomposition. */
#=0; P.=0; PA.=0; L.=0; U.=0 /*initialize some variables to 0zero. */
parse arg x /*get theobtain matrix elements from CLthe C.L. */
call makeMat /*make the A matrix from the numbers.*/
call showMat 'A', N /*display the A matrix. */
call manPmat /*manufacture P (permutation). */
call showMat 'P', N /*display the P matrix. */
call multMat /*multiply the A and P matrices. */
call showMat 'PA', N /*display the PA matrix. */
do y=1 for N; call manUmat y /*manufacture U matrix, parts. */
call manLmat y /*manufacture L matrix, parts. */
end
call showMat 'L', N /*display the L matrix. */
call showMat 'U', N /*display the U matrix. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────er subroutine───────────────────────*/
er: say; say '***error!***'; say; say arg(1); say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────makeMat subroutine──────────────────*/
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: ' ?
 
do r=1 for N /*build the "A" matrix from input*/
do c r=1 for N; #=#+1; /*build _=word(x,#);the "A" A.r.c=_matrix from the input*/
if \datatype(_,' do c=1 for N'); then call er "element#=#+1; _=word(x,#); isn't numeric: " A.r.c=_
if \datatype(_,'N') then call er "element isn't numeric: " _
end /*c*/
end end /*rc*/
end /*r*/
return
end return
/*──────────────────────────────────manLmat subroutine──────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manLmat: arg ? /*manufacture L (lower) matrix.*/
manLmat: parse arg ? do r=1 for N /*manufacture L (lower) matrix.*/
do c=1 for N; ifdo r==c then do; L.r.c=1; iterate;for endN
if do c\==?1 | for N; if r==c | c>then do; L.r.c=1; then iterate; end
_ if c\=PA.=? | r.==c | c>r then iterate
do k=1 for c-1; _=_-UPA.kr.c*L.r.k; end /*k*/
L.r. do k=1 for c-1; _=_/-U.ck.c*L.r.k; end /*k*/
end /* L.r.c*=_/U.c.c
end end /*rc*/
end /*r*/
return
end return
/*──────────────────────────────────manPmat subroutine──────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manPmat: c=N; do r=N by -1 for N /*manufacture P (permutation). */
manPmat: c=N; do r=N by P.r.c=-1; for c=c+1;N if c>N then c=N%2; if c==N then/*manufacture c=1P (permutation). */
end /* P.r*/.c=1; c=c+1; if c>N then c=N%2; if c==N then c=1
end /*cr*/
return
return
/*──────────────────────────────────manUmat subroutine──────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
manUmat: arg ? /*manufacture U (upper) matrix.*/
manUmat: parse arg ? do r=1 for N; if r\==? then iterate /*manufacture U (upper) matrix.*/
do c r=1 for N; if c<r\==? then iterate
_ do c=PA.r.1 for N; if c<r then iterate
do k=1 for r-1; _=_-UPA.kr.c*L.r.k; end /*k*/
do k=1 for r-1; _=_-U.rk.c=_*L.r.k; end /*k*/1
end /* U.r.c*=_/1
end end /*rc*/
do r=1 for N end /*build the "A" matrix from inputr*/
return
return
/*──────────────────────────────────multMat subroutine──────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
multMat: do i =1 for N /*multiply matrix P & A ──► PA */
multMat: do j i=1 for N /*multiply matrix P & A ──► PA */
do k=1 for N; pa.i.j = (pa.i.j + p.i.k * a.k.j) / do j=1; endfor N
end /* do k=1 for N; pa.i.j=(pa.i.j + p.i.k * a.k.j) / 1
end end /*ik*/
manLmat: arg ? /*manufacture Lend (lower) matrix./*j*/
return
manUmat: arg ? end /*manufacture U (upper) matrix./*i*/
/*──────────────────────────────────showMat subroutine──────────────────*/
return
showMat: parse arg mat,rows,cols; w=0; cols=word(cols rows,1); say
/*──────────────────────────────────────────────────────────────────────────────────────*/
do r =1 for rows
showMat: parse arg mat,rows,cols; w=0; do ccols=1 for word(cols; w=max(wrows,length(value(mat'.'r'.'c))1); end say
do r do r=1 for rows
end
do c=1 for cols; w=max(w, length( value( mat'.'r"."c ) ) )
say center(mat 'matrix',cols*(w+1)+7,"─")
do r =1 for rows; _= end /*c*/
do c=1 for cols; _=_ right(value(mat'.'r'.'c),w+1); end; say _ /*r*/
say center(mat 'matrix',cols*(w+1)+7,"─")
end
do r=1 for rows; _=
return</lang>
do c=1 for cols; _=_ right(value(mat'.'r'.'c),w+1); end /*c*/
{{out}} when using the input of: &nbsp; <tt> 1 3 5 &nbsp; 2 4 7 &nbsp; 1 1 0 </tt>
say _
end /*r*/
return</lang>
{{out}}'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 3 5 &nbsp; 2 4 7 &nbsp; 1 1 0 </tt>
<pre>
──A matrix───
Line 2,653 ⟶ 2,657:
0 0 -2
</pre>
{{out}}'''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>
<pre>
─────A matrix──────