Search code examples
pythonsympysolver

Exact solution from Sympy solve


Is there a way to filter Sympy solve to return only exact solutions and not expressions?

The example

import sympy as sp

x, y = sp.var('x,y',real=True);
l1, l2, l3 = sp.symbols('\lambda_1,\lambda_2,\lambda_3', real = True)

f = x**(sp.Rational(1/3))*y**(sp.Rational(2/3)) #define function
c = [x + y - 1, -x, -y]

L = f - l1*c[0] - l2*c[1] - l3*c[2]

gradL = sp.Matrix([sp.diff(L,c) for c in L.free_symbols])

stationary_points = sp.solve(gradL, L.free_symbols, dict=True)

solve returns:

[{\lambda_1: \lambda_3 + 0.666666666666667*x**(1/3)/y**(1/3),
  \lambda_2: \lambda_3 + 0.666666666666667*x**(1/3)/y**(1/3) - 0.333333333333333*y**(2/3)/x**(2/3)}]

Wereas I would have liked the empty dictionary, since this solution are not exact but are expressions.

I searched in Sympy documentation but I did not find anything.


Solution

  • The ability to detect linear subsets of systems of equations and make decisions based upon their solution is an open SymPy issue. For your case, you can help the system by requesting manual=True which will give [] as the solution.