Stef1611
Stef1611

Reputation: 2387

Problem to solve sympy symbolic equation with tanh

I write the following equation my_Eq. a and b are reals.

a,b=sp.symbols('a,b',real=True)
my_Eq=sp.Eq(sp.tanh(a),b)

When I tried to solve it : sp.solve(my_Eq,a), two solutions are found : [log(-sqrt(-(b + 1)/(b - 1))), log(sqrt(-(b + 1)/(b - 1)))]

In the first solution, a is the log of something negative. Why did I obtained this solution because a and b are declared reals ?

After, log, sqrt are used. How is to possible to have something very simple : a=atanh(b)

Upvotes: 1

Views: 104

Answers (1)

smichr
smichr

Reputation: 19115

You might have an imaginary solution (i.e., not something negative):

>>> b=S(-2)
>>> -sqrt(-(b + 1)/(b - 1))
-sqrt(3)*I/3
>>> _.is_real
False
>>> b.is_real
True

If you just want the inverse (which is not always a valid solution) you could explicitly do so, maybe like:

eq = my_Eq
try:
     af = eq.lhs.inverse()
     print(Eq(eq.lhs.args[0], af(eq.rhs)))
except:
    raise ValueError('no inverse for lhs')

Upvotes: 1

Related Questions