Ever since Newton, the word “number” 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:
In [66]:
I*I
Out[66]:
In [67]:
solve( x**2 + 1 , x)
Out[67]:
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:
In [68]:
z = 4 + 3*I
z
Out[68]:
In [69]:
re(z)
Out[69]:
In [70]:
im(z)
Out[70]:
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:
In [71]:
Abs(z)
Out[71]:
In [72]:
arg(z)
Out[72]:
The complex conjugate of $z = a + bi$ is the number $\bar{z} = a - bi$:
In [73]:
conjugate( z )
Out[73]:
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 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:
In [74]:
x = symbols('x', real=True)
exp(I*x).expand(complex=True)
Out[74]:
In [75]:
re( exp(I*x) )
Out[75]:
In [76]:
im( exp(I*x) )
Out[76]:
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,
In [77]:
(cos(x)).rewrite(exp)
Out[77]:
Compare this expression with the definition of hyperbolic cosine.