public static IComplexPolynomial GCD(IComplexPolynomial left, IComplexPolynomial right) { IComplexPolynomial a = left.Clone(); IComplexPolynomial b = right.Clone(); if (b.Degree > a.Degree) { IComplexPolynomial swap = b; b = a; a = swap; } while (!(b.Degree == 0 || (b.Terms[0].CoEfficient.Real == 1 && b.Terms[0].CoEfficient.Imaginary == 0))) { IComplexPolynomial temp = a.Clone(); a = b.Clone(); b = ComplexPolynomial.Mod(temp, b); } if (a.Degree == 0) { return(ComplexPolynomial.One); } else { return(a); } }
/* * 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); }
public static IComplexPolynomial ModMod(IComplexPolynomial toReduce, IComplexPolynomial modPoly, Complex modPrime) { return(ComplexPolynomial.Modulus(ComplexPolynomial.Mod(toReduce, modPoly), modPrime)); }