fkleon / math-expressions

A library for parsing and evaluating mathematical expressions, supporting real numbers, vectors, and basic interval arithmetic.
https://pub.dev/packages/math_expressions
MIT License
102 stars 36 forks source link

Possible to extract variables from Expression? #73

Open herrytco opened 2 years ago

herrytco commented 2 years ago

For my project, I need to parse expressions and get a list of used symbols in that expression. This would look something like this:

String input = "x+y*z+3";
Expression e = Parser().parse(input);

List<String> symbols = e.symbols; // ["x", "y", "z"]

Is that possible with the current state? I couldn't find the function on my own.

fkleon commented 2 years ago

Hi @herrytco thanks for creating this issue. It's currently not possible to get a list of symbols/variables directly from the expression tree.

herrytco commented 2 years ago

Thank you for the answer. Than I fall back to my current strategy to do the following:

  1. try to evaluate the expression
  2. catch all errors related to unbound variables
  3. set the variable to a random value
  4. repeat 2-3 until the expression is evaluated
  5. return the list of symbols found
  6. profit
VittorioParagallo commented 3 months ago

any plan to implement this feature?

fkleon commented 2 months ago

any plan to implement this feature?

No, but feel free to open a PR if you're wanting to contribute this.