Cramer's rule: Difference between revisions
Added Easylang
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: some sigil-less, other changes for code-readability) |
(Added Easylang) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 730:
{{out}}
<pre>w = 2, x = -12, y = -4, z = 1</pre>
=={{header|EasyLang}}==
{{trans|Phix}}
<syntaxhighlight>
proc det . a0[][] res .
res = 1
a[][] = a0[][]
n = len a[][]
for j to n
imax = j
for i = j + 1 to n
if a[i][j] > a[imax][j]
imax = i
.
.
if imax <> j
swap a[imax][] a[j][]
res = -res
.
if abs a[j][j] < 1e-12
print "Singular matrix!"
res = 0 / 0
return
.
for i = j + 1 to n
mult = -a[i][j] / a[j][j]
for k to n
a[i][k] += mult * a[j][k]
.
.
.
for i to n
res *= a[i][i]
.
.
proc cramer_solve . a0[][] deta b[] col res .
a[][] = a0[][]
for i to len a[][]
a[i][col] = b[i]
.
det a[][] d
res = d / deta
.
a[][] = [ [ 2 -1 5 1 ] [ 3 2 2 -6 ] [ 1 3 3 -1 ] [ 5 -2 -3 3 ] ]
b[] = [ -3 -32 -47 49 ]
det a[][] deta
for i to len a[][]
cramer_solve a[][] deta b[] i r
print r
.
</syntaxhighlight>
{{out}}
<pre>
2.00
-12
-4
1.00
</pre>
=={{header|EchoLisp}}==
Line 1,767 ⟶ 1,825:
| (a | det) as $ad
| if $ad == 0 then "matrix determinant is 0" | error
(reduce range(0; $n) as $r (a; .[$r][$c] = d[$r])) as $aa
| .[$c] = ($aa|det) / $ad )
end ;
def a: [
Line 1,786 ⟶ 1,845:
Solution is [2,-12,-4,1]
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 2,309 ⟶ 2,369:
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(require math/matrix)
(define sys
(matrix [[2 -1 5 1]
[3 2 2 -6]
[1 3 3 -1]
[5 -2 -3 3]]))
(define soln
(define
(matrix-augment (list-set (matrix-cols M) idx new-col)))
(define
(let ([denom (matrix-determinant M)]
[nvars (matrix-num-cols
(letrec ([roots (λ (position)
(if (>= position nvars)
'()
(cons (/ (matrix-determinant
(matrix-set-column M soln position))
denom)
(roots (add1 position)))))])
(map cons '(w x y z) (roots 0)))))
(cramers-rule sys soln)
</syntaxhighlight>
{{out}}
<pre>'
=={{header|Raku}}==
Line 2,606 ⟶ 2,680:
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
===Explicit Cramer's rule===
≪ '''IF''' OVER DET
'''THEN'''
LAST ROT DUP SIZE 1 GET → vect det mtx dim
≪ 1 dim '''FOR''' c
mtx
1 dim '''FOR''' r
r c 2 →LIST vect r GET PUT
'''NEXT'''
DET det /
'''NEXT'''
dim →ARRY
≫
'''END'''
≫
‘CRAMR’ STO
[[ 2 -1 5 1 ][ 3 2 2 -6 ][ 1 3 3 -1 ][ 5 -2 -3 3 ]]
[ -3 -32 -47 49 ] CRAMR
{{out}}
<pre>
1: [ 2 -12 -4 1 ]
</pre>
===Implicit Cramer's rule===
RPL use Cramer's rule for its built-in equation system resolution feature, performed by the <code>/</code> instruction.
[ -3 -32 -47 49 ]
[[ 2 -1 5 1 ][ 3 2 2 -6 ][ 1 3 3 -1 ][ 5 -2 -3 3 ]]
/
{{out}}
<pre>
1: [ 2 -12 -4 1 ]
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'matrix'
Line 2,937 ⟶ 3,046:
=={{header|Wren}}==
{{libheader|Wren-matrix}}
<syntaxhighlight lang="
var cramer = Fn.new { |a, d|
|