Gaussian elimination: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: sync up 'rref') |
|||
Line 4,849: | Line 4,849: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2018.03}} |
|||
Gaussian elimination results in a matrix in row echelon form. Gaussian elimination with back-substitution (also known as Gauss-Jordan elimination) results in a matrix in reduced row echelon form. That being the case, we can reuse much of the code from the [[Reduced row echelon form]] task. Raku stores and does calculations on decimal numbers within its limit of precision using Rational numbers by default, meaning the calculations are exact. |
Gaussian elimination results in a matrix in row echelon form. Gaussian elimination with back-substitution (also known as Gauss-Jordan elimination) results in a matrix in reduced row echelon form. That being the case, we can reuse much of the code from the [[Reduced row echelon form]] task. Raku stores and does calculations on decimal numbers within its limit of precision using Rational numbers by default, meaning the calculations are exact. |
||
Line 4,857: | Line 4,857: | ||
} |
} |
||
# reduced row echelon form |
# reduced row echelon form |
||
sub rref (@m) { |
sub rref (@m) { |
||
⚫ | |||
return unless @m; |
|||
⚫ | |||
for ^$rows -> $r { |
for ^$rows -> $r { |
||
Line 4,871: | Line 4,870: | ||
} |
} |
||
@m[$i, $r] = @m[$r, $i] if $r != $i; |
@m[$i, $r] = @m[$r, $i] if $r != $i; |
||
@m[$r] »/=» $ = @m[$r;$lead]; |
|||
@m[$r] »/=» $lv; |
|||
for ^$rows -> $n { |
for ^$rows -> $n { |
||
next if $n == $r; |
next if $n == $r; |
||
@m[$n] »-=» @m[$r] » |
@m[$n] »-=» @m[$r] »×» (@m[$n;$lead] // 0); |
||
} |
} |
||
++$lead; |
++$lead; |
||
Line 4,884: | Line 4,882: | ||
sub rat-or-int ($num) { |
sub rat-or-int ($num) { |
||
return $num unless $num ~~ Rat; |
return $num unless $num ~~ Rat; |
||
return $num.narrow if $num.narrow |
return $num.narrow if $num.narrow ~~ Int; |
||
$num.nude.join: '/'; |
$num.nude.join: '/'; |
||
} |
} |