nickme
nickme

Reputation: 47

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?

Upvotes: 1

Views: 443

Answers (2)

smichr
smichr

Reputation: 19145

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.

Upvotes: 0

xavc
xavc

Reputation: 1305

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

Upvotes: 1

Related Questions