コード例 #1
0
        public static CasTerm TermProduct(CasTerm term1, CasTerm term2)
        {
            var res = new CasTerm(term1.Coefficient * term2.Coefficient);

            foreach (var v in term1.Variables.Keys)
            {
                if (res.Variables.ContainsKey(v))
                {
                    res.Variables[v] += term1.Variables[v];
                }
                else
                {
                    res.Variables[v] = term1.Variables[v];
                }
            }
            foreach (var v in term2.Variables.Keys)
            {
                if (res.Variables.ContainsKey(v))
                {
                    res.Variables[v] += term2.Variables[v];
                }
                else
                {
                    res.Variables[v] = term2.Variables[v];
                }
            }
            res.Simplify();
            return(res);
        }
コード例 #2
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)));
        }
コード例 #3
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);
        }
コード例 #4
0
        public CasTerm CopyWithCoefficient(int c)
        {
            if (c == Coefficient)
            {
                return(this);
            }
            var copy = new CasTerm(c);

            foreach (var v in Variables)
            {
                copy.Variables[v.Key] = v.Value;
            }
            return(copy);
        }
コード例 #5
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));
     }
 }