Cramer's rule: Difference between revisions

→‎{{header|REXX}}: add a second version of a REXX program.
No edit summary
(→‎{{header|REXX}}: add a second version of a REXX program.)
Line 2,256:
 
=={{header|REXX}}==
=== version 1 ===
<lang rexx>/* REXX Use Cramer's rule to compute solutions of given linear equations */
Numeric Digits 20
Line 2,404 ⟶ 2,405:
y = -4
z = 1</pre>
 
=== version 2 ===
The REXX version is based on the REXX version 1 program with the following improvements:
* &nbsp; aligns all output
* &nbsp; shows the &nbsp; values &nbsp; of the linear equations
* &nbsp; uses a PARSE for finding some matrix elements
* &nbsp; allows larger matrices to be used
* &nbsp; finds the widest decimal numbers for better formatting instead of assuming six digits
* &nbsp; use true variable names, doesn't assume that there are only four variables
* &nbsp; uses exact comparisons where appropriate
* &nbsp; added a check to see if the matrix had all its elements specified, added an error message
* &nbsp; uses a faster form of &nbsp; '''DO''' &nbsp; loops
* &nbsp; elided dead code and superfluous statements
* &nbsp; elided the need for a high precision '''sqrt''' function
* &nbsp; eschewed the use of a variable name with a function with the same name &nbsp; (bad practice)
* &nbsp; eschewed the deprecated use of: &nbsp; &nbsp; ''call func(args)'' &nbsp; &nbsp; syntax
* &nbsp; 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*/
equals= '-3 -32 -47 49' /*what each row of the matrix equals. */
variables= substr('abcdefghijklmnopqrstuvwxyz', 27 - words(equals) ) /*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*/
say $ ' = ' right( word(equals, y), we)
end /*x*/
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(equals, j)
else !.i.j= @.i.j
end /*i*/
end /*j*/
say left('', 10) substr(variables,k,1) ' = ' right(det(makeS())/det(mat),we)
end /*k*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
det: procedure; parse arg a b c d 1 nums; call matSz 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 $
/*──────────────────────────────────────────────────────────────────────────────────────*/
matSz: arg _; do sz=0 for 1e3; if sz*sz==_ then return; end; say 'error,bad matrix';exit 9</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>
 
=={{header|Ruby}}==