LU decomposition: Difference between revisions

Content added Content deleted
m (Fortran 90 rather than FORTRAN 77 highlighting)
(Updated D entry)
Line 686: Line 686:
std.array, std.conv, std.string, std.range;
std.array, std.conv, std.string, std.range;


bool isRectangular(T)(in T[][] m) /*pure nothrow*/ {
bool isRectangular(T)(in T[][] m) pure nothrow {
return m.all!(r => r.length == m[0].length);
return m.all!(r => r.length == m[0].length);
}
}


bool isSquare(T)(in T[][] m) /*pure nothrow*/ {
bool isSquare(T)(in T[][] m) pure nothrow {
return isRectangular(m) && m[0].length == m.length;
return m.isRectangular && m[0].length == m.length;
}
}


T[][] matrixMul(T)(in T[][] A, in T[][] B) /*pure nothrow*/
T[][] matrixMul(T)(in T[][] A, in T[][] B) pure nothrow
in {
in {
assert(A.isRectangular && B.isRectangular &&
assert(A.isRectangular && B.isRectangular &&
Line 715: Line 715:
T[][] pivotize(T)(immutable T[][] m) /*pure nothrow*/
T[][] pivotize(T)(immutable T[][] m) /*pure nothrow*/
in {
in {
assert(isSquare(m));
assert(m.isSquare);
} body {
} body {
immutable n = m.length;
immutable n = m.length;
Line 723: Line 723:


foreach (immutable i; 0 .. n) {
foreach (immutable i; 0 .. n) {
// immutable row = iota(i, n).max!(j => m[j][i])();
// immutable row = iota(i, n).reduce!(max!(j => m[j][i]));
T maxm = m[i][i];
T maxm = m[i][i];
size_t row = i;
size_t row = i;
Line 743: Line 743:
lu(T)(immutable T[][] A) /*pure nothrow*/
lu(T)(immutable T[][] A) /*pure nothrow*/
in {
in {
assert(isSquare(A));
assert(A.isSquare);
} body {
} body {
immutable n = A.length;
immutable n = A.length;
Line 753: Line 753:
}
}


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


Line 784: Line 784:
[2.0, 5, 7, 1]];
[2.0, 5, 7, 1]];


//auto f = "[%([%(%.1f, %)],\n %)]]\n\n".replicate(3);
auto f = std.array.replicate("[%([%(%.1f, %)],\n %)]]\n\n", 3);
auto f = std.array.replicate("[%([%(%.1f, %)],\n %)]]\n\n", 3);
foreach (m; [a, b])
foreach (m; [a, b])