Ask Your Question

Revision history [back]

I don't see a built-in method for this, but you could solve the equation which makes the imaginary part of the general solution zero:

sage: solutions = (exp(-2*x)+exp(2*x) == 2).solve(x,to_poly_solve=True)

sage: eq1 = solutions[0]
sage: eq1
x == I*pi*z29

sage: rhs = eq1.right()
sage: rhs
I*pi*z29

sage: rhs.imag()
pi*real_part(z29)

sage: rhs.variables()
(z29,)

sage: (rhs.imag() == 0).solve(var)
[z29 == 0]

This gives the value of z29 which makes the imaginary part zero; so now substitute this value into the original equation, eq1:

sage: solutions_2 = (rhs.imag() == 0).solve(var)
sage: eq2 = solutions_2[0]

sage: eq1
x == I*pi*z29
sage: eq2
z29 == 0

sage: eq1.substitute(eq2)
x == 0

This should work for any equation you start with; you could wrap this in a function if you need to call it repeatedly (if you do that, make sure to check that there is only one solution in solutions and solutions_2, otherwise you'll want to use those equations too).