aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2024-02-24 00:51:03 +0000
committerMark <53903062+andemark@users.noreply.github.com>2024-02-24 00:51:03 +0000
commit287beaf53f83c0718b90ac7b307bc335d69cfdcd (patch)
tree6851df8023a033803f64e6c7aa90d10f6f8f05c1
parent2cd039282886786a56848441be3edff519e70da0 (diff)
downloadperlweeklychallenge-club-287beaf53f83c0718b90ac7b307bc335d69cfdcd.tar.gz
perlweeklychallenge-club-287beaf53f83c0718b90ac7b307bc335d69cfdcd.tar.bz2
perlweeklychallenge-club-287beaf53f83c0718b90ac7b307bc335d69cfdcd.zip
ch-2.raku simplified
-rw-r--r--challenge-257/mark-anderson/raku/ch-2.raku185
1 files 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
}