-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Algebraic differentiation #386
Conversation
…te, with a few functions as well. Still does not handle argument errors well, even though it returns the same it received.
…s, and parse to test against.
Thanks, very cool. I'm not sure what you mean with your first point. As for the tests: from unit test perspective it is best to write out the actual Nodes like you did: have as less indirections as possible. But it's quite cumbersome to write the nodes out, and I think we should be pragmatic too and I'm ok with testing against |
To see what I mean take a look at the commented out test cases. I think the problem is that I am only returning the nodes, and not actually running the functions (e.g. calling 'Array, Object, Object': function (args, math, scope) {
var fnScope = Object.create(scope);
var fnCode = derivative.apply(null, args).compile();
return function (x) {
fnScope[variable] = x;
return fnCode.eval(fnScope);
};
}
}); in an attempt to mimic |
After that, how can I/is it possible to get the nodes from this function? |
For these functions for which you've implemented the derivative, like I'm not sure what you mean with "get the nodes from this function". Do you mean the |
I'm not explaining my point well. The problem is that f(x) = derivative(2x + 3x, x) // should return a function (I think),
// based on the nodes of the evaluated (2 + 3)
f(1) // should print 5 but instead of printing 5, it just prints the OperatorNode.
No I mean this. I was trying to use this so that
I still want to be able to test based on the Nodes, even though this will need to provide numeric answers at times (which I think requires returning a function). So I want to be able to still get the backend Nodes. |
Ah, I get your point. We should think this through.
I think the real power lies in the first option: being able to get the derivative of a symbolic expression (in the form of a node tree) as an expression, like It may be a good idea to scetch some usage scenarios. Here some first ideas: // JS API
// symbolic computation
var f = math.parse('2x^3');
var df = math.derivative(f, 'x');
// math.derivative could also allow to pass a string,
// which will then first call math.parse
console.log('f', f.toString()); // 2x^3
console.log('df', df.toString()); // 6x^2
// evaluation
var x = 2;
var y = f.compile().eval({x: x}); // 16
var dy = df.compile().eval(x: x}); // 24
// how about passing a FunctionNode:
var f = math.parse('f(x) = 2x^3');
var df = math.derivative(f);
// derivative can know the variable x from the FunctionNode,
// and returns a FunctionNode of the derivative?
var fCompiled = f.compile().eval(); // compile into an executable function
var dfCompiled = df.compile().eval(); // compile into an executable function
// evalutation
var y = fCompiled(x); // 16
var dy = dfCompiled(x); // 24 The expression parser currently doesn't have any equipment to work with symbolic variables.
|
Most of what you said seemed fine, but I'm just unsure why this should be any different than everything else. Why should Also I somewhat disagree with:
as In short, I think |
What usage scenarios do you have in mind? If all you want to support is getting the result of |
… Added 'C^x' and FunctionAssignmentNodes.
… some error cases.
Oh wow, I thought I replied a long time ago. Something like a convergence test would suffice, but I more thinking about consistency with Math Notepad. If this isn't that important, then I'm fine with leaving it returning Nodes since it seems like it is not possible to decompile the functions back to Nodes, and a (non-Math Notepad) user can always compile it to a function if they want that; I was under a potential misapprehension that you could decompile because plot works with functions. If there are any style things let me know, I think I'm going to try to lower the character amount since there is a bit of redundant code here. |
… a variable base log, and added a test case. Adjusted the chain rule to be after the switch statement (saves ~1640 bytes and is more modular).
I adjusted it now. It's ready to be merged passing inspection. |
Ow, I just cleaned up the Good to hear from you :) I will review your code tomorrow. Returning Nodes has my preference, and an algebraic solution is just soo cool. No worries about Math Notepad: it has to adapt to math.js, not the other way around. |
The PR was trivial to redo lol |
thanks you're a pro ;) |
@josdejong I've added some comments and test cases, no code though. Just giving you a heads up. |
Thanks for the update @BigFav. |
Still has some work left, but a large portion is complete (finally got back from vacation). I have a few questions:
derivative
to be the function of the expression returned?math.parse
or the written out nodes. Is there a preference?Any comments are welcome (this PR is mostly meant for feedback).