static internal bool IsZero(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is ElementaryRealConstant) { ElementaryRealConstant ec = op as ElementaryRealConstant; double a = ec.Value; return(a == 0); } if (!(op is PolyMult)) { return(false); } PolyMult pm = op as PolyMult; for (int i = 0; i < tree.Count; i++) { if (IsZero(tree[i])) { return(true); } } return(false); }
ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string s) { Double a = 0; double val = 0; bool zero = false; if (ReturnType.Equals(a)) { zero = true; if (s.Length == 1) { if (symbol is Char) { if (s[0] == (char)symbol) { val = 1; zero = false; } } } } if (zero) { return(ElementaryRealConstant.RealZero); } ElementaryRealConstant op = new ElementaryRealConstant(val); return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>())); }
/// <summary> /// Simplifies constants /// </summary> /// <param name="tree">The tree</param> /// <param name="completed">The "completed" sign</param> /// <returns>Simplified tree</returns> public static ObjectFormulaTree SimplifyContstants(ObjectFormulaTree tree, out bool completed) { bool comp = true; completed = true; if (IsConst(tree)) { if (tree.Operation is ElementaryRealConstant) { completed = true; return(tree); } if (tree.ReturnType.Equals(a)) { ElementaryRealConstant x = new ElementaryRealConstant((double)tree.Result); completed = false; return(new ObjectFormulaTree(x, new List <ObjectFormulaTree>())); } } List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { ObjectFormulaTree t = SimplifyContstants(tree[i], out comp); if (!comp) { completed = false; } l.Add(t); } return(new ObjectFormulaTree(tree.Operation, l)); }
/// <summary> /// Calculates derivation tree /// </summary> /// <param name="tree">Tree to calculate</param> /// <param name="variableName">Name of variable</param> /// <returns>Tree of derivation</returns> static public ObjectFormulaTree Derivation(this ObjectFormulaTree tree, string variableName) { if (tree.Operation is IDerivationOperation) { IDerivationOperation op = tree.Operation as IDerivationOperation; return(op.Derivation(tree, variableName)); } if (ElementaryBinaryOperation.IsInteger(tree.Operation.ReturnType)) { ElementaryRealConstant op = new ElementaryRealConstant(0); return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>())); } if (tree.Operation is OptionalOperation) { OptionalOperation op = tree.Operation as OptionalOperation; op = op.Clone() as OptionalOperation; List <ObjectFormulaTree> list = new List <ObjectFormulaTree>(); list.Add(tree[0]); for (int i = 1; i < 3; i++) { list.Add(tree[i].Derivation(variableName)); } return(new ObjectFormulaTree(op, list)); } return(null); }
/// <summary> /// Gets constant string representation of value of tree /// </summary> /// <param name="tree">The tree</param> /// <returns>String representation</returns> public override string GetConstValue(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is ElementaryRealConstant) { ElementaryRealConstant co = op as ElementaryRealConstant; return(co.StringValue); } if (op.ReturnType.Equals("")) { return("\"\""); } return(null); }
/// <summary> /// Derivation of square root /// </summary> /// <param name="tree">Prototype tree</param> /// <param name="variableName">Name of variable</param> /// <returns>The derivation</returns> public static ObjectFormulaTree SquareRootDerivation(ObjectFormulaTree tree, string variableName) { ObjectFormulaTree co = ElementaryRealConstant.GetConstant(0.5); ObjectFormulaTree derNom = tree[0].Derivation(variableName); List <ObjectFormulaTree> lnom = new List <ObjectFormulaTree>() { co, derNom }; Double a = 0; ElementaryBinaryOperation bo = new ElementaryBinaryOperation('*', new object[] { a, a }); ObjectFormulaTree tn = new ObjectFormulaTree(bo, lnom); List <ObjectFormulaTree> l = new List <ObjectFormulaTree>() { tn, tree }; return(new ObjectFormulaTree(ElementaryFraction.Object, l)); }
ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string s) { double val = 0; bool zero = true; if (s.Equals(name)) { val = 1; zero = false; } if (zero) { return(ElementaryRealConstant.RealZero); } ElementaryRealConstant op = new ElementaryRealConstant(val); return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>())); }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The function for derivation calculation</param> /// <param name="s">Derivation string</param> /// <returns>The derivation</returns> public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string s) { double val = 0; if (s.Length == 1) { if (symbol is Char) { char ch = (char)symbol; if (s[0] == ch) { val = 1; } } } ElementaryRealConstant op = new ElementaryRealConstant(val); return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>())); }
internal static ObjectFormulaTree MultMultReverse(ObjectFormulaTree tree) { Double a = 0; List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); IObjectOperation op = tree.Operation; PolyMult pm = null; if (op is PolyMult) { pm = op as PolyMult; } for (int i = 0; i < tree.Count; i++) { l.Add(MultMultReverse(tree[i])); } if (pm == null) { return(new ObjectFormulaTree(op, l)); } if (l.Count == 1) { return(l[0]); } PolyMult p = new PolyMult(); if (l.Count == 0) { ElementaryRealConstant rc = new ElementaryRealConstant(0); return(new ObjectFormulaTree(rc, new List <ObjectFormulaTree>())); } ObjectFormulaTree left = l[0]; l.RemoveAt(0); p.l = l; ObjectFormulaTree right = MultMultReverse(new ObjectFormulaTree(p, l)); List <ObjectFormulaTree> lll = new List <ObjectFormulaTree>(); lll.Add(left); lll.Add(right); ElementaryBinaryOperation bo = new ElementaryBinaryOperation('*', new object[] { a, a }); return(new ObjectFormulaTree(bo, lll)); }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The function for derivation calculation</param> /// <param name="variableName">Name of variable</param> /// <returns>The derivation</returns> public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string variableName) { if (arity == 1) { return(ElementaryFunctionOperation.SquareRootDerivation(tree, variableName)); } IObjectOperation mainOp = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a); List <ObjectFormulaTree> mainList = new List <ObjectFormulaTree>(); mainList.Add(tree[0]); IObjectOperation secondOp = ElementaryFraction.Object; List <ObjectFormulaTree> secondList = new List <ObjectFormulaTree>(); IObjectOperation secondFistOperation = new ElementaryRealConstant(1); ObjectFormulaTree secondFirstTree = new ObjectFormulaTree(secondFistOperation, new List <ObjectFormulaTree>()); secondList.Add(secondFirstTree); secondList.Add(tree[1]); ObjectFormulaTree secondTree = new ObjectFormulaTree(secondOp, secondList); mainList.Add(secondTree); return((new ObjectFormulaTree(mainOp, mainList)).Derivation(variableName)); }
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>())); }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The function for derivation calculation</param> /// <param name="variableName">Name of variable</param> /// <returns>The derivation</returns> public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string variableName) { Double a = 0; ObjectFormulaTree[] fc = new ObjectFormulaTree[2]; ObjectFormulaTree[] fd = new ObjectFormulaTree[2]; bool[] b = new bool[] { false, false }; for (int i = 0; i < 2; i++) { fc[i] = tree[i];//.Clone() as ObjectFormulaTree; fd[i] = fc[i].Derivation(variableName); bool bb = ZeroPerformer.IsZero(fd[i]); b[i] = bb; if (!bb) { // glo = true; } } if (b[1]) { double rs = (double)fc[1].Result; if (rs == 1) { return(fd[0]); } } List <ObjectFormulaTree> mainList = new List <ObjectFormulaTree>(); IObjectOperation mainOperation = new ElementaryBinaryOperation('+', new object[] { a, a }); List <ObjectFormulaTree> firstList = new List <ObjectFormulaTree>(); IObjectOperation firstOperation = new ElementaryBinaryOperation('*', new object[] { a, a }); List <ObjectFormulaTree> secondList = new List <ObjectFormulaTree>(); IObjectOperation secondOperation = new ElementaryBinaryOperation('*', new object[] { a, a }); List <ObjectFormulaTree> firstFirstList = new List <ObjectFormulaTree>(); firstFirstList.Add(fc[1]); IObjectOperation firstFirstOperation = new ElementaryBinaryOperation('*', new object[] { a, a }); IObjectOperation firstFirstSecondOperation = new ElementaryPowerOperation(valType, powType); List <ObjectFormulaTree> firstFirstSecondList = new List <ObjectFormulaTree>(); firstFirstSecondList.Add(fc[0]); List <ObjectFormulaTree> firstFirstSecondSecondList = new List <ObjectFormulaTree>(); IObjectOperation firstFirstSecondSecondOperation = new ElementaryBinaryOperation('-', new object[] { a, a }); ObjectFormulaTree fcd = fd[1]; firstFirstSecondSecondList.Add(fc[1]); IObjectOperation firstFirstSecondSecondSecondOperation = new ElementaryRealConstant(1); ObjectFormulaTree firstFirstSecondSecondSecondTree = new ObjectFormulaTree(firstFirstSecondSecondSecondOperation, new List <ObjectFormulaTree>()); firstFirstSecondSecondList.Add(firstFirstSecondSecondSecondTree); ObjectFormulaTree firstFirstSecondSecondTree = null; bool unityDeg = false; if (ZeroPerformer.IsZero(fd[1])) { double f2d = (double)tree[1].Result - 1; if (f2d == 1) { unityDeg = true; } ElementaryRealConstant erc = new ElementaryRealConstant(f2d); firstFirstSecondSecondTree = new ObjectFormulaTree(erc, new List <ObjectFormulaTree>()); } else { firstFirstSecondSecondTree = new ObjectFormulaTree(firstFirstSecondSecondOperation, firstFirstSecondSecondList); } firstFirstSecondList.Add(firstFirstSecondSecondTree); ObjectFormulaTree firstFirstSecondTree = null; if (unityDeg) { firstFirstSecondTree = fc[0]; } else { firstFirstSecondTree = new ObjectFormulaTree(firstFirstSecondOperation, firstFirstSecondList); } firstFirstList.Add(firstFirstSecondTree); ObjectFormulaTree firstFirstTree = new ObjectFormulaTree(firstFirstOperation, firstFirstList); firstList.Add(firstFirstTree); firstList.Add(fd[0]); ObjectFormulaTree firstTree = new ObjectFormulaTree(firstOperation, firstList); mainList.Add(firstTree); // Second part IObjectOperation secondFirstOperation = new ElementaryBinaryOperation('*', new object[] { a, a }); List <ObjectFormulaTree> secondFirstList = new List <ObjectFormulaTree>(); IObjectOperation secondFirstFirstOperation = new ElementaryFunctionOperation('l'); List <ObjectFormulaTree> secondFirstFirstList = new List <ObjectFormulaTree>(); secondFirstFirstList.Add(fc[0]);//.Clone() as ObjectFormulaTree); ObjectFormulaTree secondFirstFirstTree = new ObjectFormulaTree(secondFirstFirstOperation, secondFirstFirstList); secondFirstList.Add(secondFirstFirstTree); secondFirstList.Add(tree);//.Clone() as ObjectFormulaTree); ObjectFormulaTree secondFirstTree = new ObjectFormulaTree(secondFirstOperation, secondFirstList); secondList.Add(secondFirstTree); secondList.Add(fd[1]); ObjectFormulaTree secondTree = new ObjectFormulaTree(secondOperation, secondList); mainList.Add(secondTree); if (b[0] & b[1]) { return(ElementaryRealConstant.RealZero); } for (int i = 0; i < 2; i++) { if (b[i]) { return(mainList[1 - i]); } } return(new ObjectFormulaTree(mainOperation, mainList)); }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The function for derivation calculation</param> /// <param name="s">Derivation string</param> /// <returns>The derivation</returns> public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string s) { ElementaryRealConstant op = new ElementaryRealConstant(0); return(new ObjectFormulaTree(op, new List <ObjectFormulaTree>())); }