jiggzson / nerdamer

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

Solving specific equation with long variable names hangs. #541

Closed cihancimen closed 4 years ago

cihancimen commented 4 years ago

Following code hangs on solveEquations(...) call.

const nerdamer = require('nerdamer/all')
nerdamer.setConstant("gross_margin_percentage_fcst_ly_var", 0.03)
nerdamer.setConstant("sales_dollars_fcst", 105224064.88000001)
nerdamer.setConstant("sales_dollars_ly", 95658240.80000001)
nerdamer.setConstant("sales_cost_ly", 23361379.300000004)

const equation = "gross_margin_percentage_fcst_ly_var=((sales_dollars_fcst-sales_cost_fcst) / sales_dollars_fcst) - ((sales_dollars_ly-sales_cost_ly) / sales_dollars_ly)"
const result = nerdamer.solveEquations(equation, "sales_cost_fcst")

However, if constant names are replaced shorter names, it returns a result.

nerdamer.setConstant("a", 0.03)
nerdamer.setConstant("b", 105224064.88000001)
nerdamer.setConstant("c", 95658240.80000001)
nerdamer.setConstant("d", 23361379.300000004)

const equation = "a=((b-e) / b) - ((c-d) / c)"
const result = nerdamer.solveEquations(equation, "e")

Is there any workaround other than renaming variables?

jiggzson commented 4 years ago

Is there any workaround other than renaming variables?

No need. I'll work on fixing this. The issue seems to be with integer factorization which hangs. For now I've just added a safety which just returns the unfactored number. The result should at worst be a correct but less simplified answer.

What perplexes me is the relation to long variable names. Let's leave this issue open until I find some time to looking into this some more. And thanks for reporting this.