private static ObjectFormulaTree delConst(ObjectFormulaTree tree, ref bool simple) { IObjectOperation op = tree.Operation; if (op is PolySum) { PolySum ps = op as PolySum; return(delConst(ps, ref simple)); } List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { l.Add(delConst(tree[i], ref simple)); } if ((op is ElementaryBrackets) | (op is ElementaryRoot) | (op is ElementaryPowerOperation)) { if (l.Count == 2) { ObjectFormulaTree t = l[1]; if (ElementaryFormulaSimplification.IsConst(t)) { double a = (double)t.Result; if (a == 1) { simple = false; return(l[0]); } } } } return(new ObjectFormulaTree(op, l)); }
static internal ObjectFormulaTree simplify(ObjectFormulaTree tree, out bool simple) { //bool s = true; simple = true; List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { ObjectFormulaTree t = simplifyRecursive(tree[i]); l.Add(t); } if (!(tree.Operation is PolyMult)) { return(new ObjectFormulaTree(tree.Operation, l)); } List <ObjectFormulaTree> consts = new List <ObjectFormulaTree>(); List <ObjectFormulaTree> forms = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { if (ElementaryFormulaSimplification.IsConst(tree[i])) { consts.Add(tree[i]); continue; } forms.Add(tree[i]); } double a = 1; foreach (ObjectFormulaTree t in consts) { double x = (double)t.Result; a *= x; } if (a == 0) { ElementaryRealConstant x = new ElementaryRealConstant(0); simple = true; return(new ObjectFormulaTree(x, new List <ObjectFormulaTree>())); } if (a == 1) { PolyMult pm = new PolyMult(); foreach (ObjectFormulaTree t in forms) { pm.add(t); } if (consts.Count > 0) { simple = false; } return(new ObjectFormulaTree(pm, forms)); } if (consts.Count <= 1) { PolyMult pm = new PolyMult(); pm.l = l; return(new ObjectFormulaTree(tree.Operation, l)); } simple = false; ElementaryRealConstant xx = new ElementaryRealConstant(a); ObjectFormulaTree yy = new ObjectFormulaTree(xx, new List <ObjectFormulaTree>()); List <ObjectFormulaTree> ll = new List <ObjectFormulaTree>(); ll.Add(yy); ll.AddRange(forms); PolyMult pmm = new PolyMult(); pmm.l = new List <ObjectFormulaTree>(ll); return(new ObjectFormulaTree(pmm, ll)); }
private static ObjectFormulaTree sumSumInverse(PolySum ps) { Dictionary <ObjectFormulaTree, bool> d = new Dictionary <ObjectFormulaTree, bool>(ps.summands); ObjectFormulaTree first = null; ObjectFormulaTree second = null; bool bf = true; foreach (ObjectFormulaTree t in d.Keys) { if (ElementaryFormulaSimplification.IsConst(t)) { first = t; } } if (first == null) { if (d.Count > 0) { foreach (ObjectFormulaTree t in d.Keys) { first = t; bf = d[t]; break; } } } if (first == null) { ElementaryRealConstant er = new ElementaryRealConstant(0); return(new ObjectFormulaTree(er, new List <ObjectFormulaTree>())); } d.Remove(first); first = sumSumInverse(first); if (!bf) { ElementaryFunctionOperation ef = new ElementaryFunctionOperation('-'); List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); l.Add(first); first = new ObjectFormulaTree(ef, l); } if (d.Count == 0) { return(first); } ObjectFormulaTree sec = null; foreach (ObjectFormulaTree t in d.Keys) { sec = t; } bool kb = d[sec]; PolySum pp = new PolySum(true); Dictionary <ObjectFormulaTree, bool> dd = pp.summands; foreach (ObjectFormulaTree t in d.Keys) { if (PolyMult.IsZero(t)) { continue; } bool bl = d[t]; if (!kb) { bl = !bl; } dd[t] = bl; } second = sumSumInverse(pp); List <ObjectFormulaTree> lr = new List <ObjectFormulaTree>(); lr.Add(first); lr.Add(second); char c = kb ? '+' : '-'; if (PolyMult.IsZero(second)) { return(first); } Double type = 0; ElementaryBinaryOperation eop = new ElementaryBinaryOperation(c, new object[] { type, type }); return(new ObjectFormulaTree(eop, lr)); }
private static ObjectFormulaTree delConst(PolySum ps, ref bool simple) { Dictionary <ObjectFormulaTree, bool> dd = ps.summands; List <ObjectFormulaTree> del = new List <ObjectFormulaTree>(); Dictionary <ObjectFormulaTree, bool> d = new Dictionary <ObjectFormulaTree, bool>(); foreach (ObjectFormulaTree ttt in dd.Keys) { ObjectFormulaTree ts = ElementaryFormulaSimplification.Object.Simplify(ttt); if (!PolyMult.IsZero(ts)) { d[ts] = dd[ttt]; } } PolySum p = new PolySum(true); Dictionary <ObjectFormulaTree, bool> forms = p.summands; double a = 0; int i = 0; foreach (ObjectFormulaTree t in d.Keys) { bool b = d[t]; ObjectFormulaTree tr = delConst(t, ref simple); if (PolyMult.IsZero(tr)) { simple = false; continue; } if (ElementaryFormulaSimplification.IsConst(tr)) { double x = (double)tr.Result; a += b ? x : -x; ++i; if (i > 1) { simple = false; } } else { forms[tr] = d[t]; } } if (a != 0) { ElementaryRealConstant ec = new ElementaryRealConstant(a); forms[new ObjectFormulaTree(ec, new List <ObjectFormulaTree>())] = true; } if (forms.Count == 1) { foreach (ObjectFormulaTree f in forms.Keys) { bool b = forms[f]; if (b) { return(f); } ElementaryFunctionOperation op = new ElementaryFunctionOperation('-'); List <ObjectFormulaTree> lop = new List <ObjectFormulaTree>(); lop.Add(f); return(new ObjectFormulaTree(op, lop)); } } return(new ObjectFormulaTree(p, new List <ObjectFormulaTree>())); }