LU decomposition: Difference between revisions

Improved D code
(Updated D code)
(Improved D code)
Line 589:
std.array, std.conv;
 
bool isRectangular(T)(in T[][] Mm) pure nothrow {
//return !canFind!((r){ return r.length != Mm[0].length; })(Mm);
foreach (row; Mm)
if (row.length != Mm[0].length)
return false;
return true;
}
 
stringbool prettyPrintisSquare(T)(in T[][] Mm) pure nothrow {
return "["isRectangular(m) ~&& array(map!text(M))m[0].join(",\nlength ")== ~ "]"m.length;
}
 
string prettyPrint(T)(in T[][] m) {
return "[" ~ array(map!text(m)).join(",\n ") ~ "]";
}
 
Line 622 ⟶ 626:
in {
assert(n >= 0);
} out(result) {
assert(isSquare(result));
} body {
auto m = new typeof(return)(n, n);
Line 634 ⟶ 640:
T[][] pivotize(T)(in T[][] m) pure nothrow
in {
// is squareassert(isSquare(m));
assert(isRectangular(m) && m[0].length == m.length);
} body {
immutable int n = m.length;
auto P = identityMatrix!T(n);
 
foreach (i; 0 .. n) {
T max = m[i][i];
intauto row = i;
foreach (j; i .. n)
if (m[j][i] > max) {
Line 659 ⟶ 664:
lu(T)(in T[][] A) pure nothrow
in {
// is squareassert(isSquare(A));
assert(isRectangular(A) && A[0].length == A.length);
} body {
immutable int n = A.length;
auto L = new T[][](n, n);
auto U = new T[][](n, n);
Line 669 ⟶ 673:
U[i][0 .. i] = 0;
}
 
const auto P = pivotize!T(A);
auto/*immutable*/ A2const P = matrixMulpivotize!T(P, A);
/*immutable*/ const auto PA2 = pivotizematrixMul!T(P, A);
 
foreach (j; 0 .. n) {
Anonymous user