LU decomposition: Difference between revisions

Content added Content deleted
(Updated D entry)
(Updated D entry)
Line 713: Line 713:


/// Creates the pivoting matrix for m.
/// Creates the pivoting matrix for m.
T[][] pivotize(T)(immutable T[][] m) /*pure*/ nothrow
T[][] pivotize(T)(immutable T[][] m) pure nothrow
in {
in {
assert(m.isSquare);
assert(m.isSquare);
Line 719: Line 719:
immutable n = m.length;
immutable n = m.length;
auto id = iota(n)
auto id = iota(n)
.map!(j=> n.iota.map!(i => cast(T)(i == j)).array)
.map!((in j) => n.iota.map!(i => cast(T)(i == j)).array)
.array;
.array;


Line 741: Line 741:
/// Decomposes a square matrix A by PA=LU and returns L, U and P.
/// Decomposes a square matrix A by PA=LU and returns L, U and P.
Tuple!(T[][],"L", T[][],"U", const T[][],"P")
Tuple!(T[][],"L", T[][],"U", const T[][],"P")
lu(T)(immutable T[][] A) /*pure*/ nothrow
lu(T)(immutable T[][] A) pure nothrow
in {
in {
assert(A.isSquare);
assert(A.isSquare);
Line 753: Line 753:
}
}


const P = A.pivotize!T;
immutable P = A.pivotize!T;
const A2 = matrixMul!T(P, A);
immutable A2 = matrixMul!T(P, A);


foreach (immutable j; 0 .. n) {
foreach (immutable j; 0 .. n) {
Line 785: Line 785:


auto f = "[%([%(%.1f, %)],\n %)]]\n\n".replicate(3);
auto f = "[%([%(%.1f, %)],\n %)]]\n\n".replicate(3);
foreach (m; [a, b])
foreach (immutable m; [a, b])
writefln(f, lu(m).tupleof);
writefln(f, lu(m).tupleof);
}</lang>
}</lang>