/// <summary> /// Adds the polynomial to another polynomial /// </summary> /// <returns></returns> public OneVariablePolynomial Add(OneVariablePolynomial otherPolynomial, bool addOrSubtract = true) { if (this.IsZero()) { return(otherPolynomial); } // First, initialize a zero list. List <double> result = new List <double>(new double[Math.Max(this.coefficients.Count, otherPolynomial.coefficients.Count)]); int i = 0; foreach (double d in this.coefficients) { result[i++] += d; } i = 0; foreach (double d in otherPolynomial.coefficients) { if (addOrSubtract) { result[i++] += d; } else { result[i++] -= d; } } return(new OneVariablePolynomial(result)); }
public static void TestOneVariablePolynomialDivision() { OneVariablePolynomial dividend = new OneVariablePolynomial(new double[] { 1, 1, 2, 1 }); OneVariablePolynomial divisor = new OneVariablePolynomial(new double[] { 1, 2 }); Dictionary <string, OneVariablePolynomial> result = dividend.Divide(divisor); System.Console.WriteLine(string.Join(",", result["quotient"].coefficients)); }
/// <summary> /// Finds the Greatest Common Divisor between this and another polynomial /// </summary> /// <param name="otherPolynomial">The other polynomial with which the GCD is to be calculated</param> /// <returns>The Greatest Common Divisor</returns> public OneVariablePolynomial GCD(OneVariablePolynomial otherPolynomial) { OneVariablePolynomial h = this; OneVariablePolynomial s = otherPolynomial; OneVariablePolynomial rem = new OneVariablePolynomial(); while (!s.IsZero()) { rem = h.Divide(s)["remainder"]; h = s; s = rem; } return(rem); }
/// <summary> /// Divides the polynomial with the divisor. /// </summary> /// <param name="divisor">The divisor. Denoted by g in the CLO book.</param> /// <returns>The quotient and remainder in the form of a tuple.</returns> public Dictionary <string, OneVariablePolynomial> Divide(OneVariablePolynomial divisor) { OneVariablePolynomial quotient = new OneVariablePolynomial(); OneVariablePolynomial remainder = this; while (!remainder.IsZero() && divisor.LeadingTerm().degree <= remainder.LeadingTerm().degree) { OneVariableMonomial update = remainder.LeadingTerm().Divide(divisor.LeadingTerm()); quotient = quotient.Add(new OneVariablePolynomial(update)); remainder = remainder.Add(divisor.Multiply(update), false); // A false addition is basically a subtraction. } return(new Dictionary <string, OneVariablePolynomial>() { { "quotient", quotient }, { "remainder", remainder } }); }
public OneVariablePolynomial Multiply(OneVariablePolynomial otherPolynomial) { throw new Exception("Not implemented yet"); }