Strassen's algorithm: Difference between revisions
→{{header|Raku}}: minor updates + some comments
(added Raku programming solution) |
(→{{header|Raku}}: minor updates + some comments) |
||
Line 490:
use Math::Libgsl::Matrix;
use Math::Libgsl::BLAS;
my @M;
sub SQM (\in) { # create custom sq matrix from CSV
die "Not a ■" if (my \L = in.split(/\,/)).sqrt != (my \size = L.sqrt.Int);
Line 499:
M
}
sub infix:<⊗>(\x,\y) { # custom multiplication
my Math::Libgsl::Matrix \z .= new: x.size1, x.size2;
Line 505:
z
}
sub infix:<⊕>(\x,\y) { # custom addition
my Math::Libgsl::Matrix \z .= new: x.size1, x.size2;
Line 511:
z
}
sub infix:<⊖>(\x,\y) { # custom subtraction
my Math::Libgsl::Matrix \z .= new: x.size1, x.size2;
Line 517:
z
}
sub Strassen($A, $B) {
{ return $A ⊗ $B } if (my \n = $A.size1) == 1;
my Math::Libgsl::Matrix ($A11,$A12,$A21,$A22,$B11,$B12,$B21,$B22);
my Math::Libgsl::Matrix ($P1,$P2,$P3,$P4,$P5,$P6,$P7);
my Math::Libgsl::Matrix ($C11,$C12,$C21,$C22);▼
my Math::Libgsl::Matrix::View ($mv1,$mv2,$mv3,$mv4,$mv5,$mv6,$mv7,$mv8);
($mv1,$mv2,$mv3,$mv4,$mv5,$mv6,$mv7,$mv8)»
my \half = n div 2; # dimension of quarter submatrices
$A11 = $mv1.submatrix($A, 0,0, n div 2,n div 2); ▼
$A12 = $mv2.submatrix($A, 0,n div 2, n div 2,n div 2); ▼
$A21 = $mv3.submatrix($A, n div 2,0, n div 2,n div 2); ▼
$A22 = $mv4.submatrix($A, n div 2,n div 2, n div 2,n div 2); ▼
$B11 = $mv5.submatrix($B, 0,0, n div 2,n div 2); ▼
$B12 = $mv6.submatrix($B, 0,n div 2, n div 2,n div 2); ▼
$B21 = $mv7.submatrix($B, n div 2,0, n div 2,n div 2); ▼
$A12 = $mv2.submatrix($A, 0,half, half,half); # create quarter views
$P1 = Strassen($A12 ⊖ $A22, $B21 ⊕ $B22);
$P2 = Strassen($A11 ⊕ $A22, $B11 ⊕ $B22);
Line 544 ⟶ 545:
$P6 = Strassen($A22, $B21 ⊖ $B11);
$P7 = Strassen($A21 ⊕ $A22, $B11 );
my Math::Libgsl::Matrix $C .= new: n, n; # Build C from▼
($mvC11,$mvC12,$mvC21,$mvC22)».=new ; # C21 C22
given $
given $mvC12.submatrix($
given $mvC21.submatrix($
given $mvC22.submatrix($
▲ my Math::Libgsl::Matrix $C .= new: n, n;
$C
}
for $=pod[0].contents { next if /^\n$/ ; @M.append: SQM $_ }
for @M.rotor(2) {
my $product = @_[0]
# $product.get-row($_)».round(1).fmt('%2d').put for ^$product.size1;
say "Regular multiply:";
$product.get-row($_)».fmt('%.10g').put for ^$product.size1;
$product = Strassen @_[0], @_[1];
say "Strassen multiply:";
$product.get-row($_)».fmt('%.10g').put for ^$product.size1;
}
=begin code
1,2,3,4
Line 611 ⟶ 608:
9 10 11 12
13 14 15 16</pre>
=={{header|Wren}}==
|