Anonymous user
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*/
variables= substr('abcdefghijklmnopqrstuvwxyz', 27 - words(
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)
end /*y*/ /* [↑] right─justify matrix elements.*/
say $ ' = ' right( word(
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(
else !.i.j= @.i.j
end /*i*/
end /*j*/
say left('', 10) substr(variables,k,1) ' = ' right(det(
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▼
/*──────────────────────────────────────────────────────────────────────────────────────*/▼
if sz==2 then return a*d - b*c
aa= 0
▲/*──────────────────────────────────────────────────────────────────────────────────────*/
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, #)▼
end /*k*/▼
end; /*j*/ return▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲matSz: arg _; do sz=0 for 1e3; if sz*sz==_ then return; end; say 'error,bad matrix';exit 9</lang>
{{out|output|text= 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>
|