Right, but couple of corrections/nitpicks, if I may:
if( $n < 0 ) {
return matrix_power( inv( $M ), -$n );
} elsif ( $n == 0 ) {
return identity( $M );
} elsif ( $n == 1 ) {
return copy( $M ); # (1)
#
# ...
As to (1), I'm not sure about numpy, whether its function in question returns copy or clone in this case, or maybe they don't think it's important. But then, whole can of worms is there, e.g. data type of return piddle, what to do for singular matrices and negative exponent, and perhaps some others.