Replies: 16 comments
-
There is no builtin way to do that. You would have to parse all statements into an expression tree with |
Beta Was this translation helpful? Give feedback.
-
Sounds like a good feature for a plugin :) will work on this if i find the time |
Beta Was this translation helpful? Give feedback.
-
Well, we recently introduced algebra functions |
Beta Was this translation helpful? Give feedback.
-
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 :) |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
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 :) |
Beta Was this translation helpful? Give feedback.
-
@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? |
Beta Was this translation helpful? Give feedback.
-
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). |
Beta Was this translation helpful? Give feedback.
-
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
|
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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". |
Beta Was this translation helpful? Give feedback.
-
New thread posted. :) I do have a hunch the dependency issue of this thread |
Beta Was this translation helpful? Give feedback.
-
Related: #907 |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
👍 thanks for sharing here. |
Beta Was this translation helpful? Give feedback.
-
Hi, here is a method that currently does the trick regarding OP Instead of assigning values Y = 1 + X
X = 1 / Z
Z = 5 Assign functions and they will resolve in the correct order as long they are isolated Y() = 1 + X()
X() = 1 / Z()
Z() = 5
X() # 0.2
Y() # 1.2
Z() # 5 |
Beta Was this translation helpful? Give feedback.
-
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
Beta Was this translation helpful? Give feedback.
All reactions