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*/ |
||
values= '-3 -32 -47 49' /*values of each matrix row of numbers.*/ |
|||
variables= substr('abcdefghijklmnopqrstuvwxyz', 27 - words( |
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) |
do x=1 for sz; $= $ right(@.x.y, w) |
||
end /*y*/ |
end /*y*/ /* [↑] right─justify matrix elements.*/ |
||
say $ ' = ' right( word( |
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( |
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( |
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*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if sz==2 then return a*d - b*c |
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 |
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
w= max(w, length(@.k.j) ); we= max(we, length( word( equals, j) ) ) |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
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 $ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{out|output|text= when using the internal default inputs:}} |
{{out|output|text= 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> |
||