How to resolve the algorithm Matrix-exponentiation operator step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Matrix-exponentiation operator step by step in the Raku programming language
Table of Contents
Problem Statement
Most programming languages have a built-in implementation of exponentiation for integers and reals only.
Demonstrate how to implement matrix exponentiation as an operator.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Matrix-exponentiation operator step by step in the Raku programming language
Source code in the raku programming language
subset SqMat of Array where { .elems == all(.[]».elems) }
multi infix:<*>(SqMat $a, SqMat $b) {[
for ^$a -> $r {[
for ^$b[0] -> $c {
[+] ($a[$r][] Z* $b[].map: *[$c])
}
]}
]}
multi infix:<**> (SqMat $m, Int $n is copy where { $_ >= 0 }) {
my $tmp = $m;
my $out = [for ^$m -> $i { [ for ^$m -> $j { +($i == $j) } ] } ];
loop {
$out = $out * $tmp if $n +& 1;
last unless $n +>= 1;
$tmp = $tmp * $tmp;
}
$out;
}
multi show (SqMat $m) {
my $size = $m.map( *.list».chars ).flat.max;
say .fmt("%{$size}s", ' ') for $m.list;
}
my @m = [1, 2, 0],
[0, 3, 1],
[1, 0, 0];
for 0 .. 10 -> $order {
say "### Order $order";
show @m ** $order;
}
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the BASIC programming language
You may also check:How to resolve the algorithm Benford's law step by step in the Fortran programming language
You may also check:How to resolve the algorithm Two's complement step by step in the Raku programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm File size step by step in the Elixir programming language