From 287beaf53f83c0718b90ac7b307bc335d69cfdcd Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Sat, 24 Feb 2024 00:51:03 +0000 Subject: ch-2.raku simplified --- challenge-257/mark-anderson/raku/ch-2.raku | 185 +++++++++++++++++------------ 1 file changed, 111 insertions(+), 74 deletions(-) diff --git a/challenge-257/mark-anderson/raku/ch-2.raku b/challenge-257/mark-anderson/raku/ch-2.raku index f61ebc906a..23ba966e1c 100644 --- a/challenge-257/mark-anderson/raku/ch-2.raku +++ b/challenge-257/mark-anderson/raku/ch-2.raku @@ -1,88 +1,125 @@ #!/usr/bin/env raku +use v6.e.PREVIEW; # for snip use Test; -ok reduced-row-echelon([1, 0, 0, 1], - [0, 1, 0, 2], - [0, 0, 1, 3]); - -nok reduced-row-echelon([1, 1, 0], - [0, 1, 0], - [0, 0, 0]); - -ok reduced-row-echelon([0, 1,-2, 0, 1], - [0, 0, 0, 1, 3], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0]); - -ok reduced-row-echelon([1, 0, 0, 4], - [0, 1, 0, 7], - [0, 0, 1,-1]); - -nok reduced-row-echelon([0, 1,-2, 0, 1], - [0, 0, 0, 0, 0], - [0, 0, 0, 1, 3], - [0, 0, 0, 0, 0]); - -nok reduced-row-echelon([0, 1, 0], - [1, 0, 0], - [0, 0, 0]); - -nok reduced-row-echelon([4, 0, 0, 0], - [0, 1, 0, 7], - [0, 0, 1,-1]); - -nok reduced-row-echelon([1, 0, 0, 0], - [0, 1, 0, 3], - [0, 0, 1,-3], - [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) +ok RREF([ + [1, 0, 0, 1], + [0, 1, 0, 2], + [0, 0, 1, 3] + ]); + +nok RREF([ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] + ]); + +ok RREF([ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ]); + +ok RREF([ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] + ]); + +nok RREF([ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] + ]); + +nok RREF([ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] + ]); + +nok RREF([ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] + ]); + +nok RREF([ + [1, 0, 0, 0], + [0, 1, 0, 3], + [0, 0, 1,-3], + [0, 0, 0, 1], + [0, 0, 0, 0] + ]); + +nok RREF([ + [1, 0, 0, 0], + [0, 1, 0, 1], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 0, 0, 0] + ]); + +ok RREF([ + [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 RREF([ + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0] + ]); + +ok RREF([ + [0, 0, 0], + ]); + +ok RREF([ + [0, 1, 0], + ]); + +nok RREF([ + [5], + ]); + +multi RREF(@m where all(.head) == 0) { - # the first non-zero number in a row is the pivot - my @pivots = @m>>.first(*.so, :kv); + all @m[1..*].map(*.all == 0) +} + +multi RREF(@m) +{ + # group rows according to whether they are all-zero or not + @m .= snip(|(*.any, *.any.not) xx *); - # the first row that is all zeroes - my $k = @pivots.first(*.not, :k); + # at least one all-zero row occurs in the middle + return False if @m.elems > 2; - # rows with all zeroes are grouped at the bottom - with $k - { - return False unless all(@pivots[$k..*]) eqv Any; - @pivots = @pivots[^$k]; - return True unless @pivots - } + # remove any all-zero rows grouped at the bottom + @m = @m[0;*]; - my @cols = @pivots>>[0]; - @pivots = @pivots>>[1]; + # the first non-zero number in a row is the pivot. store the indices in @cols + my @cols = @m>>.first(*.any, :k); - # pivots go from top-left to bottom-right + # the pivots go from top-left to bottom-right return False unless [<] @cols; - # remove zero rows and non-pivot columns - @m = @m[^@pivots;@cols].batch(@cols.elems)>>.Array; - # @m should be an identity matrix at this point if it is RREF + # remove any non-pivot columns + @m = @m[*;@cols].batch(@cols.elems)>>.Array; + + # @m will be an identity matrix now if it was in reduced row echelon form + # 🤓 # all pivots are ones - return False unless all(@m.keys.map({ |@m[$_].splice($_,1) })) == 1; + return False unless all(@m.keys.map({ |@m[$_].splice($_,1) })) == 1; - # everything else is zeroes - return all(@m[*;*]) == 0 + # everything else is a zero + return all(@m[*;*]) == 0 } -- cgit