/// <summary> /// Performs raising a matrix to a power /// </summary> /// <param name="A"></param> /// <param name="n"></param> /// <returns></returns> private FMatrix RaiseToPower(FMatrix A, BigInteger n) { if (n == 1) { return(A); } //decrement by 1 and multiply by itself if odd if (n % 2 == 1) { return(MultiplyMatrix(A, RaiseToPower(A, n - 1))); } //else raise power by 2 and multiply var B = RaiseToPower(A, n / 2); return(MultiplyMatrix(B, B)); }