LU decomposition: Difference between revisions
Content added Content deleted
m (J: whitespace changes to make block structure more apparent. Make certain assignments global scope.) |
(→{{header|Go}}: added library based solution) |
||
Line 848: | Line 848: | ||
p.print("p") |
p.print("p") |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
a: |
a: |
||
Line 1,023: | Line 1,023: | ||
p.print("p") |
p.print("p") |
||
}</lang> |
}</lang> |
||
Output is same as from 2D solution. |
|||
===Library=== |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
mat "github.com/skelterjohn/go.matrix" |
|||
) |
|||
func main() { |
|||
showLU(mat.MakeDenseMatrixStacked([][]float64{ |
|||
{1, 3, 5}, |
|||
{2, 4, 7}, |
|||
{1, 1, 0}})) |
|||
showLU(mat.MakeDenseMatrixStacked([][]float64{ |
|||
{11, 9, 24, 2}, |
|||
{1, 5, 2, 6}, |
|||
{3, 17, 18, 1}, |
|||
{2, 5, 7, 1}})) |
|||
} |
|||
func showLU(a *mat.DenseMatrix) { |
|||
fmt.Printf("\na:\n%v\n", a) |
|||
l, u, p := a.LU() |
|||
fmt.Printf("l:\n%v\n", l) |
|||
fmt.Printf("u:\n%v\n", u) |
|||
fmt.Printf("p:\n%v\n", p) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
a: |
|||
{1, 3, 5, |
|||
2, 4, 7, |
|||
1, 1, 0} |
|||
l: |
|||
{ 1, 0, 0, |
|||
0.5, 1, 0, |
|||
0.5, -1, 1} |
|||
u: |
|||
{ 2, 4, 7, |
|||
0, 1, 1.5, |
|||
0, 0, -2} |
|||
p: |
|||
{0, 1, 0, |
|||
1, 0, 0, |
|||
0, 0, 1} |
|||
a: |
|||
{11, 9, 24, 2, |
|||
1, 5, 2, 6, |
|||
3, 17, 18, 1, |
|||
2, 5, 7, 1} |
|||
l: |
|||
{ 1, 0, 0, 0, |
|||
0.272727, 1, 0, 0, |
|||
0.090909, 0.2875, 1, 0, |
|||
0.181818, 0.23125, 0.003597, 1} |
|||
u: |
|||
{ 11, 9, 24, 2, |
|||
0, 14.545455, 11.454545, 0.454545, |
|||
0, 0, -3.475, 5.6875, |
|||
0, 0, 0, 0.510791} |
|||
p: |
|||
{1, 0, 0, 0, |
|||
0, 0, 1, 0, |
|||
0, 1, 0, 0, |
|||
0, 0, 0, 1} |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |