Saturday, March 28, 2026

Computing sine and cosine of advanced arguments with solely actual features




Suppose you might have a calculator or math library that solely handles actual arguments however you want to consider sin(3 + 4i). What do you do?

Should you’re utilizing Python, for instance, and also you don’t have NumPy put in, you need to use the built-in math library, nevertheless it won’t settle for advanced inputs.

>>> import math
>>> math.sin(3 + 4j)
Traceback (most up-to-date name final):
File "", line 1, in 
TypeError: should be actual quantity, not advanced

You need to use the next identities to calculate sine and cosine for advanced arguments utilizing solely actual features.

The proof could be very easy: simply use the addition formulation for sine and cosine, and the next identities.

begin{align*} sin iz &= i sinh z  cos iz &= cosh z end{align*}

The next code implements sine and cosine for advanced arguments utilizing solely the built-in Python features that settle for actual arguments. It then exams these in opposition to the NumPy variations that settle for advanced arguments.


from math import *
import numpy as np

def complex_sin(z):
    x, y = z.actual, z.imag
    return sin(x)*cosh(y) + 1j*cos(x)*sinh(y)

def complex_cos(z):
    x, y = z.actual, z.imag
    return cos(x)*cosh(y) - 1j*sin(x)*sinh(y)

z = 3 + 4j
mysin = 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)

Associated posts





Related Articles

Latest Articles