LU decomposition: Difference between revisions

Content added Content deleted
m (→‎{{header|Tcl}}: Smarten up)
(Simplified D code)
Line 265:
#2A((1 0 0 0) (0 0 1 0) (0 1 0 0) (0 0 0 1))</lang>
=={{header|D}}==
//Using plussome functions from Matrix multiplication task.
{{trans|Common Lisp}}
<lang d>import std.stdio, std.algorithm, std.typecons;
// plus functions from Matrix multiplication task
 
/// Creates a nxn identity matrix.
Line 280 ⟶ 281:
return m;
}
 
/// Swap two rows l and k of a 2D array matrix m.
pure void swapRows(T)(ref T[][] m, const int l, const int k) {
swap(m[l], m[k]);
}
 
/// Creates the pivoting matrix for m.
Line 298 ⟶ 294:
T max = m[j][j];
int row = j;
foreach (i; j .. n) {
if (m[i][j] > max) {
max = m[i][j];
row = i;
}
}
if (j != row)
swapRowsswap(P, [j], P[row]);
}
 
Line 334 ⟶ 329:
s1 += U[k][j] * L[i][k];
U[i][j] = A2[i][j] - s1;
 
}
foreach (i; j .. n) {
Line 346 ⟶ 340:
return tuple(L, U, P);
}
 
 
void main() {
enum double[][] a = [[1, 3, 5], [2, 4, 7], [1, 1, 0]];
autoforeach lua =(part; lu(a);.tupleof)
writeln(prettyPrint(lua[0]part), "\n\n",);
writeln();
prettyPrint(lua[1]), "\n\n",
prettyPrint(lua[2]), "\n\n");
 
enum double[][] b = [[11, 9, 24, 2], [1, 5, 2, 6],
[3, 17, 18, 1], [2, 5, 7, 1]];
autoforeach lub =(part; lu(b);.tupleof)
writeln(prettyPrint(lub[0]part), "\n\n",);
prettyPrint(lub[1]), "\n\n",
prettyPrint(lub[2]));
}</lang>
Output: