-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Parse/evaluate expressions which are dependent on each other #791
Comments
There is no builtin way to do that. You would have to parse all statements into an expression tree with |
Sounds like a good feature for a plugin :) will work on this if i find the time |
Well, we recently introduced algebra functions |
Yup - ideally you can define an expression environment and The depdency tree can be built e.g. var env = math.parser() Or something like that :) |
Hey, I've actually implemented this idea and I'm interested in suggestions for the API. My idea was actually to implement a spreadsheet-like functional program using Math.js—multiple statements could be incorporated into a document and would be evaluated in order of dependency. The approach is as @Rex90 described—first a dependency graph is built, then the graph is evaluated starting from the leaves. My API is really simple right now: evalSystem(['x = 2 y', 'y = z', 'z = 4']) // = {0: '8', 1: '4', 2: '4' } or arbitrary keys can be assigned: evalSystem({'A1': 'x = 2 y', 'B2': 'y = z', 'C3': 'z = 4'}) // = {'A1': '8', 'B2': '4', 'C3': '4' } (There's a helper function and some small classes which build the dependency graph) So my questions are:
(The algorithm I used is borrowed from the Python-based spreadsheet Dirigible, specifically here and here) |
Nice one @caseygrun. I think it would be helpful to have it set up in such a way that the variables can be passed in without having to redefine the formulas; i.e. A scope that can be updated independently. I have implemented something like this myself as well using a js dependency graph library next to Mathjs. I haven't posted it because not well written :) |
@caseygrun that sounds really promising! Yes we definitely want to have a solver in math.js. Is the algorithm you use only suitable for a structure like About your API: shouldn't the first example with an Array as input return an array as output? |
It's only suitable for
Statement (1)'s RHS includes The re: API/arrays as output: Sure, I consider arrays and objects the same right now, it's just that the "keys" of an array are the indices. I'm more wondering 1) whether it makes sense to expose the whole thing as a separate plugin (or as part of the core library), and 2) if it would be better as a single function or as a sort of stageful object like @Rex90 describes. I like the idea of a stateful object, particularly for the spreadsheet-type application, because it would be nice to keep the dependency graph around if I could update it piecemeal as expressions are rewritten (rather than having to rebuild the whole thing). |
Thanks for your explanation. So if I understand it your current solution is sort of a "variable resolver", and indeed not an algebraic solver. For math.js I would indeed like to see an algebraic solver, which can also take a set of expressions into account like in your solution. This
|
On a related note, I've run into a serious performance issue with derivative. My Javascript neural network framework relies heavily on derivative for gradient descent and mathjs is struggling with expression parse trees of 5,000 nodes (!). I've separately implemented an Optimizer that memoizes sub expressions. https://github.com/firepick/kinann/blob/master/src/Optimizer.js Optimizer does actually tease out the dependencies of an expression and memoizes them for evaluation. I'm now actually contemplating writing a derivative method for Optimizer to see if I can get something to deal with my gnarly math expressions. I've nothing to report just yet, but wanted to bubble this up as an FYI about the problem I'm trying to tackle in this problem space. |
Thanks @firepick1. I guess there is room for performance improvements in the parser. Do you have any concrete ideas from your experience? If so, we could discuss them in a separate topic, you can simply open a new topic "Improve performance of derivative". |
New thread posted. :) I do have a hunch the dependency issue of this thread |
Related: #907 |
I've run into a similar issue. Here's the post I made: I have an application where certain equations rely on other equations. The ordering of the dependencies of equations is not known at run time. So I would like to be able to use a scope to recursively evaluate each expression without throwing errors. Right now I can't seem to figure this out and was curious if this feature is possible. For example, If I have in my scope:
And I want to evaluate the value of b like:
I expect to see the value 10, but instead I get:
Okay, not a huge deal. I can just run a while loop that stops when I see a number. But If I evaluate 'a + 3' in the same manor, I get the following error:
So again, to reiterate my question, is it possible to recursively evaluate expressions within the MathJS library? If so, please point out my mistake, and if not, perhaps this might be a feature to consider? |
👍 thanks for sharing here. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I'm not sure if I'm missing something here but is there a way to compile a set of expressions into a dependency tree?
Eg provide expressions
Y = 1 + X
X = 1/Z
Z = 5
Is there a way to eval these 3 expressions in the correct order such that mathjs doesnt complain about missing variables in the scope?
Thanks
Armen
The text was updated successfully, but these errors were encountered: