public static Matrix Power(Matrix m, int pow) // Power matrix to exponent
{
if (pow == 0)
{
return(IdentityMatrix(m.rows, m.cols));
}
if (pow == 1)
{
return(m.Duplicate());
}
if (pow == -1)
{
return(m.Invert());
}
Matrix x;
if (pow < 0)
{
x = m.Invert(); pow *= -1;
}
else
{
x = m.Duplicate();
}
Matrix ret = IdentityMatrix(m.rows, m.cols);
while (pow != 0)
{
if ((pow & 1) == 1)
{
ret *= x;
}
x *= x;
pow >>= 1;
}
return(ret);
}