Ejemplo n.º 1
0
        public static Polynomial Divide(Polynomial N, Polynomial D, out Polynomial R)
        {
            if (!Equals(N.Variable, D.Variable))
            {
                throw new ArgumentException("Polynomials must be of the same variable.", "N/D");
            }

            DefaultDictionary <int, Expression> q = new DefaultDictionary <int, Expression>(0);
            DefaultDictionary <int, Expression> r = new DefaultDictionary <int, Expression>(0);

            foreach (KeyValuePair <int, Expression> i in N.Coefficients)
            {
                r.Add(i.Key, i.Value);
            }

            while (r.Any() && !r[0].Equals(0) && r.Keys.Max() + 1 >= D.Degree)
            {
                int        rd = r.Keys.Max() + 1;
                int        dd = D.Degree;
                Expression t  = r[rd] / D[dd];
                int        td = rd - dd;

                // Compute q += t
                q[td] += t;

                // Compute r -= d * t
                for (int i = 0; i <= dd; ++i)
                {
                    r[i + td] -= -D[i] * t;
                }
            }
            R = new Polynomial(r, N.Variable);
            return(new Polynomial(q, N.Variable));
        }
Ejemplo n.º 2
0
 private Polynomial(IEnumerable<KeyValuePair<int, Expression>> Coefficients, Expression Variable)
 {
     coefficients = new DefaultDictionary<int,Expression>(0);
     foreach (KeyValuePair<int, Expression> i in Coefficients)
         coefficients.Add(i.Key, i.Value);
     variable = Variable;
 }
Ejemplo n.º 3
0
 private Polynomial(IEnumerable <KeyValuePair <int, Expression> > Coefficients, Expression Variable)
 {
     coefficients = new DefaultDictionary <int, Expression>(0);
     foreach (KeyValuePair <int, Expression> i in Coefficients)
     {
         coefficients.Add(i.Key, i.Value);
     }
     variable = Variable;
 }
Ejemplo n.º 4
0
        // Combine like terms and multiply constants.
        protected override Expression VisitProduct(Product M)
        {
            // Map terms to exponents.
            DefaultDictionary <Expression, Real> terms = new DefaultDictionary <Expression, Real>(0);

            // Accumulate constants and sum exponent of each term.
            Real C = 1;

            foreach (Expression i in M.Terms.SelectMany(i => Product.TermsOf(Visit(i))))
            {
                if (i is Constant)
                {
                    Real Ci = (Real)i;
                    // Early exit if 0.
                    if (Ci.EqualsZero())
                    {
                        return(0);
                    }
                    C *= Ci;
                }
                else
                {
                    Power Pi = i as Power;
                    if (!ReferenceEquals(Pi, null) && Pi.Right is Constant)
                    {
                        terms[Pi.Left] += (Real)Pi.Right;
                    }
                    else
                    {
                        terms[i] += 1;
                    }
                }
            }

            // Build a new expression with the accumulated terms.
            if (!C.EqualsOne())
            {
                // Find a sum term that has a constant term to distribute into.
                KeyValuePair <Expression, Real> A = terms.FirstOrDefault(i => Real.Abs(i.Value).EqualsOne() && i.Key is Sum);
                if (!ReferenceEquals(A.Key, null))
                {
                    terms.Remove(A.Key);
                    terms[ExpandExtension.Distribute(C ^ A.Value, A.Key)] += A.Value;
                }
                else
                {
                    terms.Add(C, 1);
                }
            }
            return(Product.New(terms
                               .Where(i => !i.Value.EqualsZero())
                               .Select(i => !i.Value.EqualsOne() ? Power.New(i.Key, Constant.New(i.Value)) : i.Key)));
        }
Ejemplo n.º 5
0
        private void ParseParameters()
        {
            var parameters = OriginalParametersString
                             .Split('&')
                             .Where(p => !string.IsNullOrEmpty(p))
                             .ToList();

            var originalParameters = new DefaultDictionary <string, IList <string> >(StringComparer.OrdinalIgnoreCase);
            var newParameters      = new DefaultDictionary <string, IList <string> >(StringComparer.OrdinalIgnoreCase);

            foreach (var parameter in parameters)
            {
                string key;
                string value     = null;
                var    equalsPos = parameter.IndexOf('=');
                if (equalsPos < 0)
                {
                    key = parameter.ToLower();
                }
                else
                {
                    key   = parameter.Substring(0, equalsPos).Trim().ToLower();
                    value = parameter.Substring(equalsPos + 1).Trim();
                }

                IList <string> values;
                if (originalParameters.TryGetValue(key, out values))
                {
                    values.Add(value);
                    newParameters[key].Add(value);
                }
                else
                {
                    originalParameters.Add(key, new List <string> {
                        value
                    });
                    newParameters.Add(key, new List <string> {
                        value
                    });
                }
            }

            _originalParameters = originalParameters;
            _newParameters      = newParameters;
        }
Ejemplo n.º 6
0
        public static Expression EvaluateSum(IEnumerable <Expression> Terms)
        {
            // Map terms to their coefficients.
            DefaultDictionary <Expression, Real> terms = new DefaultDictionary <Expression, Real>(0);

            // Accumulate constants and sum coefficient of each term.
            Real C = 0;

            foreach (Expression i in Terms)
            {
                if (i is Constant)
                {
                    C += (Real)i;
                }
                else
                {
                    // Find constant term.
                    Constant coeff = Product.TermsOf(i).OfType <Constant>().FirstOrDefault();
                    if (!ReferenceEquals(coeff, null))
                    {
                        terms[Product.New(Product.TermsOf(i).ExceptUnique(coeff, Expression.RefComparer))] += (Real)coeff;
                    }
                    else
                    {
                        terms[i] += 1;
                    }
                }
            }

            // Build a new expression with the accumulated terms.
            if (!C.EqualsZero())
            {
                terms.Add(Constant.New(C), (Real)1);
            }
            return(Sum.New(terms
                           .Where(i => !i.Value.EqualsZero())
                           .Select(i => !i.Value.EqualsOne() ? Product.New(i.Key, Constant.New(i.Value)) : i.Key)));
        }
Ejemplo n.º 7
0
        public static Polynomial Divide(Polynomial N, Polynomial D, out Polynomial R)
        {
            if (!Equals(N.Variable, D.Variable))
                throw new ArgumentException("Polynomials must be of the same variable.", "N/D");

            DefaultDictionary<int, Expression> q = new DefaultDictionary<int, Expression>(0);
            DefaultDictionary<int, Expression> r = new DefaultDictionary<int, Expression>(0);
            foreach (KeyValuePair<int, Expression> i in N.Coefficients)
                r.Add(i.Key, i.Value);

            while (r.Any() && !r[0].Equals(0) && r.Keys.Max() + 1 >= D.Degree)
            {
                int rd = r.Keys.Max() + 1;
                int dd = D.Degree;
                Expression t = r[rd] / D[dd];
                int td = rd - dd;

                // Compute q += t
                q[td] += t;

                // Compute r -= d * t
                for (int i = 0; i <= dd; ++i)
                    r[i + td] -= -D[i] * t;
            }
            R = new Polynomial(r, N.Variable);
            return new Polynomial(q, N.Variable);
        }
Ejemplo n.º 8
0
        // Combine like terms and multiply constants.
        protected override Expression VisitProduct(Product M)
        {
            // Map terms to exponents.
            DefaultDictionary<Expression, Real> terms = new DefaultDictionary<Expression, Real>(0);

            // Accumulate constants and sum exponent of each term.
            Real C = 1;
            foreach (Expression i in M.Terms.SelectMany(i => Product.TermsOf(Visit(i))))
            {
                if (i is Constant)
                {
                    Real Ci = (Real)i;
                    // Early exit if 0.
                    if (Ci.EqualsZero())
                        return 0;
                    C *= Ci;
                }
                else
                {
                    Power Pi = i as Power;
                    if (!ReferenceEquals(Pi, null) && Pi.Right is Constant)
                        terms[Pi.Left] += (Real)Pi.Right;
                    else
                        terms[i] += 1;
                }
            }

            // Build a new expression with the accumulated terms.
            if (!C.EqualsOne())
            {
                // Find a sum term that has a constant term to distribute into.
                KeyValuePair<Expression, Real> A = terms.FirstOrDefault(i => Real.Abs(i.Value).EqualsOne() && i.Key is Sum);
                if (!ReferenceEquals(A.Key, null))
                {
                    terms.Remove(A.Key);
                    terms[ExpandExtension.Distribute(C ^ A.Value, A.Key)] += A.Value;
                }
                else
                {
                    terms.Add(C, 1);
                }
            }
            return Product.New(terms
                .Where(i => !i.Value.EqualsZero())
                .Select(i => !i.Value.EqualsOne() ? Power.New(i.Key, Constant.New(i.Value)) : i.Key));
        }
Ejemplo n.º 9
0
        public static Expression EvaluateSum(IEnumerable<Expression> Terms)
        {
            // Map terms to their coefficients.
            DefaultDictionary<Expression, Real> terms = new DefaultDictionary<Expression, Real>(0);

            // Accumulate constants and sum coefficient of each term.
            Real C = 0;
            foreach (Expression i in Terms)
            {
                if (i is Constant)
                {
                    C += (Real)i;
                }
                else
                {
                    // Find constant term.
                    Constant coeff = Product.TermsOf(i).OfType<Constant>().FirstOrDefault();
                    if (!ReferenceEquals(coeff, null))
                        terms[Product.New(Product.TermsOf(i).ExceptUnique(coeff, Expression.RefComparer))] += (Real)coeff;
                    else
                        terms[i] += 1;
                }
            }

            // Build a new expression with the accumulated terms.
            if (!C.EqualsZero())
                terms.Add(Constant.New(C), (Real)1);
            return Sum.New(terms
                .Where(i => !i.Value.EqualsZero())
                .Select(i => !i.Value.EqualsOne() ? Product.New(i.Key, Constant.New(i.Value)) : i.Key));
        }