Article 74HKW Computing sine and cosine of complex arguments with only real functions

Computing sine and cosine of complex arguments with only real functions

by
John
from John D. Cook on (#74HKW)

Suppose you have a calculator or math library that only handles real arguments but you need to evaluate sin(3 + 4i). What do you do?

If you're using Python, for example, and you don't have NumPy installed, you can use the built-in math library, but it will not accept complex inputs.

>>> import math>>> math.sin(3 + 4j)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: must be real number, not complex

You can use the following identities to calculate sine and cosine for complex arguments using only real functions.

complex_sincos1.svg

The proof is very simple: just use the addition formulas for sine and cosine, and the following identities.

complex_sincos4.svg

The following code implements sine and cosine for complex arguments using only the built-in Python functions that accept real arguments. It then tests these against the NumPy versions that accept complex arguments.

from math import *import numpy as npdef complex_sin(z): x, y = z.real, z.imag return sin(x)*cosh(y) + 1j*cos(x)*sinh(y)def complex_cos(z): x, y = z.real, z.imag return cos(x)*cosh(y) - 1j*sin(x)*sinh(y)z = 3 + 4jmysin = complex_sin(z)mycos = complex_cos(z)npsin = np.sin(z)npcos = np.cos(z)assert(abs(mysin - npsin) < 1e-14)assert(abs(mycos - npcos) < 1e-14)
Related postsThe post Computing sine and cosine of complex arguments with only real functions first appeared on John D. Cook.
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments