Ask Your Question

giniu's profile - activity

2020-08-16 06:31:22 +0200 received badge  Good Question (source)
2019-11-30 09:46:29 +0200 received badge  Nice Question (source)
2017-02-14 14:29:46 +0200 received badge  Student (source)
2015-12-06 05:49:46 +0200 received badge  Famous Question (source)
2014-06-17 21:08:11 +0200 received badge  Notable Question (source)
2013-09-08 16:03:30 +0200 received badge  Popular Question (source)
2012-11-23 02:12:18 +0200 commented answer Assumptions

I got same when preparing materials for highschool students. Shouldn't bool in such case return a warning in addition to False? Current way of assuming False for Unknown makes things that are unknown impossible to distinguish from things that are truly false (and this can instead lead to confusion for younger users)

2012-09-09 16:24:14 +0200 received badge  Supporter (source)
2012-09-09 16:24:04 +0200 received badge  Editor (source)
2012-09-09 16:23:39 +0200 asked a question What is the best way to return only real solutions?

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.


edit:

I need such functionality because actually I'm preparing materials for high-schoolers who yet do not know about complex numbers, and shouldn't be introduced to them at that moment. My current version (above+extra full_simplify and uniq which are not really part of question) allows me to do for example this:

sage: var('x,a,b,c')                        
(x, a, b, c)
sage: assume(b^2-4*a*c>0)
sage: select_real(solve(a*x^2+b*x+c==0, x))
[x == -1/2*(b - sqrt(-4*a*c + b^2))/a, x == -1/2*(b + sqrt(-4*a*c + b^2))/a]
sage: forget(assumptions())
sage: assume(b^2-4*a*c==0)
sage: select_real(solve(a*x^2+b*x+c==0, x))
[x == -1/2*b/a]
sage: forget(assumptions())                
sage: assume(b^2-4*a*c<0)                  
sage: select_real(solve(a*x^2+b*x+c==0, x))
[]