nicolewhite / algebra.js

Build, display, and solve algebraic equations.
http://algebra.js.org
MIT License
1.34k stars 111 forks source link

Possible issue solving 1/(x-3)=1 #43

Closed sonnyk22 closed 8 years ago

sonnyk22 commented 8 years ago

I am having an issue with getting any result back for the equation: 1/(x-3)=1 The result should be: x=4 but, the eq=algebrajs.parse('1/(x-3)=1') returns: -1/3 = 1 and, the eq.solveFor('x') returns: Invalid Argument (x): Variable does not exist in the equation. I even tried (1/(x-3))=1, but iIve gotten the same error message.

Would you be able to help me this issue?

timrach commented 8 years ago

The problem is that dividing by expressions is not allowed in the library. Trying to construct the equation by hand throws the approptiate Error:

var num = new Expression(1);
var denom = new Expression('x').subtract(3);
num.divide(denom); //TypeError: Invalid Argument (x - 3): Divisor must be of type Fraction or Integer.

When the parser constructs the equation, it tries to convert the expression x-3 to a fraction by taking the constant of the expression and building a fraction with it. In x-3 the constant is -3, so the parser converts x-3 to -3/1, which is why you get the behaviour you described.

I will have a look if an error can already be thrown in the parser, to prevent confusion.

nicolewhite commented 8 years ago

That's right, you can't divide by expressions (yet) because that leads to negative exponents, among other complications.

sonnyk22 commented 8 years ago

Thanks for getting back to on this. Is there in anyway I can format the equation behind the seen on my end, so that the AlgebraJS lib can solve it?

nicolewhite commented 8 years ago

Not yet, no. You're trying to do x ^ -1 which isn't supported.