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 IComplexPolynomial GCD(IComplexPolynomial left, IComplexPolynomial right, Complex modulus) { IComplexPolynomial a = left.Clone(); IComplexPolynomial b = right.Clone(); if (b.Degree > a.Degree) { IComplexPolynomial swap = b; b = a; a = swap; } while (!(b.Terms.Length == 0 || b.Terms[0].CoEfficient == 0)) { IComplexPolynomial temp = a; a = b; b = ComplexPolynomial.ModMod(temp, b, modulus); } if (a.Degree == 0) { return(ComplexPolynomial.One); } else { return(a); } }
public static IComplexPolynomial GCDMod(IComplexPolynomial left, IComplexPolynomial right, Complex polynomialBase) { IComplexPolynomial a = left.Clone(); IComplexPolynomial b = right.Clone(); Swap(ref a, ref b); while (a.Degree != b.Degree) { IComplexPolynomial smallerA = ComplexPolynomial.ReduceDegree(a, polynomialBase); a = smallerA; Swap(ref a, ref b); } while (a.Degree != 1) { IComplexPolynomial smallerA = ComplexPolynomial.ReduceDegree(a, polynomialBase); IComplexPolynomial smallerB = ComplexPolynomial.ReduceDegree(b, polynomialBase); a = smallerA; b = smallerB; Swap(ref a, ref b); } while (a.Degree >= 1) { Swap(ref a, ref b); var bSign = b.Terms.Last().CoEfficient.Sign(); if (bSign < 0) { break; } while (!(b.Terms.Length == 0 || b.Terms[0].CoEfficient == 0 || a.CompareTo(b) < 0)) { var aSign = a.Terms.Last().CoEfficient.Sign(); bSign = b.Terms.Last().CoEfficient.Sign(); if (aSign < 0 || bSign < 0) { break; } a = ComplexPolynomial.Subtract(a, b); } } if (a.Degree == 0) { return(ComplexPolynomial.One); } else { return(a); } }
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 IComplexPolynomial Add(IComplexPolynomial left, IComplexPolynomial right) { if (left == null) { throw new ArgumentNullException(nameof(left)); } if (right == null) { throw new ArgumentNullException(nameof(right)); } Complex[] terms = new Complex[Math.Max(left.Degree, right.Degree) + 1]; for (int i = 0; i < terms.Length; i++) { Complex l = left[i]; Complex r = right[i]; Complex ttl = (l + r); terms[i] = ttl; } IComplexPolynomial result = new ComplexPolynomial(ComplexTerm.GetTerms(terms.ToArray())); return(result); }
public static IComplexPolynomial MakeMonic(IComplexPolynomial polynomial, Complex polynomialBase) { int deg = polynomial.Degree; IComplexPolynomial result = new ComplexPolynomial(polynomial.Terms.ToArray()); if (Complex.Abs(result.Terms[deg].CoEfficient) > 1) { Complex toAdd = (result.Terms[deg].CoEfficient - 1) * polynomialBase; result.Terms[deg].CoEfficient = 1; result.Terms[deg - 1].CoEfficient += toAdd; } return(result); }
public static IComplexPolynomial FromRoots(params Complex[] roots) { return(ComplexPolynomial.Product( roots.Select( zero => new ComplexPolynomial( new ComplexTerm[] { new ComplexTerm(1, 1), new ComplexTerm(Complex.Negate(zero), 0) } ) ) )); }
public static IComplexPolynomial Sum(IEnumerable <IComplexPolynomial> polys) { IComplexPolynomial result = null; foreach (IComplexPolynomial p in polys) { if (result == null) { result = p; } else { result = ComplexPolynomial.Add(result, p); } } return(result); }
public static IComplexPolynomial Modulus(IComplexPolynomial poly, Complex mod) { IComplexPolynomial clone = poly.Clone(); List <IComplexTerm> terms = new List <IComplexTerm>(); foreach (IComplexTerm term in clone.Terms) { Complex remainder = Complex.Divide(term.CoEfficient, mod); terms.Add(new ComplexTerm(remainder, term.Exponent)); } // Recalculate the degree IComplexTerm[] termArray = terms.SkipWhile(t => t.CoEfficient.Sign() == 0).ToArray(); IComplexPolynomial result = new ComplexPolynomial(termArray); return(result); }
public static IComplexPolynomial GetDerivativePolynomial(IComplexPolynomial poly) { int d = 0; List <IComplexTerm> terms = new List <IComplexTerm>(); foreach (IComplexTerm term in poly.Terms) { d = term.Exponent - 1; if (d < 0) { continue; } terms.Add(new ComplexTerm(term.CoEfficient * term.Exponent, d)); } IComplexPolynomial result = new ComplexPolynomial(terms.ToArray()); return(result); }
public static IComplexPolynomial Mod(IComplexPolynomial poly, IComplexPolynomial mod) { int sortOrder = mod.CompareTo(poly); if (sortOrder > 0) { return(poly.Clone()); } else if (sortOrder == 0) { return(ComplexPolynomial.Zero); } IComplexPolynomial remainder = new ComplexPolynomial(); Divide(poly, mod, out remainder); return(remainder); }
public static IComplexPolynomial ExtendedGCD(IComplexPolynomial left, IComplexPolynomial right, Complex mod) { IComplexPolynomial rem = ComplexPolynomial.Two; IComplexPolynomial a = left.Clone(); IComplexPolynomial b = right.Clone(); IComplexPolynomial c = ComplexPolynomial.Zero; while (c.CompareTo(ComplexPolynomial.Zero) != 0 && rem.CompareTo(ComplexPolynomial.Zero) != 0 && rem.CompareTo(ComplexPolynomial.One) != 0) { c = ComplexPolynomial.Divide(a, b, out rem); a = b; b = rem; } if (rem.CompareTo(ComplexPolynomial.Zero) != 0 || rem.CompareTo(ComplexPolynomial.One) != 0) { return(ComplexPolynomial.One); } return(rem); }
/* * 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)); }
public int GetHashCode(ComplexPolynomial obj) { IComplexPolynomial poly = obj as IComplexPolynomial; return(poly.GetHashCode(poly)); }
public static IComplexPolynomial Square(IComplexPolynomial poly) { return(ComplexPolynomial.Multiply(poly, poly)); }
public static IComplexPolynomial Divide(IComplexPolynomial left, IComplexPolynomial right) { IComplexPolynomial remainder = ComplexPolynomial.Zero; return(ComplexPolynomial.Divide(left, right, out remainder)); }
public bool Equals(ComplexPolynomial x, ComplexPolynomial y) { return(x.CompareTo(y) == 0); }
public override string ToString() { return(ComplexPolynomial.FormatString(this)); }
public bool Equals(ComplexPolynomial other) { return(this.CompareTo(other) == 0); }