# Replace hyperbolic functions with trigonometric ones

In the matrix exponential which I need for a certain problem, I often encounter terms like Sinh[ Sqrt[ b^2 - a^2 ]]. In my problem, a > b and, therefore, the previous expression is actually I Sin[ Sqrt[ a^2 - b^2]]. Is there a way to enforce Mathematica to use trigonometric functions instead of hyperbolic ones in expressions like these?

To give some context, I am writing equations of motion of a damped harmonic oscillator in vector form: $$r'(t) = A. r(t)$$, where A = {{ 0 , o }, { - o , -g }}. The solution of this equation is given by r[t_] = MatrixExp[ A t ].r0. The matrix exponential has multiple terms like Cosh[1/2 Sqrt[g^2 - 4 o^2] t]. This is not too illustrative for a weakly damped harmonic oscillator, for which the frequency o >> g (damping rate).

• You are probably forgetting an $i$: $\sinh(\sqrt{b^2-a^2}) = i \sin(\sqrt{a^2-b^2})$. Commented May 4, 2023 at 12:44
• @Domen true, my bad Commented May 4, 2023 at 15:28
• Is there a reason ReplaceAll would not work? Commented May 4, 2023 at 17:02
• @userrandrand do you mean something like Cosh[x_ Sqrt@y_ ] -> Cosh[ I x Sqrt[ -y]]? No reason, but I was worried that the patterns I can come up with are either too imprecise, like the one above, or too precise. Commented May 4, 2023 at 17:26
• @AndR depends on the full expression. Too precise is maybe fairly safe. Too imprecise might lead to unwanted substitutions. I would have turned the equality by Domen into a replacement rule like /. Sinh[ Sqrt[ b^2 - a^2 ]]->I Sin[ Sqrt[ a^2 - b^2]] but maybe that leads to an issue somewhere in your code. Commented May 4, 2023 at 21:41

Assuming[x > y > 0,
{Sqrt[x - y], Sqrt[y - x]} // ComplexExpand // FullSimplify]
(*    {Sqrt[x - y], I Sqrt[x - y]}    *)

Assuming[a > b > 0,
Sinh[Sqrt[b^2 - a^2]] // ComplexExpand // FullSimplify]
(*    I Sin[Sqrt[a^2 - b^2]]    *)

Assuming[o > g > 0,
Cosh[1/2 Sqrt[g^2 - 4 o^2] t] // ComplexExpand // FullSimplify]
(*    Cos[1/2 Sqrt[-g^2 + 4 o^2] t]    *)
• Why didn't I think of ComplexExpand myself? Thanks a lot, this is exactly what I wanted! Commented May 4, 2023 at 20:24
• @AndR note that the FullSimplify might make the code slow if your matrices are large and complex. Commented May 4, 2023 at 21:37

One way would be to write a function for this purpose

f[expr_, opt_] := Module[{pos},
pos = Position[expr, Sqrt[u_]][[1, 1]];
Simplify[
If[expr[[pos, 1]] < 0,
ReplacePart[expr, pos -> I*Sqrt[-expr[[pos, 1]]]]], opt]]