|
| 1 | +Complex numbers |
| 2 | +=============== |
| 3 | + |
| 4 | +Complex numbers consist of a real part and an `imaginary part |
| 5 | +<https://en.wikipedia.org/wiki/Imaginary_number>`_, which is given the suffix |
| 6 | +``j`` in Python. |
| 7 | + |
| 8 | +.. code-block:: pycon |
| 9 | +
|
| 10 | + >>> 7 + 2j |
| 11 | + (7+2j) |
| 12 | +
|
| 13 | +.. note:: |
| 14 | + |
| 15 | + Python expresses the resulting complex number in parentheses to indicate |
| 16 | + that the output represents the value of a single object: |
| 17 | + |
| 18 | +.. code-block:: pycon |
| 19 | +
|
| 20 | + >>> (5 + 3j) ** (3 + 5j) |
| 21 | + (-7.04464115622119-11.276062812695923j) |
| 22 | +
|
| 23 | +.. code-block:: pycon |
| 24 | +
|
| 25 | + >>> x = (5 + 3j) * (6 + 8j) |
| 26 | + >>> x |
| 27 | + (6+58j) |
| 28 | + >>> x.real |
| 29 | + 6.0 |
| 30 | + >>> x.imag |
| 31 | + 58.0 |
| 32 | +
|
| 33 | +Complex numbers consist of a real part and an imaginary part with the suffix |
| 34 | +``j``. In the preceding code, the variable ``x`` is assigned to a complex |
| 35 | +number. You can get its „real“ part with the attribute notation ``x.real`` and |
| 36 | +the „imaginary“ part with ``x.imag``. |
| 37 | + |
| 38 | +Advanced functions |
| 39 | +------------------ |
| 40 | + |
| 41 | +The functions in the :doc:`math <python3:library/math>` module are not |
| 42 | +applicable to complex numbers; one of the reasons for this is probably that the |
| 43 | +square root of ``-1`` is supposed to produce an error. Therefore, similar |
| 44 | +functions for complex numbers have been provided in the :doc:`cmath |
| 45 | +<python3:library/cmath>` module: |
| 46 | + |
| 47 | +:func:`python3:cmath.acos`, :func:`python3:cmath.acosh`, :func:`python3:cmath.asin`, :func:`python3:cmath.asinh`, :func:`python3:cmath.atan`, :func:`python3:cmath.atanh`, :func:`python3:cmath.cos`, :func:`python3:cmath.cosh`, :func:`python3:cmath.e`, :func:`python3:cmath.exp`, :func:`python3:cmath.log`, :func:`python3:cmath.log10`, :func:`python3:cmath.pi`, :func:`python3:cmath.sin`, :func:`python3:cmath.sinh`, :func:`python3:cmath.sqrt`, :func:`python3:cmath.tan`, :func:`python3:cmath.tanh`. |
| 48 | + |
| 49 | +To make it clear in the code that these functions are special functions for |
| 50 | +complex numbers, and to avoid name conflicts with the more normal equivalents, |
| 51 | +it is recommended to simply import the module to explicitly refer to the |
| 52 | +``cmath`` package when using the function, for example: |
| 53 | + |
| 54 | +.. code-block:: pycon |
| 55 | +
|
| 56 | + >>> import cmath |
| 57 | + >>> cmath.sqrt(-2) |
| 58 | + 1.4142135623730951j |
| 59 | +
|
| 60 | +.. warning:: |
| 61 | + |
| 62 | + Now it becomes clearer why we do not recommend importing all functions of a |
| 63 | + module with :samp:`from {MODULE} import \*`. If you would import the module |
| 64 | + ``math`` first and then the module ``cmath``, the functions in ``cmath`` |
| 65 | + would have priority over those of ``math``. Also, when understanding the |
| 66 | + code, it is much more tedious to find out the source of the functions used. |
| 67 | + |
| 68 | +Checks |
| 69 | +------ |
| 70 | + |
| 71 | +* Load the :mod:`math` module and try out some of the functions. Then load the |
| 72 | + :mod:`cmath` module and do the same. |
| 73 | + |
| 74 | +* How can you restore the functions of the :mod:`math` module? |
0 commit comments