Cramer's rule: Difference between revisions

m
→‎version 2: shrunk a function.
(→‎{{header|Phix}}: with js, cow note, added syntax colouring the hard way)
m (→‎version 2: shrunk a function.)
Line 2,434:
*   automatically used the minimum width when showing the matrix elements and equation values
<lang rexx>/*REXX program uses Cramer's rule to find and display solution of given linear equations*/
equalsvalues= '-3 -32 -47 49' /*whatvalues of each matrix row of the matrix equalsnumbers. */
variables= substr('abcdefghijklmnopqrstuvwxyz', 27 - words(equalsvalues) ) /*variable names.*/
call makeM '2 -1 5 1 3 2 2 -6 1 3 3 -1 5 -2 -3 3'
do y=1 for sz; $= /*display the matrix (linear equations)*/
do x=1 for sz; $= $ right(@.x.y, w) /*right─justify matrix elements.*/
end /*y*/ /* [↑] right─justify matrix elements.*/
say $ ' = ' right( word(equalsvalues, y), we)
end /*x*/ /* [↑] obtain value of the equation. */
say; say
do k=1 for sz /*construct the nominator matrix. */
do j=1 for sz
do i=1 for sz; if i==k then !.i.j= word(equalsvalues, j)
else !.i.j= @.i.j
end /*i*/
end /*j*/
say left('', 10) substr(variables,k,1) ' = ' right(det(makeSmakeL())/det(mat),we)
end /*k*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeL: $=; do x=1 for sz; do y=1 for sz; $= $ !.x.y; end; end; return $ /*matrix─►list*/
det: procedure; parse arg a b c d 1 nums; call matSz words(nums); _= 0
matSzmSize: arg _; do sz=0 for 1e3; if sz*sz==_ then return; end; say 'error,bad matrix';exit 9</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
det: procedure; parse arg a b c d 1 nums; call matSzmSize words(nums); _= 0
if sz==2 then return a*d - b*c
do j=1 for sz
do i=1 for sz; _= _ + 1; @.i.j= word(nums, _)
end /*i*/
end
aa= 0
do i=1 for sz; odd= i//2; $=
do j=2 for sz-1
do k=1 for sz; if k\==i then $= $ @.k.j
end /*k*/
end /*j*/
aa= aa - (-1 ** odd) * @.i.1 * det($)
end; /*i*/; return aa
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeM: procedure expose @. equals mat sz w we; parse arg mat; call matSz words(mat)
#= 0; w= 0; we= 0
do j=1 for sz
do k=1 for sz; #= # + 1; @.k.j= word(mat, #)
w= max(w, length(@.k.j) ); we= max(we, length( word( equals, j) ) )
end /*k*/
end; /*j*/ return
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeS: procedure expose !. sz; $= /*convert matrix !.i.j into a list. */
do j=1 for sz
do k=1 for sz; $= $ !.j.k
end /*k*/
end /*j*/; return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeM: procedure expose @. equalsvalues mat sz w we; parse arg mat; call matSzmSize words(mat)
matSz: arg _; do sz=0 for 1e3; if sz*sz==_ then return; end; say 'error,bad matrix';exit 9</lang>
#= 0; w= 0; we= digits() + 2; we w= 0
do j=1 for sz
do k=1 for sz; #= # + 1; @.k.j= word(mat, #); w= max(w, length(@.k.j) )
end /*k*/
end; /*j*/ ; return</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
2 -1 5 1 = -3
3 2 2 -6 = -32
1 3 3 -1 = -47
5 -2 -3 3 = 49
 
 
w = 2
x = -12
y = -4
z = 1
</pre>