Ask Your Question
3

problem with solve function

asked 2023-04-18 06:13:51 +0100

vyt gravatar image

updated 2023-04-19 07:29:07 +0100

Emmanuel Charpentier gravatar image

Solve function in SageMath produces some strange output while finding roots of a derivative of function (exp(x)*(1-x))^n/x^m. Entering the code below

var('x, m, n')
hh=(exp(x)*(1-x))^n/x^m
solve(hh.diff(x)==0,x)

produces output

(x, m, n)
[x^2 == (m*x^2 - m)/n]

What does `[x^2 == (m*x^2 - m)/n] is supposed to mean? By contrast Sympy quickly produces a correct easy to understand answer :

$$\left[ \frac{m - \sqrt{m \left(m - 4 n\right)}}{2 n}, \frac{m + \sqrt{m \left(m - 4 n\right)}}{2 n}\right]$$

edit retag flag offensive close merge delete

Comments

Edited for readability.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-19 07:27:47 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-04-19 07:52:11 +0100

Emmanuel Charpentier gravatar image

updated 2023-04-19 14:46:46 +0100

Step by step :

sage: var('x, m, n')
(x, m, n)
sage: hh=(exp(x)*(1-x))^n/x^m
sage: solve(hh.diff(x)==0,x)
[x^2 == (m*x^2 - m)/n]

That's an implicit solution. Sage's default solver (Maxima's) can be coaxed to find for a more explocit answer :

sage: solve(hh.diff(x)==0,x, to_poly_solve=True)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

Or you may use solve recursively) :

sage: solve(solve(hh.diff(x)==0,x),x)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

or

sage: solve(hh.diff(x)==0,x)[0].solve(x)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

But beware : you should check these vatious solutions.And be wary of simplify_full which may use simplifications validonly certain assumptions...

Left as an exercise for the reader...

HTH,

EDIT :

the final solution [x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))] that you obtained by repeated application of solve procedure is wrong, so this is the bug of "Maxima" after all.

Indeed :

sage: [Eq.subs(t).simplify_full().is_zero() for t in flatten([s.solve(x) for s in S1])]
[False, False]

This is now #35541

Workarounds :

sage: Ex.factor().solve(x)
[x == 1, x == 1/2*(m - sqrt(m^2 - 4*m*n))/n, x == 1/2*(m + sqrt(m^2 - 4*m*n))/n, x == 0]
sage: [Ex.subs(s).is_zero() for s in Ex.factor().solve(x)]
[True, True, True, True]
sage: Ex.solve(x, algorithm="giac")
[1/2*(m - sqrt(m^2 - 4*m*n))/n, 1/2*(m + sqrt(m^2 - 4*m*n))/n]
sage: [Ex.subs(x==s).is_zero() for s in Ex.solve(x, algorithm="giac")]
[True, True]
sage: [{v[1].sage():v[2].sage() for v in s} for s in mathematica.Solve(Ex==0, x)]
[{x: -1/2*(sqrt(m - 4*n)*sqrt(m) - m)/n},
 {x: 1/2*(sqrt(m - 4*n)*sqrt(m) + m)/n}]
sage: [Ex.subs(s).is_zero() for s in [{v[1].sage():v[2].sage() for v in s} for s in mathematica.Solve(Ex==0, x)]]
[True, True]

HTH,

edit flag offensive delete link more

Comments

Thank you for the clarification that the solution provided by "solve()" function uses "Maxima" and is supposed to be understood as an implicit solution! However, if this is the case then the final solution [x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))] that you obtained by repeated application of solve procedure is wrong, so this is the bug of "Maxima" after all.

vyt gravatar imagevyt ( 2023-04-19 08:30:43 +0100 )edit
1

answered 2023-04-19 05:53:39 +0100

tolga gravatar image

Simplifying the function may help:

var('x, m, n');
hh=(exp(x)*(1-x))^n/x^m
solve((hh.diff(x)).simplify_full()==0,x)
edit flag offensive delete link more

Comments

Thank you very much! Applying method "simplify_full()" produces expression that "solve" function can correctly solve. However, as a new user of SageMath I would like to ask a follow up question, as to what was the problem with the initial solution of the equation? Was the original answer [x^2 == (m*x^2 - m)/n] provided by solve() function incorrect and a result of a bug, or could it be interpreted to provide a correct answer?

vyt gravatar imagevyt ( 2023-04-19 06:30:16 +0100 )edit

Both in fact :

  • This has the form of an implicit answer, which I (incorrectly) tried to explicit.

  • It turns out that this implicit answer is wrong.

See the Github issue I cite in my answer's EDIT.

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-19 14:49:52 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2023-04-18 06:13:51 +0100

Seen: 244 times

Last updated: Apr 19 '23