Cramer's rule: Difference between revisions

add REXX
(Added Julia version)
(add REXX)
Line 505:
w=2.0 x=-12.0 y=-4.0 z=1.0
</pre>
 
=={{header|REXX}}
<lang rexx>/* Use Cramer's rule to compute solutions of given linear equations */
names='w x y z'
M=' 2 -1 5 1',
' 3 2 2 -6',
' 1 3 3 -1',
' 5 -2 -3 3'
v=' -3',
'-32',
'-47',
' 49'
Call mk_mat(m) /* M -> a.i.j */
Do j=1 To dim /* Show the input */
ol=''
Do i=1 To dim
ol=ol format(a.i.j,3)
End
ol=ol format(word(v,j),5)
Say ol
End
Say copies('-',22)
 
d=det(m) /* denominator determinant */
 
Do k=1 To dim /* construct nominator matrix */
Do j=1 To 4
Do i=1 To dim
If i=k Then
b.i.j=word(v,j)
Else
b.i.j=a.i.j
End
End
Call show_b
d.k=det(mk_str()) /* numerator determinant */
Say word(names,k) '=' d.k/d /* compute value of variable k */
End
Exit
 
mk_mat: Procedure Expose a. dim /* Turn list into matrix a.i.j */
Parse Arg list
dim=sqrt(words(list))
k=0
Do j=1 To dim
Do i=1 To dim
k=k+1
a.i.j=word(list,k)
End
End
Return
 
mk_str: Procedure Expose b. dim /* Turn matrix b.i.j into list */
str=''
Do j=1 To dim
Do i=1 To dim
str=str b.i.j
End
End
Return str
 
show_b: Procedure Expose b. dim /* show numerator matrix */
do j=1 To dim
ol=''
Do i=1 To dim
ol=ol format(b.i.j,3)
end
Call dbg ol
end
Return
 
det: Procedure /* compute determinant */
Parse Arg list
n=words(list)
call dbg 'det:' list
do dim=1 To 10
If dim**2=n Then Leave
End
call dbg 'dim='dim
If dim=2 Then Do
det=word(list,1)*word(list,4)-word(list,2)*word(list,3)
call dbg 'det=>'det
Return det
End
k=0
Do j=1 To dim
Do i=1 To dim
k=k+1
a.i.j=word(list,k)
End
End
Do j=1 To dim
ol=j
Do i=1 To dim
ol=ol format(a.i.j,3)
End
call dbg ol
End
det=0
Do i=1 To dim
ol=''
Do j=2 To dim
Do ii=1 To dim
If ii<>i Then
ol=ol a.ii.j
End
End
call dbg 'i='i 'ol='ol
If i//2 Then
det=det+a.i.1*det(ol)
Else
det=det-a.i.1*det(ol)
End
Call dbg 'det=>>>'det
Return det
 
dbg: Return</lang>
{{out}}
<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}}==
<lang ruby>require 'matrix'
2,295

edits