Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - Get defined and undefined scope from expression #1414

Closed
sebastianfelipe opened this issue Feb 20, 2019 · 4 comments
Closed

Question - Get defined and undefined scope from expression #1414

sebastianfelipe opened this issue Feb 20, 2019 · 4 comments
Labels

Comments

@sebastianfelipe
Copy link

sebastianfelipe commented Feb 20, 2019

Hi,

I would like to make an expression, but also, get the variables in there. Parser has a method called getAll, to get every defined variable into an expression, but I would like to get every variable, defined and undefined inside an expression. For example, if the expression is (x + y)/z -> get ['x', 'y', 'z'] as variables or as scope {x: undefined, y: undefined, z: undefined} or null. Is there a method that I could use to get that result or similar?

Thanks,
Sebastián.

@josdejong
Copy link
Owner

Yes you can extract all symbols in an expression. If you parse an expression using math.parse, you get a node tree having methods like filter, map, traverse to iterate over it and for example find all symbol nodes. See docs (I think you want to use the filter method):

http://mathjs.org/docs/expressions/expression_trees.html

@sebastianfelipe
Copy link
Author

sebastianfelipe commented Feb 20, 2019

Perfect! Haven't seen that function. I made a recursive function to solve that, but I think filter is better. How can I you choose symbols (not functions) only? For example if a apply a log, then it appears it were a SymbolNode and I don't need it, just need the variables.

@sebastianfelipe
Copy link
Author

sebastianfelipe commented Feb 20, 2019

I think I have the answer, using the second parameter from filter called "path" it is possible to discriminate whether it is "fn" or not. Thanks!

@josdejong
Copy link
Owner

Yes indeed, or you can check whether the parent is a FunctionNode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants