简体   繁体   中英

Solve algebraic equation in SymPy

I am trying to solve simple algebraic parametrized equation sqrt(x)=sqrt(a)+1 with SymPy command solveset :

x, a = symbols("x a")
solveset(Eq(sqrt(x), sqrt(a)+1), x)

It returns as part of answer (conditional set) two solutions: 2*sqrt(a)+a+1 and -2*sqrt(a)+a+1 . I can't understand how we can obtain the second one with minus sign from our equation!

Command solve in the same time gives us only solution (sqrt(a) + 1)**2 , which is correct in my opinion.

What happens with solveset , any ideas?

The issue is to do with the sets you are solving over. Specifically, solveset defaults to a complex domain, resulting in extraneous solutions due to the complex square root. But by explicitly specifying a real domain and defining a and x to both be positive (as we're just dealing with reals, so can't have a or x negative), the desired solution can be extracted:

x, a = symbols('x a', positive=True)
solveset(Eq(sqrt(x), sqrt(a) + 1), x, domain=S.Reals).as_relational(x).expand()
# x = 2⋅√a + a + 1

The method that solveset uses gives two roots to consider but checksol is not able to show that the 2nd solution is not generally valid so to be safe, it returns both and shows you the condition which must be checked. If you substitute a value in for a you will obtain a set with the single concrete answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM