Cramer's rule: Difference between revisions
Added Easylang
(→{{header|jq}}: fix bug) |
(Added Easylang) |
||
(2 intermediate revisions by 2 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]
.
.
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
.
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 2,311 ⟶ 2,369:
=={{header|Racket}}==
#lang racket
(require math/matrix)
(define sys
(matrix [[2 -1 5 1]
(define soln
▲<syntaxhighlight lang="racket">#lang typed/racket
(define
(matrix-augment (list-set (matrix-cols M) idx new-col)))
▲ [3 2 2 -6]
▲ [1 3 3 -1]
▲ [5 -2 -3 3]]))
(define
(let ([denom (matrix-determinant M)]
▲ -32
[nvars (matrix-num-cols
(letrec ([roots (λ (position)
▲ 49]))
(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,974 ⟶ 3,046:
=={{header|Wren}}==
{{libheader|Wren-matrix}}
<syntaxhighlight lang="
var cramer = Fn.new { |a, d|
|