How to tell to sagemath to solve for the good root

asked 2020-03-04 13:21:18 +0200

Cyrille gravatar image

Hello, this is a simple problem of optimization

%display latex

var('w, gamma, alpha, beta, h, theta, p, A, B'); U= w^(gamma); Pi = ph^(theta)- wh; N= (U)^alpha * (Pi-B)^beta show(U) show(Pi) show(N) show(N_w) N_w= factor(N.diff(w)) assume(0 < alpha <1) assume(0 < beta <1) z=solve(N_w==0, w, solution_dict=True) show(z)

If I say no bulshit, with the assumptions only one solution is valid. But Sagemath display all solutions ? How could it be done automaticaly ?

The factoriZation shows that

assume(0 < alpha <1) assume(0 < beta <1) z=solve(N_w==0, w, solution_dict=True)

edit retag flag offensive close merge delete



Hello, @Cyrille! I have some bad news: The assume statements can only be enforced when the solution is numerical. For example,


will return [x==2], ignoring the alternative solution $x=-2$, which doesn't satisfy the assumption. On the other hand, observe this example, which is should have no solution:

assume(x, 'real')

but Sage returns [x == -sqrt(b), x == sqrt(b)], which obviously violate the assumptions.

At first, I thought I had a form to avoid this problem (I even posted it as a comment); however, every time I test it, there is always a particular case that makes it fail. I even tried other approaches, but they also failed on a particular case.

dsejas gravatar imagedsejas ( 2020-03-04 17:08:34 +0200 )edit

I am afraid Sage is not clever (heuristic) enough to automatically choose the correct solution. A human should do it manually. If this is for students to do it, it should be a good exercise for them. If this is to automatically fill the solution on a book or article you are writing, you could write a small piece of code that makes the correct selection. However, remember, it is only guaranteed to work for this particular problem, and could fail to work if you change the assumptions or the equation itself, or if you add another equation.

Anyway, I am only posting this as a comment because it is not an answer to your question, and also it could be that somebody more clever and Sage-savvy than me actually finds a solution to your problem. I hope so!

dsejas gravatar imagedsejas ( 2020-03-05 21:38:23 +0200 )edit