Esempio n. 1
0
        public CasExpr ToExpr()
        {
            var p = new CasPolynomial();

            p.Terms.Add(this);
            return(new CasExpr(p, CasPolynomial.ConstantPoly(1)));
        }
Esempio n. 2
0
 public CasExpr(CasPolynomial poly1, CasPolynomial poly2)
 {
     Poly1 = poly1;
     Poly2 = poly2;
     if (poly2.Terms.Count == 0)
     {
         throw new Exception("Attempt to divide by zero.");
     }
 }
Esempio n. 3
0
        public static CasPolynomial ConstantPoly(int c)
        {
            var p = new CasPolynomial();

            if (c != 0)
            {
                p.Terms.Add(new CasTerm(c));
            }
            return(p);
        }
Esempio n. 4
0
        public CasExpr VarExpr(CasVar v)
        {
            var t = new CasTerm(1);

            t.Variables[v] = 1;
            var p = new CasPolynomial();

            p.Terms.Add(t);
            return(new CasExpr(p, CasPolynomial.ConstantPoly(1)));
        }
Esempio n. 5
0
 public CasExpr Mul(CasExpr expr1, CasExpr expr2)
 {
     if (expr1.Poly1.PolyEquals(expr2.Poly2))
     {
         return(new CasExpr(expr2.Poly1, expr1.Poly2).Simplify());
     }
     if (expr1.Poly2.PolyEquals(expr2.Poly1))
     {
         return(new CasExpr(expr1.Poly1, expr2.Poly2).Simplify());
     }
     return(new CasExpr(CasPolynomial.PolyProd(expr1.Poly1, expr2.Poly1), CasPolynomial.PolyProd(expr1.Poly2, expr2.Poly2)).Simplify());
 }
Esempio n. 6
0
        public static CasPolynomial PolyProd(CasPolynomial poly1, CasPolynomial poly2)
        {
            CasPolynomial res = new CasPolynomial();

            foreach (var t1 in poly1.Terms)
            {
                foreach (var t2 in poly2.Terms)
                {
                    res.Terms.Add(CasTerm.TermProduct(t1, t2));
                }
            }
            res.Simplify();
            return(res);
        }
Esempio n. 7
0
        public CasPolynomial DivideTerms(int d)
        {
            CasPolynomial res = new CasPolynomial();

            foreach (var term in Terms)
            {
                if (term.Coefficient % d != 0)
                {
                    throw new Exception("Attempt to divide polynomial by int it isn't divisible by");
                }
                res.Terms.Add(term.CopyWithCoefficient(term.Coefficient / d));
            }
            return(res);
        }
Esempio n. 8
0
        public static CasPolynomial PolySum(CasPolynomial poly1, CasPolynomial poly2)
        {
            CasPolynomial res = new CasPolynomial();

            foreach (var t in poly1.Terms)
            {
                res.Terms.Add(t);
            }
            foreach (var t in poly2.Terms)
            {
                res.Terms.Add(t);
            }
            res.Simplify();
            return(res);
        }
Esempio n. 9
0
 public CasExpr Add(CasExpr expr1, CasExpr expr2)
 {
     if (expr1.Poly2.PolyEquals(expr2.Poly2))
     {
         return(new CasExpr(CasPolynomial.PolySum(expr1.Poly1, expr2.Poly1), expr2.Poly2).Simplify());
     }
     else
     {
         var p1    = CasPolynomial.PolyProd(expr1.Poly1, expr2.Poly2);
         var p2    = CasPolynomial.PolyProd(expr1.Poly2, expr2.Poly1);
         var num   = CasPolynomial.PolySum(p1, p2);
         var denom = CasPolynomial.PolyProd(expr1.Poly2, expr2.Poly2);
         return(new CasExpr(num, denom).Simplify());
     }
 }
Esempio n. 10
0
        public bool PolyEquals(CasPolynomial poly)
        {
            HashSet <string> terms = new HashSet <string>();

            foreach (var t in poly.Terms)
            {
                terms.Add(t.ToString());
            }
            foreach (var t in Terms)
            {
                if (!terms.Contains(t.ToString()))
                {
                    return(false);
                }
                terms.Remove(t.ToString());
            }
            return(terms.Count == 0);
        }
Esempio n. 11
0
        public CasExpr Substitute(CasExpr expr, CasVar forV)
        {
            if (!ContainsVar(forV))
            {
                return(new CasExpr(this, CasPolynomial.ConstantPoly(1)));
            }
            List <CasExpr> exprs = new List <CasExpr>();

            foreach (var t in Terms)
            {
                exprs.Add(t.Substitute(expr, forV));
            }
            var sum = CasExpr.ConstantExpr(0);

            foreach (var e in exprs)
            {
                sum = CasSystem.Instance.Add(sum, e);
            }
            return(sum);
        }
Esempio n. 12
0
        // Result is null if solver fails
        public CasExpr MakeZero(CasExpr expr, CasVar v)
        {
            List <CasTerm> zeroTerms = new List <CasTerm>();
            List <CasTerm> oneTerms  = new List <CasTerm>();

            foreach (var t in expr.Poly1.Terms)
            {
                if (!t.Variables.ContainsKey(v))
                {
                    zeroTerms.Add(t);
                }
                else if (t.Variables[v] == 1)
                {
                    oneTerms.Add(t);
                }
                else
                {
                    return(null);
                }
            }
            if (oneTerms.Count == 0)
            {
                return(null);
            }
            CasPolynomial poly1 = new CasPolynomial();
            CasPolynomial poly2 = new CasPolynomial();

            foreach (var t in zeroTerms)
            {
                poly1.Terms.Add(t.CopyWithCoefficient(-1 * t.Coefficient));
            }
            foreach (var t in oneTerms)
            {
                poly2.Terms.Add(t.CopyWithCoefficient(t.Coefficient));
            }
            foreach (var t in poly2.Terms)
            {
                t.Variables.Remove(v);
            }
            return(new CasExpr(poly1, poly2));
        }
Esempio n. 13
0
 public static CasExpr ConstantExpr(int c)
 {
     return(new CasExpr(CasPolynomial.ConstantPoly(c), CasPolynomial.ConstantPoly(1)));
 }
Esempio n. 14
0
 // Result is null if solver fails
 public Tuple <CasExpr, CasVar> Solve(CasExpr expr, CasVar forV)
 {
     if (expr.Poly1.ContainsVar(forV) && expr.Poly2.ContainsVar(forV))
     {
         return(null);
     }
     if (!expr.Poly1.ContainsVar(forV) && !expr.Poly2.ContainsVar(forV))
     {
         return(null);
     }
     if (expr.Poly1.ContainsVar(forV) && !expr.Poly2.ContainsVar(forV))
     {
         List <CasTerm> zeroTerms = new List <CasTerm>();
         List <CasTerm> oneTerms  = new List <CasTerm>();
         foreach (var t in expr.Poly1.Terms)
         {
             if (!t.Variables.ContainsKey(forV))
             {
                 zeroTerms.Add(t);
             }
             else if (t.Variables[forV] == 1)
             {
                 oneTerms.Add(t);
             }
             else
             {
                 return(null);
             }
         }
         if (oneTerms.Count == 0)
         {
             return(null);
         }
         CasPolynomial polyZeros = new CasPolynomial();
         CasPolynomial polyOnes  = new CasPolynomial();
         foreach (var t in zeroTerms)
         {
             polyZeros.Terms.Add(t.CopyWithCoefficient(-1 * t.Coefficient));
         }
         foreach (var t in oneTerms)
         {
             polyOnes.Terms.Add(t.CopyWithCoefficient(t.Coefficient));
             t.Variables.Remove(forV);
         }
         var newVar  = NewVar();
         var newTerm = new CasTerm(1);
         newTerm.Variables[newVar] = 1;
         polyZeros.Terms.Add(newTerm);
         var resExpr = new CasExpr(CasPolynomial.PolyProd(polyZeros, expr.Poly2), polyOnes);
         return(new Tuple <CasExpr, CasVar>(resExpr, newVar));
     }
     else
     {
         List <CasTerm> zeroTerms = new List <CasTerm>();
         List <CasTerm> oneTerms  = new List <CasTerm>();
         foreach (var t in expr.Poly2.Terms)
         {
             if (!t.Variables.ContainsKey(forV))
             {
                 zeroTerms.Add(t);
             }
             else if (t.Variables[forV] == 1)
             {
                 oneTerms.Add(t);
             }
             else
             {
                 return(null);
             }
         }
         if (oneTerms.Count == 0)
         {
             return(null);
         }
         CasPolynomial polyZeros = new CasPolynomial();
         CasPolynomial polyOnes  = new CasPolynomial();
         var           newVar    = NewVar();
         foreach (var t in zeroTerms)
         {
             polyZeros.Terms.Add(t.CopyWithCoefficient(-1 * t.Coefficient));
             t.Variables[newVar] = 1;
         }
         foreach (var t in oneTerms)
         {
             polyOnes.Terms.Add(t.CopyWithCoefficient(t.Coefficient));
             t.Variables.Remove(forV);
             t.Variables[newVar] = 1;
         }
         var resExpr = new CasExpr(CasPolynomial.PolySum(polyZeros, expr.Poly2), polyOnes);
         return(new Tuple <CasExpr, CasVar>(resExpr, newVar));
     }
 }