/// <summary> /// Constructs a polynomial from an existing polynomial. /// </summary> public DecimalPolynomial(DecimalPolynomial copySource) : this() { copySource.Should().NotBeNull(); Polynomials = copySource.Polynomials; this.Normalize(); this.TrimLeadingZeroMonomials(); }
protected bool Equals(DecimalPolynomial other) { if (other == null) { return(false); } return (this.Polynomials.Count == other.Polynomials.Count && !this.Polynomials.Except(other.Polynomials).Any()); }
/// <summary> /// Implements division through recursion. /// </summary> private static Tuple <DecimalPolynomial, DecimalPolynomial> Divide(DecimalPolynomial left, DecimalPolynomial right, DecimalPolynomial accumulatedResult) { var leftIsSmallerDegree = left.Degree < right.Degree; var isLeftDivisible = left.LeadingMonomial.Value / right.LeadingMonomial.Value != 0; if (leftIsSmallerDegree || !isLeftDivisible) { return(Tuple.Create(accumulatedResult, left)); } var degreeDifference = left.LeadingMonomial.Degree - right.LeadingMonomial.Degree; var valueMultiplicalDifference = left.LeadingMonomial.Value / right.LeadingMonomial.Value; var polynomialForSubstraction = right.MultiplyBy(valueMultiplicalDifference).RiseDegreeBy(degreeDifference); accumulatedResult.Add(new DecimalMonomial(valueMultiplicalDifference, degreeDifference + 1)); return(Divide(left - polynomialForSubstraction, right, accumulatedResult)); }
/// <summary> /// Divides left polynomial with right and returns a tuple (result, rest). /// </summary> public static Tuple <DecimalPolynomial, DecimalPolynomial> Divide(DecimalPolynomial left, DecimalPolynomial right) { return(Divide(left, right, new DecimalPolynomial())); }
public void Substract(DecimalPolynomial subtrahend) { subtrahend.AdditiveInverse.Polynomials.ForEach(this.Add); this.Normalize(); this.TrimLeadingZeroMonomials(); }
public void Add(DecimalPolynomial summand) { summand.Polynomials.ForEach(this.Add); }