jiggzson / nerdamer

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

Issue solving x^3+y^3=3 #594

Closed markhats closed 3 years ago

markhats commented 3 years ago

Hello

I noticed a problem with solving equations of a form similar to:

x^3+y^3=3

Running the following command:

solve(x^3+y^3=3,y)

gives an output of:

[ (-1/3)*(-27*abs(-3+x^3)-81+27*x^3)^(1/3)*2^(-1/3), 
  (1/6)*(-27*abs(-3+x^3)-81+27*x^3)^(1/3)*(1+i*sqrt(3))*2^(-1/3), 
  (1/6)*(-27*abs(-3+x^3)-81+27*x^3)^(1/3)*(-i*sqrt(3)+1)*2^(-1/3) ]

I would expect an output more like:

[ (3 - x^3)^(1/3),
  -((-1)^(1/3) (3 - x^3)^(1/3)),
  (-1)^(2/3) (3 - x^3)^(1/3) ]

Let me know if you need any more info. TBH getting the real-valued root correct would be enough for me. Thanks.

jiggzson commented 3 years ago

@markhats, I know that the solutions looks a bit hairy but they're based on the cubic equation.

TBH getting the real-valued root correct would be enough for me.

Reading that gave me the impression that the solutions where incorrect so I did a quick test.

// First solve for y
var sol = nerdamer('x^3+y^3=3').solveFor('y');

// Convert it all to numbers
var numsol = sol.map(function(solution) {
    return nerdamer(solution, {x: 1}, 'numer').text();
});

// Cube all the numbers. Since x=1, y should be 2
var cubed = numsol.map(function(n) {
    return nerdamer(n).pow(3).evaluate().text();
});

console.log(cubed); // [ '2.000000000000000329', '-1.2865e-15*i+2', '1.2865e-15*i+2' ]

You can see some floating point and rounding errors but the answers are generally correct. With v2.0, I'm hoping to eliminate or at the very least, greatly reduce these errors.

If you have any suggestions as to how to simplify the algebraic solutions, I'm all ears.

Edit: This is just one point of course, so if you know of any points that cause an error, please let me know. As I mentioned, the test above is a quick test.

markhats commented 3 years ago

Hi @jiggzson

Thanks for the reply. I've looked into it a bit more. I think the problem I'm having is due to the solution

(-1/3)*(-27*abs(-3+x^3)-81+27*x^3)^(1/3)*2^(-1/3)

evaluating to zero for all values of x > √3. You can see this with your above test. If you substitute in x=2 rather than x=1, then the result is zero.

Happy to help in any way I can, although I'm by no means an expert on CAS. To me, the obvious form of the solution to

x^3+y^3=3

is

y=(3 - x^3)^(1/3)

but I'm guessing that nerdamer has to handle the much more general case.

jiggzson commented 3 years ago

evaluating to zero for all values of x > √3.

Oh man. I see. @markhats. Let me look into it.

jiggzson commented 3 years ago

@markhats, this should be fixed. The fix is currently sitting on the dev branch. So the solution doesn't quite come in the form that you're looking for but I noticed that if you simplify the answers that you do get it in the form you're looking for.

Edit: Disregard. I just realized that simplify gives the wrong answer. This isn't related to the solutions for your issue. I've created an entry for that issue.

markhats commented 3 years ago

Sorry @jiggzson - missed this update. All seems to be working great now!

Many thanks.

Edit: Did you mean to delete line 700 in Solve.js? https://github.com/jiggzson/nerdamer/blob/master/Solve.js#L700

jiggzson commented 3 years ago

@markhats, no worries. I just updated the cubic equation on dev because it still had an issue and that should be fixed now. I was rushing to wrap everything up so I forgot to delete that line 😆. Thanks for pointing it out.