Пример #1
0
 /// <summary>
 /// Test if this polynomial is equal to P.
 /// </summary>
 /// <param name="P"></param>
 /// <returns></returns>
 public bool Equals(Polynomial P)
 {
     int d = Degree;
     if (d != P.Degree)
         return false;
     for (int i = 0; i <= d; ++i)
         if (!this[i].Equals(P[i]))
             return false;
     return true;
 }
Пример #2
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);
        }
Пример #3
0
        public static Polynomial Multiply(Polynomial L, Polynomial R)
        {
            if (!Equals(L.Variable, R.Variable))
                throw new ArgumentException("Polynomials must be of the same variable.", "L*R");

            Dictionary<int, Expression> P = new Dictionary<int, Expression>();
            int D = L.Degree + R.Degree;
            for (int i = 0; i <= D; ++i)
                for (int j = 0; j <= i; ++j)
                    P[i] += L[j] * R[i - j];

            return new Polynomial(P, L.Variable);
        }
Пример #4
0
        public static Polynomial Add(Polynomial L, Polynomial R)
        {
            if (!Equals(L.Variable, R.Variable))
                throw new ArgumentException("Polynomials must be of the same variable.", "L+R");

            DefaultDictionary<int, Expression> P = new DefaultDictionary<int, Expression>(0);
            int D = Math.Max(L.Degree, R.Degree);
            for (int i = 0; i <= D; ++i)
                P[i] = L[i] + R[i];

            return new Polynomial(P, L.Variable);
        }