What is the most efficient way to numerically compute the sine of a complex number?

Suppose I want to calculate the sine of a complex number a + bi on a computer. Suppose that a and b are both floating point numbers with a limited precision and I want to compute the real and complex parts of the sine of a + bi.

I'm sure that there are many ways to break this down, but supposing that I have functions available in a library that compute exponents and trig functions of real numbers, how can I go about computing the sine of a complex number?

  • $\begingroup$ That should be provided by the standard library of the language. If not, $$\sin (a+ib) = \sin a \cos (ib) + \cos a \sin (ib) = \sin a \cosh b + i \cos a \sinh b$$ should be faster and more accurate than anything you write by hand unless you're an expert. $\endgroup$ – Daniel Fischer May 16 '14 at 17:35
  • $\begingroup$ @DanielFischer, I think that should be posted as an answer. I'm working on a library in Javascript. I want it to be compatible with this new technology called asm.js (which most libraries aren't). Therefore, I needed to know how I can do this sort of computation. I think that your answer is probably best! $\endgroup$ – Daniel Allen Langdon May 16 '14 at 17:42

You can use the usual trigonometric formulas ($\cos(a+b)=\cos a \cos b-\sin a\sin b$, etc.), and you also need:

$$\cos ix=\mathrm{cosh}\, x$$

$$\sin ix=i \,\mathrm{sinh}\, x$$

They are direct consequences of the definition of $e^z$ (and then trigonometric and hyperbolic functions) as a series.

Thus, for example,

$$\cos(a+ib)=\cos a\,\mathrm{cosh}\,b-i\sin a\,\mathrm{sinh}\, b$$

  • 1
    $\begingroup$ You mean $\sin ix = i$ sh $x$, surely? (And I would write $\sinh x$ for sh $x$.) $\endgroup$ – TonyK May 16 '14 at 17:43
  • $\begingroup$ Yes, latex typo :-) $\endgroup$ – Jean-Claude Arbaut May 16 '14 at 17:44
  • $\begingroup$ The usual abbreviation in the United States for the function $x \mapsto \frac12(e^x+e^{-x})$ is $\cosh x$, and similarly $x \mapsto \frac12(e^x-e^{-x})$ is $\sinh x$. $\endgroup$ – MJD May 16 '14 at 17:45
  • $\begingroup$ @MJD I know, but I'm french :-) Ok, I'll change this, since it's the most common in programming languages, so the most likely to be known... $\endgroup$ – Jean-Claude Arbaut May 16 '14 at 17:47
  • $\begingroup$ No need to change it; I was only adding my comment to help explain your French abbreviation. $\endgroup$ – MJD May 16 '14 at 18:02

$(1)$ You can always use the taylor series of the $\sin$ function namely, $$\sin(z) = z - \frac{z^3}{3!} + \frac{z^5}{5!} - \frac{z^7}{7!} + \cdots$$

$(2)$ You can also use the relationship $$e^{iz}=\cos(z)+i\sin(z),\ \sin(z)=\frac{e^{iz}-e^{-iz}}{2i}$$

  • $\begingroup$ I am familiar with using the Taylor series. I thought that perhaps there was a more efficient way that would perhaps involve defining the real and imaginary parts of the answer in terms of real-valued trig functions or somesuch. I am doing this in a computer program that will compute the sine of many, many complex numbers, so I'm looking for the most efficient way. $\endgroup$ – Daniel Allen Langdon May 16 '14 at 17:34
  • $\begingroup$ @Daniel: You are right $-$ Rod's answer is irrelevant and unhelpful. $\endgroup$ – TonyK May 16 '14 at 17:38
  • $\begingroup$ @Rod: Now that's better! +1 $\endgroup$ – TonyK May 16 '14 at 17:42
  • $\begingroup$ I wouldn't call it irrelevant. I certainly could use a Taylor expansion to do this sort of calculation. I just don't know how it would compare to other ways of doing the calculation. $\endgroup$ – Daniel Allen Langdon May 16 '14 at 20:18
  • $\begingroup$ @Daniel: It's irrelevant because you told us that you already have the trig functions over $\mathbb R$ at your disposal. $\endgroup$ – TonyK May 18 '14 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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