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).

  • 1
    $\begingroup$ You are probably forgetting an $i$: $\sinh(\sqrt{b^2-a^2}) = i \sin(\sqrt{a^2-b^2})$. $\endgroup$
    – Domen
    Commented May 4, 2023 at 12:44
  • $\begingroup$ @Domen true, my bad $\endgroup$
    – And R
    Commented May 4, 2023 at 15:28
  • $\begingroup$ Is there a reason ReplaceAll would not work? $\endgroup$ Commented May 4, 2023 at 17:02
  • $\begingroup$ @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. $\endgroup$
    – And R
    Commented May 4, 2023 at 17:26
  • $\begingroup$ @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. $\endgroup$ Commented May 4, 2023 at 21:41

2 Answers 2

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]    *)
  • $\begingroup$ Why didn't I think of ComplexExpand myself? Thanks a lot, this is exactly what I wanted! $\endgroup$
    – And R
    Commented May 4, 2023 at 20:24
  • $\begingroup$ @AndR note that the FullSimplify might make the code slow if your matrices are large and complex. $\endgroup$ 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]];
   If[expr[[pos, 1]] < 0, 
    ReplacePart[expr, pos -> I*Sqrt[-expr[[pos, 1]]]]], opt]]

Then, taking your function:

expr = Sinh[Sqrt[b^2 - a^2]];

one finds

f[expr, {a > b, b > 0}]

(*  I Sin[Sqrt[a^2 - b^2]]  *)

However, in a more complex case, one maybe needs to modify this function to account for peculiarity of the expression.

Have fun!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.