Cramer's rule: Difference between revisions

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




w = 2
w = 2
x = -12
x = -12
y = -4
y = -4
z = 1
z = 1
</pre>
</pre>