Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a polynomial from an existing polynomial.
        /// </summary>
        public DecimalPolynomial(DecimalPolynomial copySource) : this()
        {
            copySource.Should().NotBeNull();

            Polynomials = copySource.Polynomials;
            this.Normalize();
            this.TrimLeadingZeroMonomials();
        }
Ejemplo n.º 2
0
 protected bool Equals(DecimalPolynomial other)
 {
     if (other == null)
     {
         return(false);
     }
     return
         (this.Polynomials.Count == other.Polynomials.Count &&
          !this.Polynomials.Except(other.Polynomials).Any());
 }
Ejemplo n.º 3
0
        /// <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));
        }
Ejemplo n.º 4
0
 /// <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()));
 }
Ejemplo n.º 5
0
 public void Substract(DecimalPolynomial subtrahend)
 {
     subtrahend.AdditiveInverse.Polynomials.ForEach(this.Add);
     this.Normalize();
     this.TrimLeadingZeroMonomials();
 }
Ejemplo n.º 6
0
 public void Add(DecimalPolynomial summand)
 {
     summand.Polynomials.ForEach(this.Add);
 }