Okay, then, implementing a full matrix_power() with PDL using "same algorithm as python numpy" (https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.linalg.matrix_power.html), which says "the power is computed by repeated matrix squarings and matrix multiplications" -- which is then the matrix version of https://en.wikipedia.org/wiki/Exponentiation_by_squaring.
#!/usr/bin/perl # [id://1229234] use warnings; use strict; use PDL; sub matrix_power { my ($M,$n) = @_; if($n<0) { return matrix_power(1/$M, -$n); } elsif ($n==0) { return 1; } elsif ($n==1) { return $M; } elsif (0 == $n % 2) { return matrix_power($M x $M, $n/2); } else { return $M x matrix_power($M x $M, ($n-1)/2); } } my $x = pdl([0,1], [2,3]); for(0 .. 3) { print "M ** $_ = " . matrix_power( $x, $_ ) . "\n"; }
In reply to Re^4: In PDL, how to raise a matrix $m to a power $v
by pryrt
in thread In PDL, how to raise a matrix $m to a power $v
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |