```python
from sympy import *
x, y, z, t = symbols('x y z t')
```

## Complex numbers

Ever since Newton, the word &ldquo;number&rdquo; has been used to refer to one
of the following types of math objects: the naturals $\mathbb{N}$, the integers
$\mathbb{Z}$, the rationals $\mathbb{Q}$, and the real numbers $\mathbb{R}$. Each set of numbers is
associated with a different class of equations. The natural numbers
$\mathbb{N}$ appear as solutions of the equation $m + n = x$, where $m$ and $n$ are
natural numbers (denoted $m, n \in \mathbb{N}$). The integers $\mathbb{Z}$ are the solutions
to equations of the form $x + m = n$, where $m, n \in \mathbb{N}$. The rational
numbers $\mathbb{Q}$ are necessary to solve for $x$ in $mx = n$, with $m, n \in \mathbb{Z}$.
The solutions to $x^2 = 2$ are irrational (so $\not\in \mathbb{Q}$) so we need an even
larger set that contains *all* possible numbers: real set of numbers $\mathbb{R}$.
A pattern emerges where more complicated equations require the
invention of new types of numbers.

Consider the quadratic equation $x^2 = -1$. There are no real solutions
to this equation, but we can define an imaginary number $i = \sqrt{-1}$
(denoted `I` in `SymPy`) that satisfies this equation:


```python
I*I
```




$\displaystyle -1$




```python
solve( x**2 + 1 , x)
```




    [-I, I]



The solutions are $x = i$ and $x = -i$, and indeed we can verify that
$i^2 + 1 = 0$ and $(-i)^2 + 1 = 0$ since $i^2 = -1$.

The complex numbers $\mathbb{C}$ are defined as $\{ a+bi \,|\, a,b \in \mathbb{R} \}$. Complex numbers
contain a real part and an imaginary part:


```python
z = 4 + 3*I
z
```




$\displaystyle 4 + 3 i$




```python
re(z)
```




$\displaystyle 4$




```python
im(z)
```




$\displaystyle 3$



The *polar* representation of a complex number is $z\!\equiv\!|z|\angle\theta\!\equiv \!|z|e^{i\theta}$.
For a complex number $z=a+bi$, 
the quantity $|z|=\sqrt{a^2+b^2}$ is known as the absolute value of $z$,
and $\theta$ is its *phase* or its *argument*:


```python
Abs(z)
```




$\displaystyle 5$




```python
arg(z)
```




$\displaystyle \operatorname{atan}{\left(\frac{3}{4} \right)}$



The complex conjugate of $z = a + bi$ is the number $\bar{z} = a - bi$:


```python
conjugate( z )
```




$\displaystyle 4 - 3 i$



Complex conjugation is important for computing the absolute value
of $z$ $\left(|z|\equiv\sqrt{z\bar{z}}\right)$ and for division by $z$ $\left(\frac{1}{z}\equiv\frac{\bar{z}}{|z|^2}\right)$.

### Euler's formula

[Euler's formula](https://en.wikipedia.org/wiki/Euler's_formula) shows an important relation between the exponential
function $e^x$ and the trigonometric functions $sin(x)$ and $cos(x)$:

$$e^{ix} = \cos x + i \sin x.$$

To obtain this result in `SymPy`, you must specify that the number $x$ is
real and also tell `expand` that you're interested in complex expansions:


```python
x = symbols('x', real=True)
exp(I*x).expand(complex=True)
```




$\displaystyle i \sin{\left(x \right)} + \cos{\left(x \right)}$




```python
re( exp(I*x) )
```




$\displaystyle \cos{\left(x \right)}$




```python
im( exp(I*x) )
```




$\displaystyle \sin{\left(x \right)}$



Basically, $\cos(x)$ is the real part of $e^{ix}$, and $\sin(x)$ is the imaginary
part of $e^{ix}$. Whaaat? I know it's weird, but weird things are bound
to happen when you input imaginary numbers to functions.

Euler's formula is often used to rewrite the functions `sin` and `cos` in
terms of complex exponentials. For example,


```python
(cos(x)).rewrite(exp)
```




$\displaystyle \frac{e^{i x}}{2} + \frac{e^{- i x}}{2}$



Compare this expression with the definition of hyperbolic cosine.

f64907997c71bcfb33b672dac935001b8de161ee
ipynb
Jupyter Notebook
notebooks/Complex-numbers.ipynb
minireference/sympytut_notebooks
6669e7bfccef9e70ae029ac5cbb54cb6cbc31652
BSD-3-Clause
2016-08-29T12:04:19.000Z
2020-02-23T05:14:52.000Z
notebooks/Complex-numbers.ipynb
minireference/sympytut_notebooks
6669e7bfccef9e70ae029ac5cbb54cb6cbc31652
BSD-3-Clause
notebooks/Complex-numbers.ipynb
minireference/sympytut_notebooks
6669e7bfccef9e70ae029ac5cbb54cb6cbc31652
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES