Reduced row echelon form: Difference between revisions

m
→‎Row operations, object-oriented code: house-keeping wrap-up: tidy code, add output
(Reduced row echelon form in Yabasic)
m (→‎Row operations, object-oriented code: house-keeping wrap-up: tidy code, add output)
Line 3,613:
 
=== Row operations, object-oriented code ===
And theThe same code as previous section, recast into OO. Also, scale and shear are recast as unscale and unshear, which fit the problem better.
<lang perl6>class Matrix is Array {
method unscale_row unscale-row ( @M: $\scale, $\row ) { @M[row] = @M[row] »/» scale }
method unshear-row ( @M: \scale, \r1, \r2 ) { @M[$rowr1] = @M[$rowr1] »/-» @M[r2] »×» $scale; }
method reduce-row ( @M: \row, \col ) { @M.unscale-row( @M[row;col], row ) }
}
method unshear_rowclear-column ( @M: $scale \row, \col ) { @M.unshear-row( @M[$r1_;col], $r2_, row ) {for @M.keys.grep: * != row }
@M[$r1] = @M[$r1] »-» ( @M[$r2] »*» $scale );
}
method reduce_row ( @M: $row, $col ) {
@M.unscale_row( @M[$row][$col], $row );
}
method clear_column ( @M: $row, $col ) {
for @M.keys.grep( * != $row ) -> $scanning_row {
@M.unshear_row( @M[$scanning_row][$col], $scanning_row, $row );
}
}
method reduced_row_echelon_form ( @M: ) {
my $column_count = +@( @M[0] );
 
method reduced_row_echelon_formreduced-row-echelon-form ( @M: ) {
my $current_col = 0;
#my Skip$column-count past= all-zero columns.@M[0];
whilemy all( @M».[$current_col] )col == 0 {;
for @M.keys -> $current_col++;row {
return@M.reduce-row( if $current_colrow, ==$col $column_count); # Matrix was all-zeros.
@M.unscale_rowclear-column( @M[$row][$col], $rowcol );
}
return if ++$current_colcol == $column_countcolumn-count;
 
for @M.keys -> $current_row {
@M.reduce_row( $current_row, $current_col );
@M.clear_column( $current_row, $current_col );
$current_col++;
return if $current_col == $column_count;
}
}
Line 3,654 ⟶ 3,637:
);
 
$M.reduced-row-echelon-form;
$M.reduced_row_echelon_form;
say @($_)».fmt(' %4g') for @($M);</lang>
 
{{out}}
say @($_)».fmt(' %4g') for @($M);</lang>
<pre>[ 1 0 0 -8]
[ 0 1 0 1]
[ 0 0 1 -2]</pre>
 
=={{header|REXX}}==
2,392

edits