Reduced row echelon form: Difference between revisions

Shorter D code
(→‎{{header|Euphoria}}: Euphoria example added)
(Shorter D code)
Line 700:
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.array, std.conv;
 
pure nothrow boolvoid isRectangulartoReducedRowEchelonForm(T)(in T[][] M) pure {
foreachconst (row;nrows = M).length;
if (row.length !=nrows) M[0].length)return;
const ncols = return falseM[0].length;
returnint truelead;
foreach (r; 0 .. rowCountnrows) {
}
if (ncols <= lead) return;
 
pure void toReducedRowEchelonForm(T)(T[][] M)
in {
assert(isRectangular(M));
}
body {
if (!M.length)
return;
immutable int rowCount = M.length;
immutable int columnCount = M[0].length;
int lead = 0;
foreach (r; 0 .. rowCount) {
if (columnCount <= lead)
return;
int i = r;
while (M[i][lead] == 0) {
iif (nrows == ++;i) {
if (rowCount == i) {
i = r;
leadif (ncols == ++lead) return;
if (columnCount == lead)
return;
}
}
swap(M[i], M[r]);
M[r][] /= M[r][lead];
foreach (j; 0 .. rowCountnrows)
if (j != r)
M[j][] -= M[r][] * M[j][lead];
Line 742 ⟶ 725:
 
void main() {
auto mtxA = [[ 1, 2, -1, -4],
[ 2, 3, -1, -11],
[-2, 0, -3, 22]];
toReducedRowEchelonForm(mtxA);
writeln("[", array(map!text(A)).join("\n "), "]");
foreach (row; mtx)
writeln(row);
}</lang>
Output:
<pre>[[1, 0, 0, -8]
[0, 1, 0, 1]
[0, 0, 1, -2]]</pre>
 
=={{header|Euphoria}}==
Anonymous user