diff options
| -rw-r--r-- | challenge-257/mark-anderson/raku/ch-2.raku | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index a6f2d34b0c..5c4c4cf95d 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -37,12 +37,23 @@ nok reduced-row-echelon([1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]); +nok reduced-row-echelon([1, 0, 0, 0], + [0, 1, 0, 1], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 0, 0, 0]); + ok reduced-row-echelon([1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]); +ok reduced-row-echelon([0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0]); + sub reduced-row-echelon(+@m) { # the first non-zero number in a row is the pivot @@ -51,25 +62,24 @@ sub reduced-row-echelon(+@m) # the first row that is all zeroes my $k = @pivots.first(*.not, :k); - # all 0 rows are grouped at the bottom - if $k + # rows with all zeroes are grouped at the bottom + with $k { return False unless all(@pivots[$k..*]) eqv Any; - @pivots = @pivots[^$k] + @pivots = @pivots[^$k]; + return True unless @pivots } - my @keys = @pivots>>[0]; + my @cols = @pivots>>[0]; @pivots = @pivots>>[1]; - # all pivots == 1 - return False unless all(@pivots) == 1; - # pivots go from top-left to bottom-right - return False unless [<] @keys; + return False unless [<] @cols; + + # remove extraneous rows and columns + @m = @m[^@pivots;@cols].batch(@cols.elems); - # pivot columns are all 0 (except for the 1) - return all (([Z] @m[^@pivots])[@keys]).map({ - all .sum == 1, - all(.Bag.keys) == 0|1 - }) + # @m should be an identity matrix at this point + # so check rows for all zeroes and a 1 + return all @m.map({ all(.sum == 1, all(.Bag.keys) == 0|1) }) } |
