Coding the complex exp, sin and cos function

649 Views Asked by At

I want to implement the complex exp, sin and cos function. The problem is the following:

For computing $\exp(z)$ I wanted to use $\exp(z) = \cos(z)+ i\cdot\sin(z)$. Then I first have to implement sin and cos.

For computing sin and cos I wanted to use the power series expansion

$\sin(z) = z - \dfrac{z^3}{3!}+\dfrac{z^5}{5!}-\dfrac{z^7}{7!}+\dots$

same for $\cos(z)$.

So I need to implement first how to compute $(x+iy)^n$. I thought I should use something like $z^n = (re^{i\varphi})^n$. The problem: I need the complex exp first.

Even if I use the power series expansion for exp I need the code for raising a complex number to a power. Where should I start?

I also thougt about $z^n = (re^{i\varphi})^n = r^n(\cos(\varphi n) +i\sin(\varphi n))$ (De Moivre's Formula) which allows me to compute the $n$-th power of $z$ without using exp. You should see the point here. I cant compute sin and cos without computing the $n$-th power of $z$.

4

There are 4 best solutions below

4
On

If you have $z=a+bi$, you can find $r$ and $\theta$ for $z$ using $$r=\sqrt{a^2+b^2} \quad \text{and} \quad \theta =\arctan(a,b)$$ where $\arctan(a,b)$ gives you an angle from $0$ to $2\pi$ by incorporating the sign of the value of $a$ and $b$. Then you can use the normal exponential since $r$ and $\theta$ are real numbers to obtain $$z^n = r^n \cos(n\theta)+ir^n\sin(n\theta)$$

2
On

You do NOT want to use the power series expansion to compute sin and cos.

The first step is to do a range reduction, so that the parameter is in a restricted range such as $[0, \pi/4]$.

Then you should investigate ways of computing the restricted sin and cos.

cordic is one thing you might look up.

For serious computation, do not roll your own functions.

1
On

You are very much putting the cart before the horse.

In the comments you express a hesitance to just use repeated multiplication to compute $z^n$. You cannot get away from the fact that, fundamentally, exponentiation is repeated multiplication, because that idea lies at the heart of any definition of complex exponentiation. A concept like complex exponentiation doesn't just spring out of nowhere, it's built out of smaller building blocks. If you want to implement things from scratch, at one point of another, those building blocks are going to have to show up in your code. Repeated multiplication is not something that you can avoid, because in some objective sense it's what complex exponentiation is.

You expressed a concern over how long it would take to compute $z^n$ for large $n$. But the same concern applies to computing the series expansion of $e^z$ - you'll have to compute many many terms to get good precision. And for larger and larger values of $z$, you'll need more and more terms to get the same level of precision.

You should use repeated multiplication to implement $z^n$, possibly with the "by squaring" optimization.

0
On

As has been mentioned and should be mentioned again: any programming environment with respect for itself already has support for complex numbers of elementary functions and you don't want to implement this yourself for any serious applications unless you really know what you are doing (and not even then). That being said if you want to do this as an exericse for yourself then here is a list of how the usual elementary functions can be computed for a complex number $z=x+iy$ in terms of their real valued functions. I'm assuming you know how to compute $e^x,\sin(x),$ etc. for real $x$.


The multiplication of two complex numbers $z = x+iy$ and $w = x'+iy'$

$$zw = xx' - yy' + i(xy'+x'y)$$

Complex conjugation $$z^* = x - iy$$

Division by a complex number

$$\frac{z}{w} = \frac{zw^*}{|w|^2}$$

The norm of a complex number $$|z| = \sqrt{x^2 + y^2}$$

The argument of a complex number $$\arg(z) = \begin{cases} \arctan(\frac y x) &\text{if } x > 0, \\ \arctan(\frac y x) + \pi &\text{if } x < 0 \text{ and } y \ge 0, \\ \arctan(\frac y x) - \pi &\text{if } x < 0 \text{ and } y < 0, \\ +\frac{\pi}{2} &\text{if } x = 0 \text{ and } y > 0, \\ -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y < 0, \\ 0 &\text{if } x = 0 \text{ and } y = 0. \end{cases}$$ where I choose to simply define $\arg(0) \equiv 0$ (this is usually undefined). With these functions you can express $z$ on polar form $$z = |z| e^{i\arg(z)}$$ The complex exponential follows from Eulers formula $$e^{z} = e^xe^{iy} = e^x\cos(y) + ie^x\sin(y)$$ Trigonometric functions follows from the addition formulas $$\cos(z) = \cos(x)\cosh(y) - i\sin(x)\sinh(y)$$ $$\sin(z) = \sin(x)\cosh(y) + i\cos(x)\sinh(y)$$ where $\cosh(x) = \frac{e^x+e^{-x}}{2}$ and $\sinh(x) = \frac{e^{x}-e^{-x}}{2}$ are the hyperbolic functions.

The principal branch of the complex logarithm can be computed as $$\text{Log}(z) = \log|z| + i \arg(z)$$

Unlike the real logarithm the complex one is multivalued so other valid choices are $\text{Log}_k(z) = \log|z| + i[\arg(z)+2\pi k]$ where $k$ is an integer $k$. This is called the $k$th branch of the logarithm.

Powers of complex numbers can be defined via the exponential and a logarithm as $$z^\alpha \equiv e^{\text{Log}(z)\alpha}$$