public static IComplexPolynomial Pow(IComplexPolynomial poly, int exponent) { if (exponent < 0) { throw new NotImplementedException("Raising a polynomial to a negative exponent not supported. Build this functionality if it is needed."); } else if (exponent == 0) { return(new ComplexPolynomial(new ComplexTerm[] { new ComplexTerm(1, 0) })); } else if (exponent == 1) { return(poly.Clone()); } else if (exponent == 2) { return(Square(poly)); } IComplexPolynomial total = ComplexPolynomial.Square(poly); int counter = exponent - 2; while (counter != 0) { total = ComplexPolynomial.Multiply(total, poly); counter -= 1; } return(total); }
/* * public static IPoly ExponentiateMod(IPoly startPoly, Complex s2, IPoly f, Complex p) * { * IPoly result = ComplexPoly.One; * if (s2 == 0) { return result; } * * IPoly A = startPoly.Clone(); * * byte[] byteArray = s2.ToByteArray(); * bool[] bitArray = new BitArray(byteArray).Cast<bool>().ToArray(); * * // Remove trailing zeros ? * if (bitArray[0] == true) * { * result = startPoly; * } * * int i = 1; * int t = bitArray.Length; * while (i < t) * { * A = ComplexPoly.ModMod(ComplexPoly.Square(A), f, p); * if (bitArray[i] == true) * { * result = ComplexPoly.ModMod(ComplexPoly.Multiply(A, result), f, p); * } * i++; * } * * return result; * } */ #endregion public static IComplexPolynomial ModPow(IComplexPolynomial poly, Complex exponent, IComplexPolynomial modulus) { //if (exponent.Sign() == -1) //{ // throw new NotImplementedException("Raising a polynomial to a negative exponent not supported. Build this functionality if it is needed."); //} if (exponent == Complex.Zero) { return(ComplexPolynomial.One); } else if (exponent == Complex.One) { return(poly.Clone()); } else if (exponent == 2) { return(ComplexPolynomial.Square(poly)); } IComplexPolynomial total = ComplexPolynomial.Square(poly); Complex counter = exponent - 2; while (counter != 0) { total = Multiply(poly, total); if (total.CompareTo(modulus) < 0) { total = ComplexPolynomial.Mod(total, modulus); } counter -= 1; } return(total); }