Skip to content

Commit

Permalink
Moved sections around, in the search for a clearer flow of ideas.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleach committed Nov 28, 2023
1 parent 5b1dcf3 commit b674997
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions concepts/complex-numbers/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ Again, it is best to focus on the underlying simplicity and not the strange name

There are two common ways to create complex numbers.

The [`complex(real, imag)`][complex] constructor takes two `float` parameters:
1) The [`complex(real, imag)`][complex] constructor takes two `float` parameters:

```python
>>> z1 = complex(1.5, 2.0)
>>> z1
(1.5+2j)
```

Most engineers are happy with `j`.
Most scientists and mathematicians prefer the mathematical notation `i`, but in designing Python the engineers won.
2) The complex number can be specified as `<real part> + <complex part>j`, or just `<complex part>j` if the real part is zero:

```python
>>> z2 = 2.0 + 1.5j
>>> z2
(2+1.5j)
```
The end result is identical to using a constructor.

So there are two rules for an imaginary part in Python:
There are two rules for an imaginary part in Python:

- It is designated with `j` not `i`.
- It is designated with `j` (not `i` as you may see in textbooks).
- The `j` must immediately follow a number, to prevent Python seeing it as a variable name. If necessary, use `1j`.

```python
Expand All @@ -44,13 +49,8 @@ NameError: name 'j' is not defined
<class 'complex'>
```

With this, we have a second and perhaps clearer way to create a complex number:
```python
>>> z2 = 2.0 + 1.5j
>>> z2
(2+1.5j)
```
The end result is identical to using a constructor.
Most engineers are happy with `j`.
Most scientists and mathematicians prefer the mathematical notation `i`, but in designing Python the engineers won.

To access the parts individually:
```python
Expand All @@ -73,6 +73,15 @@ However, it is still a complex number in Python.
(1+0j)
```

You may have heard that "`i` (or `j`) is the square root of -1".

For now, all this means is that the imaginary part _by definition_ satisfies the equality
```python
1j * 1j == -1 # => True
```

This is a simple idea, but it leads to interesting consequences.

## Arithmetic

Most of the [`operators`][operators] used with floats and ints also work with complex numbers:
Expand Down

0 comments on commit b674997

Please sign in to comment.