jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
514 stars 82 forks source link

How to build nested functions in nerdamer? #582

Closed MAGNETO903 closed 3 years ago

MAGNETO903 commented 3 years ago

I have 2 simple functions: f1(x)=2*x f2(x)=x^2

I want to make a function f3 that: f3(x) = f1(f2(x)) = f1(x^2) = 2x^2

I type in nerdamer:

nerdamer.setFunction("a", ["x"], "2*x")
nerdamer.setFunction("b", ["x"], "x^2")

nerdamer("a(b(x))").text() // return 2*x instead of 2x^2

nerdamer.setFunction("c", ["x"], "a(b(x))"]
nerdamer("c(x)") // also 2*x instead of 2x^2
jiggzson commented 3 years ago

@MAGNETO903, I suspect that the problem is related to confusion regarding the variable names. If you do,

nerdamer.setFunction("a", ["y"], "2*y");
nerdamer.setFunction("b", [], "x^2");

var ans = nerdamer("a(b(x))").text();
console.log(ans); // 2*x^2

it seems to give the expected result. Ironically I remember this not being the problem in past versions so I'm not sure when this broke. I'll take a look at this. Thanks.

MAGNETO903 commented 3 years ago

Ok!)

jiggzson commented 3 years ago

@MAGNETO903, this issue should be fixed.