Reduced row echelon form: Difference between revisions

m
→‎Row operations, procedural code: house-keeping phase 2: tidy section #2
m (→‎{{header|Raku}}: house-keeping phase 1: split long entry into 3 sections, tidy up 1st section)
m (→‎Row operations, procedural code: house-keeping phase 2: tidy section #2)
Line 3,533:
[http://unapologetic.wordpress.com/2009/09/03/reduced-row-echelon-form/ reduced row echelon form]
 
<lang perl6>sub swap_rows scale-row ( @M, \scale, \r $r1, $r2 ) { @M[r] = $r1, $r2 ] = @M[r] $r2, $r1»×» scale ] };
sub scale_row shear-row ( @M, $\scale, $r \r1, \r2 ) { @M[$rr1] = @M[r1] »+» ( @M[$rr2] »*×» $scale ) };
sub shear_row reduce-row ( @M, $scale, $r1 \r, $r2 \c ) { @M[$r1] =scale-row @M[$r1].list »+» (, 1/@M[$r2r;c], »*» $scale )r };
sub reduce_row clear-column ( @M, $\r, $\c ) { scale_row(shear-row @M, 1/-@M[$r][$_;c], $_, r )for @M.keys.grep: * != r };
sub clear_column ( @M, $r, $c ) {
for @M.keys.grep( * != $r ) -> $row_num {
shear_row( @M, -1*@M[$row_num][$c], $row_num, $r );
}
}
 
my @M = (
Line 3,549 ⟶ 3,544:
);
 
my $column_countcolumn-count = +@( @M[0] );
my $current_colcol = 0;
 
for @M.keys -> $current_rowrow {
my $current_col = 0;
reduce-row( @M, $row, $col );
while all( @M».[$current_col] ) == 0 {
clear-column( @M, $row, $col );
$current_col++;
returnlast if ++$current_colcol == $column_countcolumn-count; # Matrix was all-zeros.
}
 
for @M.keys -> $current_row {
reduce_row( @M, $current_row, $current_col );
clear_column( @M, $current_row, $current_col );
$current_col++;
return if $current_col == $column_count;
}
 
say @($_)».fmt(' %4g') for @M;</lang>
 
=== Row operations, object-oriented code ===
2,392

edits