Menu

#3013 'solve' can depend on order (or presence) of variables given to solve

None
open
solve (51)
5
2016-10-05
2015-09-03
No

Problem with the 'solve' function - results/whether or not it returns anything dependent depend on the order (or presense) of the list of variables given to solve -- consider following MWE:

like : 28*log((1-s)*r)+18*log(s*(1-s)*r)+18*log(s*s*(1-s)*r)
  +436*log((1-s)*(1-r)+s*(1-s)*(1-r)+s*s*(1-s)*(1-r)+s*s*s);
dlike_s : diff(like,s)=0;
dlike_r : diff(like,r)=0;

If I try solve([dlike_s,dlike_r],[s,r]);

(%i4) solve([dlike_s,dlike_r],[s,r]);
(%o4) []

if I simply switch the order of the variables...

(%i5) solve([dlike_s,dlike_r],[r,s]);
(%o5) [[r = 50653/(454750+4000*sqrt(4021)),s = -(sqrt(4021)+5)/74],
       [r = -50653/(4000*sqrt(4021)-454750),s = (sqrt(4021)-5)/74]]

Further, if I leave the list of variables off altogether:

       (%i6) solve([dlike_s,dlike_r]);
(%o6) [[r = 50653/(454750+4000*sqrt(4021)),s = -(sqrt(4021)+5)/74],
       [r = -50653/(4000*sqrt(4021)-454750),s = (sqrt(4021)-5)/74]]

Discussion

  • johnny_canuck

    johnny_canuck - 2015-09-04

    Also, same is true if 'algsys' is used instead of 'solve'.

     
  • Robert Dodier

    Robert Dodier - 2016-05-12
    • labels: Order dependence in 'solve' --> solve
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,24 +1,27 @@
     Problem with the 'solve' function - results/whether or not it returns anything dependent  depend on the order (or presense) of the list of variables given to solve -- consider following MWE:
    
    +~~~~
     like : 28*log((1-s)*r)+18*log(s*(1-s)*r)+18*log(s*s*(1-s)*r)+436*log((1-s)*(1-r)+s*(1-s)*(1-r)+s*s*(1-s)*(1-r)+s*s*s);
     dlike_s : diff(like,s)=0;
     dlike_r : diff(like,r)=0;
    +~~~~
    
    -If I try solve([dlike_s,dlike_r],[s,r]);
    +If I try `solve([dlike_s,dlike_r],[s,r]);`
    
    +~~~~
     (%i4) solve([dlike_s,dlike_r],[s,r]);
     (%o4) []
    +~~~~
    
     if I simply switch the order of the variables...
    -
    +~~~~
     (%i5) solve([dlike_s,dlike_r],[r,s]);
     (%o5) [[r = 50653/(454750+4000*sqrt(4021)),s = -(sqrt(4021)+5)/74],
            [r = -50653/(4000*sqrt(4021)-454750),s = (sqrt(4021)-5)/74]]
    -       
    -       Further, if I leave the list of variables off altogether:
    -       
    +~~~~
    +Further, if I leave the list of variables off altogether:
    +~~~~       
            (%i6) solve([dlike_s,dlike_r]);
     (%o6) [[r = 50653/(454750+4000*sqrt(4021)),s = -(sqrt(4021)+5)/74],
            [r = -50653/(4000*sqrt(4021)-454750),s = (sqrt(4021)-5)/74]]
    -
    -
    +~~~~
    
     
  • Robert Dodier

    Robert Dodier - 2016-05-12
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,7 +1,8 @@
     Problem with the 'solve' function - results/whether or not it returns anything dependent  depend on the order (or presense) of the list of variables given to solve -- consider following MWE:
    
     ~~~~
    -like : 28*log((1-s)*r)+18*log(s*(1-s)*r)+18*log(s*s*(1-s)*r)+436*log((1-s)*(1-r)+s*(1-s)*(1-r)+s*s*(1-s)*(1-r)+s*s*s);
    +like : 28*log((1-s)*r)+18*log(s*(1-s)*r)+18*log(s*s*(1-s)*r)
    +  +436*log((1-s)*(1-r)+s*(1-s)*(1-r)+s*s*(1-s)*(1-r)+s*s*s);
     dlike_s : diff(like,s)=0;
     dlike_r : diff(like,r)=0;
     ~~~~
    
     
  • Robert Dodier

    Robert Dodier - 2016-05-12

    Another example from the mailing list 2016-05-11: "Solve command succeeds or fails based on order variables are specified in the argument"

    A: 2*y*z+2*x*z+2*x*y;
    V:x*y*z;
    
    e1: diff(V,x)=a*diff(A,x);
    e2: diff(V,y)=a*diff(A,y);
    e3: diff(V,z)=a*diff(A,z);
    e4: A=12;
    
    solve([e1,e2,e3,e4],[x,y,z,a]);
    []
    
    solve([e1,e2,e3,e4],[a,x,y,z]);
    [[a=-1/2^(3/2),x=-sqrt(2),y=-sqrt(2),z=-sqrt(2)],
     [a=1/2^(3/2),x=sqrt(2),y=sqrt(2),z=sqrt(2)]]
    
     
  • David Billinghurst

    • assigned_to: David Billinghurst
     
  • David Billinghurst

    The first is definitely an algsys problem. I will have a look.

    (%i1) display2d:false$
    
    (%i2) like : 28*log((1-s)*r)+18*log(s*(1-s)*r)+18*log(s*s*(1-s)*r)
      +436*log((1-s)*(1-r)+s*(1-s)*(1-r)+s*s*(1-s)*(1-r)+s*s*s)$
    
    (%i3) dlike_s : diff(like,s)=0$
    
    (%i4) dlike_r : diff(like,r)=0$
    
    (%i5) solve([dlike_s,dlike_r],[s,r]);
    
    (%o5) []
    (%i6) trace(algsys);
    
    (%o6) [algsys]
    (%i7) solve([dlike_s,dlike_r],[s,r]);
    
    1 Enter algsys
    [[1426*r*s^4-1362*r*s^3+((-118*r)+118)*s+54*r-54,500*r*s^3-500*r+64],[s,r]]
    1 Exit  algsys []
    (%o7) []
    (%i8) solve([dlike_r,dlike_s],[s,r]);
    
    1 Enter algsys
    [[500*r*s^3-500*r+64,1426*r*s^4-1362*r*s^3+((-118*r)+118)*s+54*r-54],[s,r]]
    1 Exit  algsys []
    (%o8) []
    (%i9) solve([dlike_r,dlike_s],[r,s]);
    
    1 Enter algsys
    [[500*r*s^3-500*r+64,1426*r*s^4-1362*r*s^3+((-118*r)+118)*s+54*r-54],[r,s]]
    1 Exit  algsys
    [[r = -(16*sqrt(4021)-1819)/11250,s = -(sqrt(4021)+5)/74],
     [r = (16*sqrt(4021)+1819)/11250,s = (sqrt(4021)-5)/74]]
    (%o9) [[r = -(16*sqrt(4021)-1819)/11250,s = -(sqrt(4021)+5)/74],
           [r = (16*sqrt(4021)+1819)/11250,s = (sqrt(4021)-5)/74]]
    
     
  • David Billinghurst

    I have simplified the original equations and changed variables, but the bug is still there.

    (%i1) eq1:125*x*y^3-125*x+16$
    (%i2) eq2:713*x*y^4-681*x*y^3+(-59)*x*y+59*y+27*x-27$
    
    (%i3) algsys([eq1,eq2],[x,y]);
                  16 sqrt(4021) - 1819        sqrt(4021) + 5
    (%o3) [[x = - --------------------, y = - --------------],
                         11250                      74
                                         16 sqrt(4021) + 1819      sqrt(4021) - 5
                                    [x = --------------------, y = --------------]]
                                                11250                    74
    
    (%i4) algsys([eq1,eq2],[y,x]);
    (%o4)                                 []
    

    I have traced the working solution through algsys and followed the process manually in maxima. Here is the manual workings for the first solution found by maxima at %o3 (see batch file bug3013.mac attached). It is the second solution in the list.

    (%i2) eq1:125*x*y^3-125*x+16
                                        3
    (%o2)                        125 x y  - 125 x + 16
    (%i3) eq2:713*x*y^4-681*x*y^3+(-59)*x*y+59*y+27*x-27
                           4          3
    (%o3)           713 x y  - 681 x y  - 59 x y + 59 y + 27 x - 27
    (%i4) r:resultant(eq1,eq2,x)
                                      4       3
    (%o4)                  - 109 (37 y  - 69 y  + 59 y - 27)
    
    /* Now solve resultant r for y
       r factors.  Solve rf1 and rf2 to give solutions Y1 and Y2.
     */
    
    (%i5) rf:factor(r)
                                        2      2
    (%o5)                  - 109 (y - 1)  (37 y  + 5 y - 27)
    (%i6) inpart(rf,2)
                                              2
    (%o6)                              (y - 1)
    (%i7) rf1:inpart(%,1)
    (%o7)                                y - 1
    (%i8) rf2:inpart(rf,3)
                                       2
    (%o8)                          37 y  + 5 y - 27
    
    /* One solution Y1 for rf1 and two solutions Y2 for rf2
       Y1 doesn't satisfy either eq1 or eq2.  Discard it
     */
    
    (%i9) Y1:solve(rf1,y)
    (%o9)                               [y = 1]
    (%i10) Y2:solve(rf2,y)
                             sqrt(4021) + 5      sqrt(4021) - 5
    (%o10)            [y = - --------------, y = --------------]
                                   74                  74
    (%i11) eq11:subst(Y1[1],eq1)
    (%o11)                                16
    (%i12) eq21:subst(Y1[1],eq2)
    (%o12)                                32
    
    /* Try solution Y2[1]
       - eq121: substitute Y2[1] into eq1
       - eq221: substitute Y2[1] into eq2
       - ratsimp them and remove constant factor
     */
    
    (%i13) eq121:subst(Y2[1],eq1)
                                              3
                          125 (sqrt(4021) + 5)  x
    (%o13)             (- -----------------------) - 125 x + 16
                                  405224
    (%i14) eq221:subst(Y2[1],eq2)
                               4                         3
           713 (sqrt(4021) + 5)  x   681 (sqrt(4021) + 5)  x
    (%o14) ----------------------- + -----------------------
                  29986576                   405224
                            59 (sqrt(4021) + 5) x          59 (sqrt(4021) + 5)
                          + --------------------- + 27 x - ------------------- - 27
                                     74                            74
    (%i15) eq122:subst(Y2[2],eq1)
                                             3
                         125 (sqrt(4021) - 5)  x
    (%o15)               ----------------------- - 125 x + 16
                                 405224
    (%i16) eq222:subst(Y2[2],eq2)
                                                           4
              59 (sqrt(4021) - 5) x    713 (sqrt(4021) - 5)  x
    (%o16) (- ---------------------) + -----------------------
                       74                     29986576
                                              3
                          681 (sqrt(4021) - 5)  x          59 (sqrt(4021) - 5)
                        - ----------------------- + 27 x + ------------------- - 27
                                  405224                           74
    (%i17) ratsimp(50653*eq122)
    (%o17)              (64000 sqrt(4021) - 7276000) x + 810448
    (%i18) eq122:ratsimp(%/16)
    (%o18)               (4000 sqrt(4021) - 454750) x + 50653
    (%i19) eq222:ratsimp(3748322*eq222)
    (%o19) (1991697750 - 36002250 sqrt(4021)) x + 2988527 sqrt(4021) - 116147329
    
    /* Try to solve eq122 and eq222 for x
       - resultant = 0 so solution exists
       - X222: solve eq222 for x
       - substitute into eq122.  Confirm == 0.
     */
    
    (%i20) r2:resultant(eq122,eq222,x)
    (%o20)                                 0
    (%i21) X222:solve(eq222,x)
                              2988527 sqrt(4021) - 116147329
    (%o21)              [x = --------------------------------]
                             36002250 sqrt(4021) - 1991697750
    (%i22) ev(X222:ratsimp(X222),algebraic = true)
                                   16 sqrt(4021) + 1819
    (%o22)                    [x = --------------------]
                                          11250
    (%i23) ev(E122:ratsimp(subst(X222,eq122)),algebraic = true)
    (%o23)                                 0
    
    /* This is the first solution.  Similarly for Y2[2] */
    
     

    Last edit: David Billinghurst 2016-10-05
  • David Billinghurst

    This is what maxima attempts for the failed case.
    1. algsys choses to eliminate y first
    2. we solve for x successfully
    3. substitute x into eq1 and eq2
    4. equations for y are cubic
    5. solutions are correct, but too complicated as maxima can't denest cube roots
    6. fail

    We could make progress by any of:
    1. chosing to first eliminate x. This is based on a heuristic and it may be possible to do better.
    2. denesting cube roots. Not trivial but the correct solution.
    3. switching to a numerical solution at some point. This is done if solve can't find a symbolic solution, but not when solve finds a "complicated" solution. Perhaps we could numerically evaluate constant expressions in presultant and do something - at least give a warning - if the result is close to zero.

    (%i2) eq1:125*x*y^3-125*x+16
                                        3
    (%o2)                        125 x y  - 125 x + 16
    (%i3) eq2:713*x*y^4-681*x*y^3+(-59)*x*y+59*y+27*x-27
                           4          3
    (%o3)           713 x y  - 681 x y  - 59 x y + 59 y + 27 x - 27
    
    /* Choose to eliminate y - (leastvars eq1 eq2 ) => eq1 y */
    
    (%i4) r:resultant(eq1,eq2,y)
                                3           2
    (%o4)             20720464 x  (2812500 x  - 909500 x + 50653)
    
    /* Factor resultant
       Discard conants and duplicates => rf1 and rf2 */
    
    (%i5) rf:factor(r)
                                3           2
    (%o5)             20720464 x  (2812500 x  - 909500 x + 50653)
    (%i6) rf1:inpart(rf,2)
                                           3
    (%o6)                                 x
    (%i7) rf1:inpart(rf,1)
    (%o7)                              20720464
    (%i8) rf2:inpart(rf,3)
                                      2
    (%o8)                    2812500 x  - 909500 x + 50653
    
    /* solve rf1 and rf2 for x
       X is ordered to match algsys */
    
    (%i9) X1:solve(rf1,x)
    (%o9)                                 []
    (%i10) ev(X1:ratsimp(X1),algebraic = true)
    (%o10)                                []
    (%i11) X2:solve(rf2,x)
                       16 sqrt(4021) - 1819      16 sqrt(4021) + 1819
    (%o11)      [x = - --------------------, x = --------------------]
                              11250                     11250
    (%i12) ev(X2:ratsimp(X2),algebraic = true)
                       16 sqrt(4021) - 1819      16 sqrt(4021) + 1819
    (%o12)      [x = - --------------------, x = --------------------]
                              11250                     11250
    (%i13) X:append(X1,reverse(X2))
                     16 sqrt(4021) + 1819        16 sqrt(4021) - 1819
    (%o13)      [x = --------------------, x = - --------------------]
                            11250                       11250
    
    /* Subst X[1] into eq1 and eq2
       Confirm it isn't a solution
       Don't reproduce.
     */
    
    /* Now subst X[2] into eq1 and eq2
       - solve for y
     */
    
    (%i14) eq12:subst(X[2],eq1)
                                          3
                  (16 sqrt(4021) - 1819) y     16 sqrt(4021) - 1819
    (%o14)     (- -------------------------) + -------------------- + 16
                             90                         90
    (%i15) eq22:subst(X[2],eq2)
                                          4                                3
              713 (16 sqrt(4021) - 1819) y     227 (16 sqrt(4021) - 1819) y
    (%o15) (- -----------------------------) + -----------------------------
                          11250                            3750
                 59 (16 sqrt(4021) - 1819) y          3 (16 sqrt(4021) - 1819)
               + --------------------------- + 59 y - ------------------------ - 27
                            11250                               1250
    
    /* Choose to work on eq12
       OK. degree(eq12)=3 < degree(eq22) = 4
     */
    
    (%i16) ev(eq12:ratsimp(eq12*90),algebraic = true)
                                            3
    (%o16)          (1819 - 16 sqrt(4021)) y  + 16 sqrt(4021) - 379
    (%i17) ev(eq22:ratsimp(eq22*11250),algebraic = true)
                                         4                                 3
    (%o17) (1296947 - 11408 sqrt(4021)) y  + (10896 sqrt(4021) - 1238739) y
                            + (944 sqrt(4021) + 556429) y - 432 sqrt(4021) - 254637
    
    (%i18) r2:resultant(eq22,eq12,y)
    (%o18)                                 0
    
    /* = 0.  Correct */
    
    (%i19) Y2:solve(eq12,y)
                                             1/3                           1/3
                sqrt(3) (16 sqrt(4021) - 379)    %i - (16 sqrt(4021) - 379)
    (%o19) [y = --------------------------------------------------------------, 
                                                         1/3
                                 2 (16 sqrt(4021) - 1819)
                                       1/3                           1/3
          sqrt(3) (16 sqrt(4021) - 379)    %i + (16 sqrt(4021) - 379)
    y = - --------------------------------------------------------------, 
                                                   1/3
                           2 (16 sqrt(4021) - 1819)
                             1/3
        (16 sqrt(4021) - 379)
    y = -------------------------]
                              1/3
        (16 sqrt(4021) - 1819)
    
    /* Here is the failure.
       The third root is solution we want, but we can't denest cube root 
       Later we will reject this solution */
    
    (%i20) rectform(float(Y2))
    (%o20) [y = 0.4622388881753429 - 0.8006212395538426 %i, 
          y = 0.8006212395538426 %i + 0.4622388881753429, y = - 0.9244777763506857]
    (%i21) float(-(sqrt(4021)+5)/74);
    (%o21)                        - 0.9244777763506861
    
     

    Last edit: David Billinghurst 2016-10-05
  • David Billinghurst

    Here is a reduced test case for the failure above. It should return the real solution of Y2 above.

    eq12:1819-16*sqrt(4021))*y^3+16*sqrt(4021)-379$
    eq22:1296947-11408*sqrt(4021))*y^4+(10896*sqrt(4021)-1238739)*y^3
              +(944*sqrt(4021)+556429)*y-432*sqrt(4021)-254637$
    algsys([eq12,eq22],[y]);
    
     

Log in to post a comment.