Matrix exponentiation code.