LU decomposition: Difference between revisions

→‎{{header|zkl}}: added GSL solution
(→‎{{header|zkl}}: added GSL solution)
Line 3,208:
 
=={{header|zkl}}==
Using the GNU Scientific Library, which does the decomposition without returning the permutations:
<lang zkl>var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
fcn luTask(A){
A.LUDecompose(); // in place, contains L & U
L:=A.copy().lowerTriangle().setDiagonal(0,0,1);
U:=A.copy().upperTriangle();
return(L,U);
}
 
A:=GSL.Matrix(3,3).set(1,3,5, 2,4,7, 1,1,0); // example 1
L,U:=luTask(A);
println("L:\n",L.format(),"\nU:\n",U.format());
 
A:=GSL.Matrix(4,4).set(11.0, 9.0, 24.0, 2.0, // example 2
1.0, 5.0, 2.0, 6.0,
3.0, 17.0, 18.0, 1.0,
2.0, 5.0, 7.0, 1.0);
L,U:=luTask(A);
println("L:\n",L.format(),"\nU:\n",U.format());</lang>
{{out}}
<pre>
L:
1.00, 0.00, 0.00
0.50, 1.00, 0.00
0.50, -1.00, 1.00
U:
2.00, 4.00, 7.00
0.00, 1.00, 1.50
0.00, 0.00, -2.00
L:
1.00, 0.00, 0.00, 0.00
0.27, 1.00, 0.00, 0.00
0.09, 0.29, 1.00, 0.00
0.18, 0.23, 0.00, 1.00
U:
11.00, 9.00, 24.00, 2.00
0.00, 14.55, 11.45, 0.45
0.00, 0.00, -3.47, 5.69
0.00, 0.00, 0.00, 0.51
</pre>
Or, using lists:
{{trans|Common Lisp}}
{{trans|D}}
Anonymous user