Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complex-numbers concept docs #3554

Merged
merged 42 commits into from
Dec 5, 2023
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
eda3a5a
complex-numbers concept docs
colinleach Nov 25, 2023
8165d51
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
6fe58da
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
17d8b42
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
b0a6f94
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
9e23c2c
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
498c565
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
ca8f941
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
2f568e5
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
a51f39f
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
e2fca12
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
77f574c
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
c6ec6a6
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
2e0f35e
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
d84f687
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
934d33c
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
5653132
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
759bb5b
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
ef8ab6e
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
b4b02ab
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
153c019
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
72fabca
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
38d37f8
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
bf81d3a
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
c8b8fc1
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
f8ac3cd
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
efd391b
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
1e59be5
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
67d0653
Update concepts/complex-numbers/about.md
colinleach Nov 27, 2023
93b26d8
fixed several links
colinleach Nov 27, 2023
5b1dcf3
some relatively small fixes, mainly typos and formatting
colinleach Nov 28, 2023
b674997
Moved sections around, in the search for a clearer flow of ideas.
colinleach Nov 28, 2023
02867e1
added bit on `complex(string)` constructor.
colinleach Nov 28, 2023
8942925
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
0dcf403
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
6e12c30
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
d773536
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
7113623
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
4dfc09d
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
a2aa792
Update concepts/complex-numbers/about.md
colinleach Nov 29, 2023
f648017
Edited About to serve as Introduction
BethanyG Dec 5, 2023
86d4093
Titled Introduction as Introduction
BethanyG Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Edited About to serve as Introduction
BethanyG committed Dec 5, 2023
commit f648017739d9372b613bd0eab834771e14f75c85
2 changes: 1 addition & 1 deletion concepts/complex-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"blurb": "Complex numbers are a fundamental data type in Python, along with int and float. Further support is added with the cmath module, which is part of the Python standard library.",
"authors": ["bethanyg", "colinleach"],
"authors": ["BethanyG", "colinleach"],
"contributors": []
}
137 changes: 50 additions & 87 deletions concepts/complex-numbers/introduction.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# Introduction
# About

`Complex numbers` are not complicated.
They just need a less alarming name.

They are so useful, especially in engineering and science, that Python includes [`complex`][complex] as a standard type alongside integers and floating-point numbers.
They are so useful, especially in engineering and science (_everything from JPEG compression to quantum mechanics_), that Python includes [`complex`][complex] as a standard numeric type alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]).

## Basics

A `complex` value in Python is essentially a pair of floating-point numbers.
These are called the "real" and "imaginary" parts.

There are two common ways to create them.
The `complex(real, imag)` constructor takes two `float` parameters:
A `complex` value in Python is essentially a pair of floating-point numbers:

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

There are two rules for an imaginary part:
- It is designated with `j` (not `i`, which 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`.
These are called the "real" and "imaginary" parts.
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 `1j * 1j == -1`.
This is a simple idea, but it leads to interesting mathematical consequences.

In Python, the "imaginary" part is designated with `j` (_not `i` as you would see in math textbooks_), and
the `j` must immediately follow a number, to prevent Python seeing it as a variable name:


```python
>>> j
@@ -36,95 +34,60 @@ NameError: name 'j' is not defined
<class 'complex'>
```

With this, we have a second 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.

To access the parts individually:
```python
>>> z2.real
2.0
>>> z2.imag
1.5
```
There are two common ways to create complex numbers.

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

Most of the [`operators`][operators] used with `float` also work with `complex`:
```python
>>> z1 = complex(1.5, 2.0)
>>> z1
(1.5+2j)
```

```python
>>> z1, z2
((1.5+2j), (2+1.5j))
The constructor can also parse string input.
This has the odd limitation that it fails if the string contains spaces.

>>> z1 + z2 # addition
(3.5+3.5j)
```python
>>> complex('4+2j')
(4+2j)

>>> complex('4 + 2j')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: complex() arg is a malformed string
```

>>> z1 - z2 # subtraction
(-0.5+0.5j)

>>> z1 * z2 # multiplication
6.25j
2) The complex number can be specified as `<real part> + <complex part>j` literal, or just `<complex part>j` if the real part is zero:

>>> z1 / z2 # division
(0.96+0.28j)

>>> z1 ** 2 # exponentiation
(-1.75+6j)
```python
>>> z2 = 2.0 + 1.5j
>>> z2
(2+1.5j)
```
The end result is identical to using the `complex()` constructor.

>>> 2 ** z1 # another exponentiation
(0.5188946835878313+2.7804223253571183j)

>>> 1j ** 2 # j is the square root of -1
(-1+0j)
```

Explaining the rules for complex multiplication and division is out of scope here.
Any introduction to complex numbers will cover this.
Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement this from first principles.

There are two functions that are useful with complex numbers:
- `conjugate()` simply flips the sign of the complex part.
- `abs()` is guaranteed to return a real number with no imaginary part.

```python
>>> z1
(1.5+2j)

>>> z1.conjugate() # flip the z1.imag sign
(1.5-2j)

>>> abs(z1) # sqrt(z1.real ** 2 + z1.imag ** 2)
2.5
```
## Arithmetic

## The `cmath` module
Most of the [`operators`][operators] used with floats and ints also work with complex numbers.

The Python standard library has a `math` module full of useful functionality for working with real numbers.
It also has an equivalent `cmath` module for complex numbers.
Integer division is _**not**_ possible on complex numbers, so the `//` and `%` operators and `divmod()` functions will fail for the complex number type.

Details are available in the [`cmath`][cmath] module documents, but the main categories are:
- conversion between Cartesian and polar coordinates
- exponential and log functions
- trig functions
- hyperbolic functions
- classification functions
- useful constants
Explaining the rules for complex number multiplication and division is out of scope for this concept (_and you are unlikely to have to perform those operations "by hand" very often_).

```python
>>> import cmath
Any [mathematical][math-complex] or [electrical engineering][engineering-complex] introduction to complex numbers will cover these scenarios, should you want to dig into the topic.

>>> euler = cmath.exp(1j * cmath.pi) # Euler's equation
The Python standard library has a [`math`][math-module] module full of useful functionality for working with real numbers and the [`cmath`][cmath] module is its equivalent for working with complex numbers.

>>> euler.real
-1.0
>>> round(euler.imag, 15) # round to 15 decimal places
0.0
```

[complex]: https://docs.python.org/3/library/functions.html#complex
[cmath]: https://docs.python.org/3/library/cmath.html
[complex]: https://docs.python.org/3/library/functions.html#complex
[engineering-complex]: https://www.khanacademy.org/science/electrical-engineering/ee-circuit-analysis-topic/ee-ac-analysis/v/ee-complex-numbers
[floats]: https://docs.python.org/3/library/functions.html#float
[ints]: https://docs.python.org/3/library/functions.html#int
[math-complex]: https://www.nagwa.com/en/videos/143121736364/
[math-module]: https://docs.python.org/3/library/math.html
[operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
4 changes: 4 additions & 0 deletions concepts/complex-numbers/links.json
Original file line number Diff line number Diff line change
@@ -10,5 +10,9 @@
{
"url": "https://docs.python.org/3/library/cmath.html/",
"description": "Module documentation for cmath."
},
{
"url": "https://docs.python.org/3/library/math.html/",
"description": "Module documentation for math."
}
]