public MathFunc(MathFuncNode left, MathFuncNode right, VarNode variable = null, IEnumerable <ConstNode> parameters = null, bool simplify = true, bool calculateConstants = false) { LeftNode = left; RightNode = right; Root = RightNode; Variable = variable; if (Variable == null) { ConstToVars(); } if (parameters != null) { Parameters = parameters.Except(parameters.Where(p => p.Name == Variable.Name)).ToDictionary(node => node.Name); } FindParamsAndUnknownFuncs(Root); Root.Sort(); if (simplify) { Root = Simplify(Root); } if (calculateConstants) { Root = Precompile(null, Root); } }
public MathFunc(string str, string v = null, bool simplify = true, bool precompile = false) { if (!Helper.Parser.Parse(str)) { throw new Exception("Impossible to parse input string"); } LeftNode = Helper.Parser.FirstStatement.LeftNode; RightNode = Helper.Parser.FirstStatement.RightNode; Root = RightNode; if (string.IsNullOrEmpty(v)) { ConstToVars(); } else { Variable = new VarNode(v); ConstToVar(Root); } FindParamsAndUnknownFuncs(Root); Root.Sort(); if (simplify) { Root = Simplify(Root); } if (precompile) { Root = Precompile(null, Root); } }
public MathFunc(MathFuncNode root, VarNode variable = null, IEnumerable <ConstNode> parameters = null, bool calculateConstants = false, bool simplify = false) : this(new FuncNode("f", new List <MathFuncNode>() { variable }), root, variable, parameters, calculateConstants, simplify) { }
protected void GetFirstParam(MathFuncNode node) { if (Variable == null) { for (int i = 0; i < node.Childs.Count; i++) { if (Variable == null) { if (node.Childs[i].Type == MathNodeType.Constant) { Variable = new VarNode(node.Childs[i].Name); break; } else if (node.Childs[i].Type == MathNodeType.Variable) { Variable = (VarNode)node.Childs[i]; break; } else { GetFirstParam(node.Childs[i]); } } } } }
public MathFunc(string str, string v = null, bool simplify = true, bool precompile = false) { List <MathFunc> mathFuncs = new MathExprConverter().Convert(str); MathFunc first = mathFuncs.FirstOrDefault(); LeftNode = first.LeftNode; RightNode = first.RightNode; Root = RightNode; if (string.IsNullOrEmpty(v)) { ConstToVars(); } else { Variable = new VarNode(v); ConstToVar(Root); } FindParamsAndUnknownFuncs(Root); Root.Sort(); if (simplify) { Root = Simplify(Root); } if (precompile) { Root = Precompile(null, Root); } }
public void ConstToVars() { if (LeftNode.Type == MathNodeType.Variable) { Variable = (VarNode)LeftNode; } if (LeftNode.Type == MathNodeType.Constant) { Variable = new VarNode(LeftNode.Name); } else if (LeftNode.Type == MathNodeType.Function) { Variable = null; if (LeftNode.Childs.Count > 1 && ((FuncNode)LeftNode).Childs[1] != null) { var secondNode = ((FuncNode)LeftNode).Childs[1]; if (secondNode.Type == MathNodeType.Constant) { Variable = new VarNode(secondNode.Name); } else if (secondNode.Type == MathNodeType.Variable) { Variable = (VarNode)secondNode; } } GetFirstParam(RightNode); if (Variable == null) { Variable = new VarNode("x"); } } ConstToVar(Root); if (Root.Name == Variable.Name) { Root = Variable; } }
public void ConstToVars() { if (LeftNode is VarNode varNode) { Variable = varNode; } if (LeftNode is ConstNode constNode) { Variable = new VarNode(LeftNode.Name); } else if (LeftNode is FuncNode funcNode) { Variable = null; if (LeftNode.Children.Count > 1 && funcNode.Children[1] != null) { var secondNode = funcNode.Children[1]; if (secondNode is ConstNode) { Variable = new VarNode(secondNode.Name); } else if (secondNode is VarNode secondVarNode) { Variable = secondVarNode; } } GetFirstParam(RightNode); if (Variable == null) { Variable = new VarNode("x"); } } ConstToVar(Root); if (Root.Name == Variable.Name) { Root = Variable; } }
protected void GetFirstParam(MathFuncNode node) { for (int i = 0; i < node.Children.Count; i++) { if (Variable == null) { if (node.Children[i] is ConstNode) { Variable = new VarNode(node.Children[i].Name); break; } else if (node.Children[i] is VarNode varNode) { Variable = varNode; break; } else { GetFirstParam(node.Children[i]); } } } }