Cramer's rule: Difference between revisions

Added Easylang
(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]
imax = -32i
.
.
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 2,311 ⟶ 2,369:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang typed/racket
#lang racket
(require math/matrix)
 
(define sys
{{incorrect|Racket|Doesn't really use Cramer's rule}}
(matrix [[2 -1 5 1]
[3 2 2 -6]
[1 3 3 -1]
[5 -2 -3 493]]))
 
(define soln
<syntaxhighlight lang="racket">#lang typed/racket
(require math/ (col-matrix [-3 -32 -47 49]))
 
(define A (matrix-set-column [[2M new-1 5 col 1]idx)
(matrix-augment (list-set (matrix-cols M) idx new-col)))
[3 2 2 -6]
[1 3 3 -1]
[5 -2 -3 3]]))
 
(define B (colcramers-matrixrule [M -3soln)
(let ([denom (matrix-determinant M)]
-32
[nvars (matrix-num-cols -47M)])
(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>
 
(matrix->vector (matrix-solve A B))</syntaxhighlight>
{{out}}
 
<pre>'#((w . 2) (x . -12) (y . -4) (z . 1))</pre>
 
=={{header|Raku}}==
Line 2,974 ⟶ 3,046:
=={{header|Wren}}==
{{libheader|Wren-matrix}}
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
 
var cramer = Fn.new { |a, d|
2,056

edits