LU decomposition: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: add gonum/matrix version)
(→‎Library gonum/matrix: update for library change, also improve output formatting a bit.)
Line 1,233: Line 1,233:
1, 1, 0,
1, 1, 0,
}))
}))
fmt.Println()
showLU(mat64.NewDense(4, 4, []float64{
showLU(mat64.NewDense(4, 4, []float64{
11, 9, 24, 2,
11, 9, 24, 2,
Line 1,241: Line 1,242:
}
}


func showLU(m *mat64.Dense) {
func showLU(a *mat64.Dense) {
fmt.Printf("m:\n%v\n", M{m})
fmt.Printf("a: %v\n\n", mat64.Formatted(a, mat64.Prefix(" ")))
f := mat64.LU(m)
var lu mat64.LU
lu.Factorize(a)
fmt.Printf("l:\n%.5f\n", M{f.L()})
var l, u mat64.TriDense
fmt.Printf("u:\n%.5f\n", M{f.U()})
fmt.Println("p:", f.Pivot)
l.LFrom(&lu)
u.UFrom(&lu)
}
fmt.Printf("l: %.5f\n\n", mat64.Formatted(&l, mat64.Prefix(" ")))

fmt.Printf("u: %.5f\n\n", mat64.Formatted(&u, mat64.Prefix(" ")))
type M struct{ mat64.Matrix }
fmt.Println("p:", lu.Pivot(nil))

}</lang>
func (m M) Format(f fmt.State, c rune) { mat64.Format(m, 0, 0, f, c) }</lang>
{{out}}
{{out}}
Pivot format is a little different here.
Pivot format is a little different here. (But library solutions don't really meet task requirements anyway.)
<pre>
<pre>
a: ⎡1 3 5⎤
m:
⎢2 4 7⎥
⎡1 3 5⎤
⎣1 1 0⎦
⎢2 4 7⎥

⎣1 1 0⎦
l: ⎡ 1.00000 0.00000 0.00000⎤
l:
1.00000 0.00000 0.00000⎤
0.50000 1.00000 0.00000⎥
0.50000 1.00000 0.00000⎥
0.50000 -1.00000 1.00000⎦

⎣ 0.50000 -1.00000 1.00000⎦
u: ⎡ 2.00000 4.00000 7.00000⎤
u:
2.00000 4.00000 7.00000⎤
⎢ 0.00000 1.00000 1.50000⎥
0.00000 1.00000 1.50000⎥
0.00000 0.00000 -2.00000⎦

0.00000 0.00000 -2.00000⎦
p: [1 0 2]
p: [1 0 2]

m:
⎡11 9 24 2⎤
a: ⎡11 9 24 2⎤
⎢ 1 5 2 6⎥
⎢ 1 5 2 6⎥
⎢ 3 17 18 1⎥
⎢ 3 17 18 1⎥
⎣ 2 5 7 1⎦
⎣ 2 5 7 1⎦

l:
⎡1.00000 0.00000 0.00000 0.00000⎤
l: ⎡1.00000 0.00000 0.00000 0.00000⎤
⎢0.27273 1.00000 0.00000 0.00000⎥
⎢0.27273 1.00000 0.00000 0.00000⎥
⎢0.09091 0.28750 1.00000 0.00000⎥
⎢0.09091 0.28750 1.00000 0.00000⎥
⎣0.18182 0.23125 0.00360 1.00000⎦
⎣0.18182 0.23125 0.00360 1.00000⎦

u:
⎡11.00000 9.00000 24.00000 2.00000⎤
u: ⎡11.00000 9.00000 24.00000 2.00000⎤
⎢ 0.00000 14.54545 11.45455 0.45455⎥
⎢ 0.00000 14.54545 11.45455 0.45455⎥
⎢ 0.00000 0.00000 -3.47500 5.68750⎥
⎢ 0.00000 0.00000 -3.47500 5.68750⎥
⎣ 0.00000 0.00000 0.00000 0.51079⎦
⎣ 0.00000 0.00000 0.00000 0.51079⎦

p: [0 2 1 3]
p: [0 2 1 3]
</pre>
</pre>