2

I have been trying in vain to equate a linear equation to zero.

I am using Math.js library to perform a first order differential on an equation I provided:

const derivative = math.derivative(equation,"x");

This provides me with the following:

(0.10471975511965977 * (x * -2 / 3 + 72) - 0.06981317007977317 * x) * 2025 / 3.37496e+5

I want to equate this to zero, to find x.

I have tried using Algebra.js but the parser seems have an issue with negative numbers (see https://github.com/nicolewhite/algebra.js/issues/88)

Is there any way I can do this (with preferably a library).

Julius Knafl
  • 429
  • 3
  • 14

2 Answers2

1

I'd avoid going via the string representation, and instead turn a Math.js expression tree into an Algebra.js expression tree via recursive transformation. Would be easier if the Math.js expressions were to support a visitor pattern, but you could either add that to the library by extending the corresponding prototypes, or use a bunch of case distinctions instead. Most likely handling 3 or 4 node types should be enough: OperatorNode, SymbolNode, ConstantNode, perhaps also ParenthesisNode.

MvG
  • 57,380
  • 22
  • 148
  • 276
1

You can give nerdamer a go for this, especially since you prefer a library. See the snippet. Full disclosure, I wrote nerdamer. Hopefully it helps.

var ans = nerdamer.solve('(0.10471975511965977 * (x * -2 / 3 + 72) - 0.06981317007977317 * x) * 2025 / 3.37496e+5', 'x');
console.log(ans.toString());
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/nerdamer.core.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Algebra.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Calculus.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Solve.js"></script>
jiggzson
  • 515
  • 1
  • 4
  • 13