I'm trying to write a function that filters out non-real solutions returned by solve. For now, I'm using
def select_real(xs):
return map(lambda eq: eq.lhs() == eq.rhs().real(), filter(lambda eq: not bool(eq.rhs().imag()), xs))
and it works for some simple cases like this
sage: select_real(solve(x^3+8==0, x))
[x == -2]
but this assumes multiple things, like fact that the solve retuns list in the form of actual solutions and imaginary part can be calculated. Generally it is only an ugly hack that I don't like.
I looked at assume(x, 'real') but found out that it does not work ( Ticket #11941 ). I also tried to do check using "in RR", but in above case all 3 solutions gave False. Also at first I tried using eq.full_simplify() in map step above, but it turns out that simplifying returns different root than using real/imag:
sage: ((-8)^(1/3)).full_simplify()
-2^(1/3)
sage: ((-8)^(1/3)).real()
1
After all that, I'm out of ideas. What is considered the best way to obtain only real solutions? Thanks in advance.