Cramer's rule: Difference between revisions

Go solution
m (Edit to clone function comment to increase accuracy.)
(Go solution)
Line 439:
</pre>
And at this point I suddenly noticed that the habits of Old Fortran are not so easily suppressed: all calculations are done with double precision. Curiously enough, for the specific example data, the same results are obtained if all variables are integer.
 
=={{header|Go}}==
'''Library gonum:'''
<lang go>package main
 
import (
"fmt"
 
"github.com/gonum/matrix/mat64"
)
 
var m = mat64.NewDense(4, 4, []float64{
2, -1, 5, 1,
3, 2, 2, -6,
1, 3, 3, -1,
5, -2, -3, 3,
})
 
var v = []float64{-3, -32, -47, 49}
 
func main() {
x := make([]float64, len(v))
b := make([]float64, len(v))
d := mat64.Det(m)
for c := range v {
mat64.Col(b, c, m)
m.SetCol(c, v)
x[c] = mat64.Det(m) / d
m.SetCol(c, b)
}
fmt.Println(x)
}</lang>
{{out}}
<pre>
[2 -12.000000000000007 -4.000000000000001 1.0000000000000009]
</pre>
'''Library go.matrix:'''
<lang go>package main
 
import (
"fmt"
 
"github.com/skelterjohn/go.matrix"
)
 
var m = matrix.MakeDenseMatrixStacked([][]float64{
{2, -1, 5, 1},
{3, 2, 2, -6},
{1, 3, 3, -1},
{5, -2, -3, 3},
})
 
var v = []float64{-3, -32, -47, 49}
 
func main() {
x := make([]float64, len(v))
b := make([]float64, len(v))
d := m.Det()
for c := range v {
m.BufferCol(c, b)
m.FillCol(c, v)
x[c] = m.Det() / d
m.FillCol(c, b)
}
fmt.Println(x)
}</lang>
{{out}}
<pre>
[2.0000000000000004 -11.999999999999998 -4 0.9999999999999999]
</pre>
 
=={{header|Haskell}}==
1,707

edits