LU decomposition: Difference between revisions
Content added Content deleted
(→Flat representation: simplify) |
(→{{header|Go}}: add gonum/matrix version) |
||
Line 1,218: | Line 1,218: | ||
Output is same as from 2D solution. |
Output is same as from 2D solution. |
||
===Library=== |
===Library gonum/matrix=== |
||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"github.com/gonum/matrix/mat64" |
|||
) |
|||
func main() { |
|||
showLU(mat64.NewDense(3, 3, []float64{ |
|||
1, 3, 5, |
|||
2, 4, 7, |
|||
1, 1, 0, |
|||
})) |
|||
showLU(mat64.NewDense(4, 4, []float64{ |
|||
11, 9, 24, 2, |
|||
1, 5, 2, 6, |
|||
3, 17, 18, 1, |
|||
2, 5, 7, 1, |
|||
})) |
|||
} |
|||
func showLU(m *mat64.Dense) { |
|||
fmt.Printf("m:\n%v\n", M{m}) |
|||
f := mat64.LU(m) |
|||
fmt.Printf("l:\n%.5f\n", M{f.L()}) |
|||
fmt.Printf("u:\n%.5f\n", M{f.U()}) |
|||
fmt.Println("p:", f.Pivot) |
|||
} |
|||
type M struct{ mat64.Matrix } |
|||
func (m M) Format(f fmt.State, c rune) { mat64.Format(m, 0, 0, f, c) }</lang> |
|||
{{out}} |
|||
Pivot format is a little different here. |
|||
<pre> |
|||
m: |
|||
⎡1 3 5⎤ |
|||
⎢2 4 7⎥ |
|||
⎣1 1 0⎦ |
|||
l: |
|||
⎡ 1.00000 0.00000 0.00000⎤ |
|||
⎢ 0.50000 1.00000 0.00000⎥ |
|||
⎣ 0.50000 -1.00000 1.00000⎦ |
|||
u: |
|||
⎡ 2.00000 4.00000 7.00000⎤ |
|||
⎢ 0.00000 1.00000 1.50000⎥ |
|||
⎣ 0.00000 0.00000 -2.00000⎦ |
|||
p: [1 0 2] |
|||
m: |
|||
⎡11 9 24 2⎤ |
|||
⎢ 1 5 2 6⎥ |
|||
⎢ 3 17 18 1⎥ |
|||
⎣ 2 5 7 1⎦ |
|||
l: |
|||
⎡1.00000 0.00000 0.00000 0.00000⎤ |
|||
⎢0.27273 1.00000 0.00000 0.00000⎥ |
|||
⎢0.09091 0.28750 1.00000 0.00000⎥ |
|||
⎣0.18182 0.23125 0.00360 1.00000⎦ |
|||
u: |
|||
⎡11.00000 9.00000 24.00000 2.00000⎤ |
|||
⎢ 0.00000 14.54545 11.45455 0.45455⎥ |
|||
⎢ 0.00000 0.00000 -3.47500 5.68750⎥ |
|||
⎣ 0.00000 0.00000 0.00000 0.51079⎦ |
|||
p: [0 2 1 3] |
|||
</pre> |
|||
===Library go.matrix=== |
|||
<lang go>package main |
<lang go>package main |
||