Anonymous user
LU decomposition: Difference between revisions
Udated D entry
m (→{{header|REXX}}: updated the '''output'''s with new version (trailing zeroes suppressed). -- ~~~~) |
(Udated D entry) |
||
Line 587:
{{trans|Common Lisp}}
<lang d>import std.stdio, std.algorithm, std.typecons, std.numeric,
std.array, std.conv, std.string, std.range;
bool isRectangular(T)(in T[][] m) pure /*nothrow*/ {
Line 598:
}
▲ in {
assert(A[0].length == B.length);
▲ A[0].length == B.length);
auto result = new T[][](A.length, B[0].length);
▲ } body {
foreach (j; 0 ..
foreach (
foreach (i, ai;
result[i][j] = dotProduct(ai, aux);▼
}▼
return result;▼
}▼
assert(n >= 0);▼
foreach (i, row; m) {▼
row[] = 0;▼
row[i] = 1;▼
return m;▼
}
}
/// Creates the pivoting matrix for m.
.map!(j=> iota(n).map!(i => cast(T)(i == j))().array())()
// immutable row =
T
auto row =
if
swap(P[i], P[row]);▼
}
}
/// Decomposes a square matrix A by PA=LU and returns L, U and P.
Tuple!(T[][],"L", T[][],"U", const T[][],"P")
lu(T)(immutable T[][] A) /*pure nothrow*/
foreach (i; j .. n) {▼
T s2 = 0;▼
s2 += U[k][j] * L[i][k];▼
}
▲ }
▲ }
}
void main() {
immutable a = [[1.0, 3, 5],
[2.0, 4, 7],
[1.0, 1, 0]];
immutable b = [[11.0, 9, 24, 2],
foreach
writefln(f,
}</lang>
{{out}}
<pre>[[1.0, 0.0, 0.0],
[0.5, 1.0, 0.0],
[0.5, -1.0, 1.0]]
[[2.0, 4.0, 7.0],
[0.0, 1.0, 1.5],
[0.0, 0.0, -2.0]]
[[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.0, 1.0]]
[[1.0, 0.0, 0.0, 0.0],
[0.
[0.
[0.
[[11.0, 9.0, 24.0, 2.0],
[0.0, 14.
[0.0, 0.0, -3.
[0.0, 0.0, 0.0, 0.
[[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0]]</pre>
=={{header|Go}}==
|