confused_researcher
confused_researcher

Reputation: 17

sympy assumptions in nsolve not holding

I am trying to solve 2 simultaneous equations, which lead to two sets of x-y values. I only want the x-y set that has a positive x.

I set my assumption in x when defining it to be positive= True. Yet when I get my x-y solutions, I still only get the [-1.41421356237310], [2.00000000000000] set when I want [1.41421356237310], [2.00000000000000] set. It appears that my assumption to make x positive is not working, although the print((x).is_positive() returns True. Why is my assumption not working, and how do I force nsolve to assume that x must be positive? The code can be found below.

import sympy as sym

x = sym.symbols('x', positive= True)
y= sym.symbols('y', positive= True)
eq1= sym.Eq(y,x**2)
eq2= sym.Eq(y,2)
result= sym.nsolve((eq1, eq2), (x,y),(-1,-5))
print(result)
print((x).is_positive)

Upvotes: 0

Views: 66

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149135

You should use solve instead of nsolve, because only the former will use the assumptions on the variables:

...
result = sym.solve((eq1, eq2), (x, y))
print(result)

gives as expected:

[(sqrt(2), 2)]

(while you get [(-sqrt(2), 2), (sqrt(2), 2)] if you remove the positive=True assumption on x)

Upvotes: 2

Related Questions