# bool returns false with arcsin(x) and 2*arctan(x/(1+sqrt(1-x^2)))

The two expression should be equal but when I write bool(arcsin(x) == 2*arctan(x/(1+sqrt(1-x^2)))) it returns false.

Any clue - Why it does that? Any get around?

edit retag close merge delete

Sort by » oldest newest most voted

I can answer the "why it does that" question, anyway: Sage follows Maxima's conventions on equality testing. That is, it only returns True if it's convinced the equality holds. "False" here translates as "False or unknown".

I actually couldn't find a good workaround-- I'm not sure what the right way to fake new rewrite rules is. I guess you could confirm that the lhs and rhs agree via something like this:

sage: eq = arcsin(x) == 2*arctan(x/(sqrt(-x*x+1)+1))
sage: bool(eq)
False
sage: # but..
sage: bool(eq.subs(x=0))
True
sage: bool(diff(eq, x))
True


And if you had several cases where the "agree at one point and derivatives equal" trick works, you could put it in a function, maybe with something like this:

Warning: untested code! Will probably break when given unexpected input!


def tryharder_bool(some_expr):
# hack hack hack
if some_expr:
return True
if type(some_expr) == Expression and some_expr.is_relational():
v = some_expr.variables()
if len(v) == 1:
if some_expr.subs({v[0]:0}) and diff(some_expr, v[0]):
return True
return False


giving

sage: bool(eq)
False
sage: tryharder_bool(eq)
True


UPDATE:

For some of the other cases mentioned in the comments, playing around with the trig_* functions and/or converting to exponential form works:

sage: eq1 = arcsin(x) == 2*arctan(x/(sqrt(-x*x+1)+1))
sage: eq2 = sin(x) == 2*sin(x/2)*cos(x/2)
sage: eq3 = sin(2*x) == 2*sin(x)*cos(x)
sage: eq4 = tan(x/2) == sin(x)/(1+cos(x))
sage: eqs = eq1, eq2, eq3, eq4
sage: [bool(q) for q in eqs]
[False, False, True, False]
sage: [bool(q.trig_reduce()) for q in eqs]
[False, True, True, False]
sage: [bool(q.trig_expand()) for q in eqs]
[False, False, True, False]
sage: [bool(q.trig_expand(half_angles=True)) for q in eqs]
[False, False, True, True]
sage:
sage:
sage: eq2.maxima_methods().exponentialize()
1/2*I*e^(-I*x) - 1/2*I*e^(I*x) == 1/2*(I*e^(-1/2*I*x) - I*e^(1/2*I*x))*(e^(-1/2*I*x) + e^(1/2*I*x))
sage: eq1.maxima_methods().exponentialize()
arcsin(x) == 2*arctan(x/(sqrt(-x^2 + 1) + 1))
sage: [bool(q.maxima_methods().exponentialize()) for q in eqs]
[False, True, True, True]
sage:
sage: def trig_xeq_check(eq):
....:         return bool(eq) or bool(eq.maxima_methods().exponentialize()) or (bool(eq.subs(x=0)) and bool(diff(eq,x)))
....:
sage:
sage: [trig_xeq_check(q) for q in eqs]
[True, True, True, True]

more

thanks for your answer. I liked you workaround. Just one thing, to find the point at which it agree can I use y=find_root(eq, -1000, 1000) and use that y.

( 2010-11-15 12:00:23 +0200 )edit

If the two expressions are really equal then they should agree everywhere, so it doesn't matter what point you choose. I'd avoid looking for a root, though: (1) it's slower, (2) it's generally a good idea to stay symbolic (i.e. not floating-point) as long as possible, and (3) there might not even be a root to find..

( 2010-11-15 12:38:42 +0200 )edit

What if the two functions are not defined at the point I choose. e.g. in the above equation if I use bool(eq.subs(x=2)) It will return "False"

( 2010-11-15 17:42:19 +0200 )edit

Then depending on the function, it could crash, give you "nan == nan" which is False, or maybe even return True (if the function is undefined but evaluating the expression gave Infinity on both sides, for example). I'm afraid you're going to have to consider the functions involved.

( 2010-11-15 18:50:04 +0200 )edit

Do you have any idea to work around for bool( sin(x) == 2*sin(x/2)*cos(x/2) ) ? The differentiation equality check also fails in sage for this one.

( 2010-11-16 20:17:21 +0200 )edit