nerdamer icon indicating copy to clipboard operation
nerdamer copied to clipboard

Pending issues with solve

Open Happypig375 opened this issue 6 years ago • 44 comments

~``` solve((x-1)(x+1)x=3x,x) => [(-1/3)(-1)^(1/6)24^(1/3)sqrt(3)^(1/3)-4(-1)^(-1/6)24^(-1/3)sqrt(3)^(-1/3), (1/6)(-1)^(1/6)(1+isqrt(3))24^(1/3)sqrt(3)^(1/3)+2((-1)^(1/6)24^(1/3)sqrt(3)^(1/3))^(-1)(-isqrt(3)+1), (1/6)(-1)^(1/6)(-i*sqrt(3)+1)*24^(1/3)sqrt(3)^(1/3)+2((-1)^(1/6)24^(1/3)sqrt(3)^(1/3))^(-1)(1+isqrt(3))] should=> [2,-2,0]~

Fixed. Adding test.

Happypig375 avatar Nov 22 '17 16:11 Happypig375

~solve(sqrt(x)+sqrt(2x+1)=5,x) => []~ should=> [4]

Happypig375 avatar Nov 22 '17 16:11 Happypig375

@Happypig375 Thanks. Can you start closing off some of the issues that are fixed?

jiggzson avatar Nov 22 '17 17:11 jiggzson

~solve(x=2/(3-x),x)~ => [1,1.0000000000000002] (@0.7.13) => [2*(-x+3)^(-1)] (@0.7.14) should=> [1,2]

Happypig375 avatar Nov 26 '17 05:11 Happypig375

solveEquations([sqrt(2)a-sqrt(2)c+b+d=0,a+c+sqrt(2)b-sqrt(2)d=0,a+c=0,b+d=1]) => Error: System must contain all linear equations!

All four equations are linear, but multivariate.

Happypig375 avatar Nov 30 '17 16:11 Happypig375

@Happypig375 so which variable are you trying to solve for?

jiggzson avatar Nov 30 '17 16:11 jiggzson

@jiggzson All of them. Currently solveEquations([a+b=1,a-b=2]) => [a,1.5,b,-0.5]. Therefore, solveEquations([sqrt(2)a-sqrt(2)c+b+d=0,a+c+sqrt(2)b-sqrt(2)d=0,a+c=0,b+d=1]) should=> [a,-1/(2 sqrt(2)), b, 1/2, c, 1/(2 sqrt(2)), d, 1/2]

Happypig375 avatar Dec 01 '17 03:12 Happypig375

Also, I think that solveEquations([a+b=1,a-b=2]) should=> [a=1.5,b=-0.5] This is more clear about the connection between the unknown and the value.

Happypig375 avatar Dec 01 '17 03:12 Happypig375

Oops. I confused it with solve for a moment. You're right. Something is causing it to incorrectly flag it as non-linear. I'll look into this.

jiggzson avatar Dec 01 '17 20:12 jiggzson

~solve(sqrt(x)-1,x) => [] should=> [1]~ ~solve(sqrt(x)+1,x) => [0] should=> []~

Happypig375 avatar Dec 03 '17 03:12 Happypig375

~solve(sqrt(x^2+1),x) => [] should=> [i, -i] solve(sqrt(x^2-1),x) => [] should=> [1, -1]~

Happypig375 avatar Dec 03 '17 06:12 Happypig375

The second argument to solve (variable to solve for) should be inferred for univariate polynomials/equations, and solve for all variables in multivariate equations like in solveEquations.

Happypig375 avatar Dec 03 '17 06:12 Happypig375

Cross-ref: #345

Happypig375 avatar Dec 10 '17 05:12 Happypig375

~~solve(((x+1)*((x+1)+1))/2=n,x) Expected: +/-(sqrt(8n+1)+3)/2 Gives: +/-(sqrt(4n+1)+3)/2~~ Fixed!

jiggzson avatar Dec 10 '17 15:12 jiggzson

solve(x^x^x^2018=2018,x) => [] should=> [2018^(1/2018)]

See https://youtu.be/UNwhaN2s2ww

Happypig375 avatar Jan 01 '18 09:01 Happypig375

~solve(sqrt(10x+186)=x+9,x) => [7.000000000000002,7,6.999999999999999] should=> [7]~

Happypig375 avatar Jan 01 '18 11:01 Happypig375

~solve(x^3+8=x^2+6,x) eval=> [−0.9999999999999996,0.9999999999999999⋅i+1,−0.9999999999999999⋅i+1] should=> [-1,1+i,1-i]~

Happypig375 avatar Jan 01 '18 13:01 Happypig375

Also, the solve function itself looks like the variable x is assumed when the 2nd argument is not provided.

  1. The minimum number of arguments needed to pass to solve should be 1 instead of 2.
  2. If the equation given is in one unknown, then that unknown should be assumed the variable to solve, and return to using x if there are multiple variables (or throw).

Happypig375 avatar Jan 01 '18 13:01 Happypig375

Missing active label here 😃

Happypig375 avatar Jan 04 '18 09:01 Happypig375

The minimum number of arguments needed to pass to solve should be 1 instead of 2.

I'll fix this. Additionally I've extended the symbolic range of Solve but solve is in dire need of unit tests.

jiggzson avatar Jan 11 '18 12:01 jiggzson

@Happypig375, automatically assuming the variable won't work at the moment since solve is used for various cases including systems of equations. I'm planning to refactor Solve in the near future so I'll go ahead and take care of it then.

jiggzson avatar Jan 13 '18 04:01 jiggzson

Absolutely wrong answer by solve: ~solve(x^3-10x^2+31x-30,x)~ should return [2,3,5]

Happypig375 avatar Jan 14 '18 09:01 Happypig375

~~Absolutely wrong answer by solve:~~

Have you verified the answer? The result you get is: ~~A. Ugly~~ B. Needlessly complex C. 100% correct The problem is that nerdamer does not simplify complex numbers efficiently and that is something I'm currently working on. If I solve it numerically I get the right answer right away. Would defaulting to that solve your problem? The downside is that you don't get nice algebraic answers anymore whenever possible.

jiggzson avatar Jan 14 '18 17:01 jiggzson

Have you verified the answer? The result you get is: A. Ugly B. Needlessly complex C. 100% correct

A. Evaluate can't simplify it B. The length exceeds Wolfram Alpha's input limit

How can I verify the answer when I don't even know what to do with it?

If I solve it numerically I get the right answer right away.

Ok

Would defaulting to that solve your problem? The downside is that you don't get nice algebraic answers anymore whenever possible.

Just fix this but don't change the defaults.

Happypig375 avatar Jan 15 '18 03:01 Happypig375

@Happypig375, fixing this is a little more involved than it might appear. One might be tempted to solve this numerically but then you end up with a tiny imaginary part for all the answers which is not ideal. Not to mention that the real part will now have rounding errors. Until I can successfully simplify [(-1/3)*(-20+18*i*sqrt(3))^(1/3)*2^(-1/3)+(-7/3)*(-20+18*i*sqrt(3))^(-1/3)*2^(1/3)+10/3,(1/6)*(-20+18*i*sqrt(3))^(1/3)*(1+i*sqrt(3))*2^(-1/3)+(7/6)*((-20+18*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(-i*sqrt(3)+1)+10/3,(1/6)*(-20+18*i*sqrt(3))^(1/3)*(-i*sqrt(3)+1)*2^(-1/3)+(7/6)*((-20+18*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(1+i*sqrt(3))+10/3] algebraically, I've decided to take the following approach.

  • Check if all coefficients are numeric.
  • Solve the polynomial
  • Check if all the roots are integers. If they are then return that answer. If they aren't then solve it algebraically.

The unfortunate part is that I now solve twice but that'll have to do until I find the time to simplify the algebraic solution above.

jiggzson avatar Jan 15 '18 19:01 jiggzson

I think that this can easily solved by implementing cube root for complex numbers. Ideas?

Happypig375 avatar Jan 16 '18 09:01 Happypig375

I think that this can easily solved by implementing cube root for complex numbers. Ideas?

That's what I initially thought myself. I implemented nth root for complex numbers which might still need some final touch-ups but when doing this one of the roots becomes 0.000000000000000221377340243793938154443⋅i+1.999999999999999555910790149937482438160. This has rounding errors in both the real and imaginary part. I can solve this algebraically at which point this error goes away but that requires a bit more work. I eventually hope to get to that at some point but I won't have the time now.

jiggzson avatar Jan 16 '18 13:01 jiggzson

I think I saw where the rounding error comes from - using Math.sin and Math.cos inside Parser.pow. The conversion between Frac and Javascript Number is what causes the error.

Happypig375 avatar Jan 16 '18 14:01 Happypig375

The conversion between Frac and Javascript Number is what causes the error.

I think that exacerbates the problem but doesn't eliminate it from what I remember. I'll give it a go later so let's see. Additionally, I still need to work on eliminating rounding errors in pow.

jiggzson avatar Jan 16 '18 14:01 jiggzson

The workaround earlier is not useful when fractional answers are there... ~solve(8x^3-26x^2+3x+9,x) should=> [3,3/4,-1/2] It currently produces [(-1/24)*(-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3)+(-151/6)*(-13984+15120*i*sqrt(3))^(-1/3)*2^(1/3)+13/12,(1/48)*(-13984+15120*i*sqrt(3))^(1/3)*(1+i*sqrt(3))*2^(-1/3)+(151/12)*((-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(-i*sqrt(3)+1)+13/12,(1/48)*(-13984+15120*i*sqrt(3))^(1/3)*(-i*sqrt(3)+1)*2^(-1/3)+(151/12)*((-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(1+i*sqrt(3))+13/12].~

Another bug is present here. When evaluate is checked, the vector does not evaluate automatically, but evaluates when vecget one of the solutions. Aka solve(8x^3-26x^2+3x+9,x) eval=> [(-1/24)*(-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3)+(-151/6)*(-13984+15120*i*sqrt(3))^(-1/3)*2^(1/3)+13/12,(1/48)*(-13984+15120*i*sqrt(3))^(1/3)*(1+i*sqrt(3))*2^(-1/3)+(151/12)*((-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(-i*sqrt(3)+1)+13/12,(1/48)*(-13984+15120*i*sqrt(3))^(1/3)*(-i*sqrt(3)+1)*2^(-1/3)+(151/12)*((-13984+15120*i*sqrt(3))^(1/3)*2^(-1/3))^(-1)*(1+i*sqrt(3))+13/12]

vecget(solve(8x^3-26x^2+3x+9,x),0) eval=> 1.0515041619631423e+33/754813980813027500i-300239975158033/150119987579016

Really. The rounding errors are important to fix.

Happypig375 avatar Jan 24 '18 11:01 Happypig375

Also this. ~solve(x^3-1/2x^2-13/2x-3,x) => [(-1/3)*((315/4)*i*sqrt(3)-221/2)^(1/3)*2^(-1/3)+(-79/12)*((315/4)*i*sqrt(3)-221/2)^(-1/3)*2^(1/3)+1/6,(1/6)*((315/4)*i*sqrt(3)-221/2)^(1/3)*(1+i*sqrt(3))*2^(-1/3)+(79/24)*(((315/4)*i*sqrt(3)-221/2)^(1/3)*2^(-1/3))^(-1)*(-i*sqrt(3)+1)+1/6,(1/6)*((315/4)*i*sqrt(3)-221/2)^(1/3)*(-i*sqrt(3)+1)*2^(-1/3)+(79/24)*(((315/4)*i*sqrt(3)-221/2)^(1/3)*2^(-1/3))^(-1)*(1+i*sqrt(3))+1/6] should=> [-1/2,3,-2]~

Happypig375 avatar Jan 24 '18 11:01 Happypig375