public float Eval(CasExpr expr, Dictionary <CasVar, float> values) { float res1 = expr.Poly1.Eval(values); float res2 = expr.Poly2.Eval(values); return(res1 / res2); }
public CasExpr Substitute(CasExpr expr, CasVar forV, CasExpr inExpr) { CasExpr expr1 = inExpr.Poly1.Substitute(expr, forV); CasExpr expr2 = inExpr.Poly2.Substitute(expr, forV); return(Div(expr1, expr2)); }
public CasExpr Mul(CasExpr expr1, CasExpr expr2) { if (expr1.Poly1.PolyEquals(expr2.Poly2)) { return(new CasExpr(expr2.Poly1, expr1.Poly2)); } if (expr1.Poly2.PolyEquals(expr2.Poly1)) { return(new CasExpr(expr1.Poly1, expr2.Poly2)); } return(new CasExpr(CasPolynomial.PolyProd(expr1.Poly1, expr2.Poly1), CasPolynomial.PolyProd(expr1.Poly2, expr2.Poly1))); }
public CasExpr Substitute(CasExpr expr, CasVar forV) { if (!Variables.ContainsKey(forV)) { return(this.ToExpr()); } var exprPow = expr.RaisedToPower(Variables[forV]); var newTerm = CopyWithCoefficient(Coefficient); newTerm.Variables.Remove(forV); return(CasSystem.Instance.Mul(newTerm.ToExpr(), exprPow)); }
public CasExpr RaisedToPower(int exp) { if (exp == 0) { return(ConstantExpr(1)); } CasExpr res = this; for (int i = 1; i < exp; i++) { res = CasSystem.Instance.Mul(res, this); } return(res); }
public CasExpr Add(CasExpr expr1, CasExpr expr2) { if (expr1.Poly2.PolyEquals(expr2.Poly2)) { return(new CasExpr(CasPolynomial.PolySum(expr1.Poly1, expr2.Poly1), expr2.Poly2)); } 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)); } }
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); }
// 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)); }
public CasExpr Div(CasExpr expr1, CasExpr expr2) { return(Mul(expr1, new CasExpr(expr2.Poly2, expr2.Poly1))); }
public CasExpr Sub(CasExpr expr1, CasExpr expr2) { return(Add(expr1, Mul(expr2, ConstExpr(-1)))); }
public CasExpr ConstExpr(int c) { return(CasExpr.ConstantExpr(c)); }
// 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)); } }