Perl6-Math-Matrix icon indicating copy to clipboard operation
Perl6-Math-Matrix copied to clipboard

List returned by .row has properties different from lists returned by other methods

Open tsalada opened this issue 4 years ago • 0 comments

I'm using a Math::Matrix object to represent a 3x3 magic square (all rows, columns and diagonals sum to 15). The list returned by magicSquare.row(r) responds incorrectly to the list reduction operator, [+], returning 3 (the number of elements for rows 0..2) instead of 15. The lists returned by magicSquare.column, magicSquare.diagonal and magicSquare.skew-diagonal all respond correctly to the list reduction operator, [+].

#!/usr/bin/env raku

raku load_Magic_Square.raku

use Math::Matrix;

Load Known Magic Square

my $magicSquare = Math::Matrix.new( [[2,7,6], [9,5,1], [4,3,8]]); say $magicSquare;

my @row = (); my $rowSum; my @col = (); my $colSum; my $magicTotal = 15;

say "";

Verify Rows

for 0..2 -> $r { @row = $magicSquare.row($r); say "Row $r: " ~ @row; $rowSum = [+] @row; say "Row $r Sum: $rowSum"; if ($rowSum == $magicTotal) { say "Verified Row $r"; } say ""; }

say "";

Verify Columns

for 0..2 -> $c { @col = $magicSquare.column($c); say "Col $c: " ~ @col; $colSum = [+] @col; say "Col $c Sum: $colSum"; if ($colSum == $magicTotal) { say "Verified Column $c"; } say ""; }

say "";

Verify Main Diagonal (Upper Left to Lower Right)

my @mDiagonal = $magicSquare.diagonal; my $mDiagonalSum = [+] @mDiagonal; if ($mDiagonalSum == $magicTotal) { say "Verified Main Diagonal"; }

say "";

Verify Skew Diagonal (Lower Left to Upper Right)

my @sDiagonal = $magicSquare.skew-diagonal; my $sDiagonalSum = [+] @sDiagonal; if ($sDiagonalSum == $magicTotal) { say "Verified Skew Diagonal"; }

tsalada avatar Jul 09 '21 21:07 tsalada