From eda3a5a384ef23ec6bb6588805513e607f356234 Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Sat, 25 Nov 2023 16:37:51 -0700 Subject: [PATCH 01/42] complex-numbers concept docs --- concepts/complex-numbers/.meta/config.json | 4 +- concepts/complex-numbers/about.md | 182 ++++++++++++++++++++- concepts/complex-numbers/introduction.md | 130 ++++++++++++++- concepts/complex-numbers/links.json | 16 +- 4 files changed, 318 insertions(+), 14 deletions(-) diff --git a/concepts/complex-numbers/.meta/config.json b/concepts/complex-numbers/.meta/config.json index 9b9e8da5a9..3d0539aece 100644 --- a/concepts/complex-numbers/.meta/config.json +++ b/concepts/complex-numbers/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "TODO: add blurb for this concept", - "authors": ["bethanyg", "cmccandless"], + "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", "cmccandless", "colinleach"], "contributors": [] } diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index c628150d56..08139d98b8 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -1,2 +1,182 @@ -#TODO: Add about for this concept. +# 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. + +## Basics + +A `complex` value in Python is essentially a pair of floating-point numbers. +These are called the "real" and "imaginary" parts, for unfortunate historical reasons. +Again, it is best to focus on the underlying simplicity and not the strange names. + +There are two common ways to create them. +The `complex(real, imag)` 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 `i`, but in designing Python the engineers won. + +So there are two rules for an imaginary part: +- It is designated with `j` not `i`. +- The `j` must immediately follow a number, to prevent Python seeing it as a variable name. If necessary, use `1j`. + +```python +>>> j +Traceback (most recent call last): + File "", line 1, in +NameError: name 'j' is not defined + +>>> 1j +1j + +>>> type(1j) + +``` + +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. + +To access the parts individually: +```python +>>> z2.real +2.0 +>>> z2.imag +1.5 +``` + +## Arithmetic + +Most of the [`operators`][operators] used with floats also work with complex + +```python +>>> z1, z2 +((1.5+2j), (2+1.5j)) + +>>> z1 + z2 # addition +(3.5+3.5j) + +>>> z1 - z2 # subtraction +(-0.5+0.5j) + +>>> z1 * z2 # multiplication +6.25j + +>>> z1 / z2 # division +(0.96+0.28j) + +>>> z1 ** 2 # exponentiation +(-1.75+6j) + +>>> 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. + +Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` function will fail. + +There are two functions that are useful with complex numbers: +- `conjugate()` simply flips the sign of the complex part. +Because of the way complex multiplication works, this is more useful than you might think. +- `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 +``` + +## The `cmath` module + +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. + +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 + +```python +>>> import cmath + +>>> euler = cmath.exp(1j * cmath.pi) # Euler's equation + +>>> euler.real +-1.0 +>>> round(euler.imag, 15) # round to 15 decimal places +0.0 +``` + +So a simple expression with three of the most important constants in nature `e`, `i` (or `j`) and `pi` gives the result `-1`. +Some people believe this is the most beautiful result in all of mathematics. +It dates back to around 1740. + +----- + +## Optional section: a Complex Numbers FAQ + +This part can be skipped, unless you are interested. + +### Isn't this some strange new piece of pure mathematics? + +It was strange and new in the 16th century. + +500 years later, it is central to most of engineering and the physical sciences. + +### Why would anyone use these? + +It turns out that complex numbers are the simplest way to describe anything that rotates or anything with a wave-like property. + +You can see things rotate. +Complex numbers may not make the world go round, but they are great for explaining what happens as a result of the world going round: look at any satellite image of a major storm. + +Less obviously, sound is wave-like, light is wave-like, radio signals are wave-like, The economy of your home country is at least partly wave-like. + +A lot of this can be done with trig functions (`sin()` and `cos()`) but that gets messy quickly. +Complex exponentials are ___much___ easier to work with. + +### But I never use complex numbers! + +Only true if you are living in a cave and foraging for your food. + +If you read this on any sort of screen, you are utterly dependent on some useful 20th-centry advances. + +1. __Semiconductor chips__. + - These make no sense in classical physics and can only be explained (and designed) by quantum mechanics (QM). + - In QM, everything is complex-valued by definition. +2. __The Fast Fourier Transform algorithm__. + - FFT is an application of complex numbers, and it is in everything. + - Audio files? MP3 and other formats use FFT for compression. So does MP4 video, JPEG photos, among many others. + - Also, it is in the digital filters that let a cellphone mast separate your signal from everyone else's. + +So, you are probably using complex numbers thousands of times per second. +Be grateful to the tech people who understand this stuff so that you maybe don't need to. + +[complex]: https://docs.python.org/3/library/functions.html#complex +[cmath]: https://docs.python.org/3/library/cmath.html +[operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex diff --git a/concepts/complex-numbers/introduction.md b/concepts/complex-numbers/introduction.md index fcde74642c..0ac61d5987 100644 --- a/concepts/complex-numbers/introduction.md +++ b/concepts/complex-numbers/introduction.md @@ -1,2 +1,130 @@ -#TODO: Add introduction for this concept. +# Introduction +`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. + +## 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: + +```python +>>> z1 = complex(1.5, 2.0) +>>> z1 +(1.5+2j) +``` + +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`. + +```python +>>> j +Traceback (most recent call last): + File "", line 1, in +NameError: name 'j' is not defined + +>>> 1j +1j + +>>> type(1j) + +``` + +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 +``` + +## Arithmetic + +Most of the [`operators`][operators] used with `float` also work with `complex`: + +```python +>>> z1, z2 +((1.5+2j), (2+1.5j)) + +>>> z1 + z2 # addition +(3.5+3.5j) + +>>> z1 - z2 # subtraction +(-0.5+0.5j) + +>>> z1 * z2 # multiplication +6.25j + +>>> z1 / z2 # division +(0.96+0.28j) + +>>> z1 ** 2 # exponentiation +(-1.75+6j) + +>>> 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 +``` + +## The `cmath` module + +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. + +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 + +```python +>>> import cmath + +>>> euler = cmath.exp(1j * cmath.pi) # Euler's equation + +>>> 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 +[operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex diff --git a/concepts/complex-numbers/links.json b/concepts/complex-numbers/links.json index eb5fb7c38a..5428d4c8d2 100644 --- a/concepts/complex-numbers/links.json +++ b/concepts/complex-numbers/links.json @@ -1,18 +1,14 @@ [ { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex/", + "description": "Operations on numeric types." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/functions.html#complex/", + "description": "The complex class." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." - }, - { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/cmath.html/", + "description": "Module documentation for cmath." } ] From 8165d51846fc5744bb515e05128b374e6f62b5d0 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 07:52:56 -0700 Subject: [PATCH 02/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 08139d98b8..945b98ee86 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -3,7 +3,8 @@ `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, that Python includes [`complex`][complex] as a standard numeric type alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]). + ## Basics From 6fe58da6582fd3ea74ed9e071115043b7c5274f5 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 07:53:30 -0700 Subject: [PATCH 03/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 945b98ee86..def480f2ad 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -12,7 +12,8 @@ A `complex` value in Python is essentially a pair of floating-point numbers. These are called the "real" and "imaginary" parts, for unfortunate historical reasons. Again, it is best to focus on the underlying simplicity and not the strange names. -There are two common ways to create them. +There are two common ways to create complex numbers. + The `complex(real, imag)` constructor takes two `float` parameters: ```python From 17d8b42a2cd58a0afef34e0b869d18670c14cbe2 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 07:59:01 -0700 Subject: [PATCH 04/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index def480f2ad..3b2679b624 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -23,7 +23,8 @@ The `complex(real, imag)` constructor takes two `float` parameters: ``` Most engineers are happy with `j`. -Most scientists and mathematicians prefer `i`, but in designing Python the engineers won. +Most scientists and mathematicians prefer the mathematical notation `i`, but in designing Python the engineers won. + So there are two rules for an imaginary part: - It is designated with `j` not `i`. From b0a6f94019a727525726f775681a23947e8a112f Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:02:23 -0700 Subject: [PATCH 05/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 3b2679b624..2f27b551db 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -61,7 +61,8 @@ To access the parts individually: ## Arithmetic -Most of the [`operators`][operators] used with floats also work with complex +Most of the [`operators`][operators] used with floats and ints also work with complex numbers: + ```python >>> z1, z2 From 9e23c2c21c086cec814f70ea9b63bdd26d0d3ac0 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:03:05 -0700 Subject: [PATCH 06/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 2f27b551db..febd6314ab 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -65,8 +65,9 @@ Most of the [`operators`][operators] used with floats and ints also work with co ```python ->>> z1, z2 -((1.5+2j), (2+1.5j)) +>>> z1 = (1.5+2j) +>>> z2 = (2+1.5j) + >>> z1 + z2 # addition (3.5+3.5j) From 498c565229c1bfdad04a9bcd247c56a248cf0c3c Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:05:46 -0700 Subject: [PATCH 07/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index febd6314ab..aab45bb877 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -91,7 +91,8 @@ Most of the [`operators`][operators] used with floats and ints also work with co (-1+0j) ``` -Explaining the rules for complex multiplication and division is out of scope here. +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_). + Any introduction to complex numbers will cover this. Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement this from first principles. From ca8f941bdfc07f78f794013cbc2a242c6594e9a9 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:06:53 -0700 Subject: [PATCH 08/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index aab45bb877..38c1c701ca 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -93,7 +93,8 @@ Most of the [`operators`][operators] used with floats and ints also work with co 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_). -Any introduction to complex numbers will cover this. +Any [mathematical][math-complex] or [electrical engineering][engineering-complex] introduction to complex numbers will cover this, should you want to dig into the topic. + Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement this from first principles. Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` function will fail. From 2f568e54eb7927d6898416c74ef566cc0ca91183 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:07:10 -0700 Subject: [PATCH 09/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 38c1c701ca..74786aa95e 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -177,7 +177,8 @@ If you read this on any sort of screen, you are utterly dependent on some useful - These make no sense in classical physics and can only be explained (and designed) by quantum mechanics (QM). - In QM, everything is complex-valued by definition. 2. __The Fast Fourier Transform algorithm__. - - FFT is an application of complex numbers, and it is in everything. + - FFT is an application of complex numbers, and it is in _everything_ connected to sound transmission, audio processing, photos, and video. + - Audio files? MP3 and other formats use FFT for compression. So does MP4 video, JPEG photos, among many others. - Also, it is in the digital filters that let a cellphone mast separate your signal from everyone else's. From a51f39fb18cff399c975ba3b59f3f2cea4800ce8 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:07:23 -0700 Subject: [PATCH 10/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 74786aa95e..85e655742e 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -179,7 +179,9 @@ If you read this on any sort of screen, you are utterly dependent on some useful 2. __The Fast Fourier Transform algorithm__. - FFT is an application of complex numbers, and it is in _everything_ connected to sound transmission, audio processing, photos, and video. - - Audio files? MP3 and other formats use FFT for compression. So does MP4 video, JPEG photos, among many others. + -MP3 and other audio formats use FFT for compression, ensuring more audio can fit within a smaller storage space. + - JPEG compression and MP4 video, among many other image and video formats also use FTT for compression. + - Also, it is in the digital filters that let a cellphone mast separate your signal from everyone else's. So, you are probably using complex numbers thousands of times per second. From e2fca12533aae8514a133835dac696144ed8d826 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:07:38 -0700 Subject: [PATCH 11/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 85e655742e..f6770265ae 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -182,7 +182,8 @@ If you read this on any sort of screen, you are utterly dependent on some useful -MP3 and other audio formats use FFT for compression, ensuring more audio can fit within a smaller storage space. - JPEG compression and MP4 video, among many other image and video formats also use FTT for compression. - - Also, it is in the digital filters that let a cellphone mast separate your signal from everyone else's. + - FFT is also deployed in the digital filters that allow cellphone towers to separate your personal cell signal from everyone else's. + So, you are probably using complex numbers thousands of times per second. Be grateful to the tech people who understand this stuff so that you maybe don't need to. From 77f574cf6acf452cfd00552385c796c63a82d34d Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:07:56 -0700 Subject: [PATCH 12/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index f6770265ae..903648b5a6 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -185,7 +185,8 @@ If you read this on any sort of screen, you are utterly dependent on some useful - FFT is also deployed in the digital filters that allow cellphone towers to separate your personal cell signal from everyone else's. -So, you are probably using complex numbers thousands of times per second. +So, you are probably using technology that relies on complex number calculations thousands of times per second. + Be grateful to the tech people who understand this stuff so that you maybe don't need to. [complex]: https://docs.python.org/3/library/functions.html#complex From c6ec6a6ef21ec3d7f767cb919568e17cb8c5918c Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:08:07 -0700 Subject: [PATCH 13/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 1 - 1 file changed, 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 903648b5a6..49868c1074 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -187,7 +187,6 @@ If you read this on any sort of screen, you are utterly dependent on some useful So, you are probably using technology that relies on complex number calculations thousands of times per second. -Be grateful to the tech people who understand this stuff so that you maybe don't need to. [complex]: https://docs.python.org/3/library/functions.html#complex [cmath]: https://docs.python.org/3/library/cmath.html From 2e0f35efe0abca63f724d2799f61743a14dafe3a Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:09:34 -0700 Subject: [PATCH 14/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 49868c1074..40a3841f5a 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -95,7 +95,8 @@ Explaining the rules for complex number multiplication and division is out of sc Any [mathematical][math-complex] or [electrical engineering][engineering-complex] introduction to complex numbers will cover this, should you want to dig into the topic. -Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement this from first principles. +Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement a complex number class with these operations from first principles. + Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` function will fail. From d84f687aeebbe4da173d128cb68ae213e83bb8f1 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:10:09 -0700 Subject: [PATCH 15/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 40a3841f5a..29fcd6446b 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -98,7 +98,8 @@ Any [mathematical][math-complex] or [electrical engineering][engineering-complex Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement a complex number class with these operations from first principles. -Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` function will fail. +Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` functions will fail for the complex number type. + There are two functions that are useful with complex numbers: - `conjugate()` simply flips the sign of the complex part. From 934d33c6cbc36028aaec1e895541b32fc65f41a2 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:10:34 -0700 Subject: [PATCH 16/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 29fcd6446b..958ca27137 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -101,7 +101,8 @@ Alternatively, Exercism has a `Complex Numbers` practice exercise where you can Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` functions will fail for the complex number type. -There are two functions that are useful with complex numbers: +There are two functions implemented for numeric types that are very useful when working with complex numbers: + - `conjugate()` simply flips the sign of the complex part. Because of the way complex multiplication works, this is more useful than you might think. - `abs()` is guaranteed to return a real number with no imaginary part. From 5653132bc4a450a1d8ad4fff89beee526067c658 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:11:07 -0700 Subject: [PATCH 17/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 958ca27137..4e41b65b22 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -103,7 +103,8 @@ Integer division is ___not___ possible on complex numbers, so the `//` and `%` o There are two functions implemented for numeric types that are very useful when working with complex numbers: -- `conjugate()` simply flips the sign of the complex part. +- `.conjugate()` simply flips the sign of the imaginary part of a complex number (_from + to - or vice-versa_). + Because of the way complex multiplication works, this is more useful than you might think. - `abs()` is guaranteed to return a real number with no imaginary part. From 759bb5bb6b577e9eedb8aa8570b39db938baf765 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:11:34 -0700 Subject: [PATCH 18/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 4e41b65b22..4627312256 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -106,7 +106,8 @@ There are two functions implemented for numeric types that are very useful when - `.conjugate()` simply flips the sign of the imaginary part of a complex number (_from + to - or vice-versa_). Because of the way complex multiplication works, this is more useful than you might think. -- `abs()` is guaranteed to return a real number with no imaginary part. +- `abs()` is guaranteed to return a real number with no imaginary part. + ```python >>> z1 From ef8ab6e664543d14a13c8076d4035fef6d1e6060 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:12:18 -0700 Subject: [PATCH 19/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 4627312256..b616c38899 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -122,7 +122,8 @@ Because of the way complex multiplication works, this is more useful than you mi ## The `cmath` module -The Python standard library has a `math` module full of useful functionality for working with real numbers. +The Python standard library has a [`math`][math-module] module full of useful functionality for working with real numbers. + It also has an equivalent `cmath` module for complex numbers. Details are available in the [`cmath`][cmath] module documents, but the main categories are: From b4b02abc37be91fae0e8331396681bd2e041f897 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:12:49 -0700 Subject: [PATCH 20/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index b616c38899..6d79385c51 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -124,7 +124,8 @@ Because of the way complex multiplication works, this is more useful than you mi The Python standard library has a [`math`][math-module] module full of useful functionality for working with real numbers. -It also has an equivalent `cmath` module for complex numbers. +It also has an equivalent [`cmath`][cmath] module for working with complex numbers. + Details are available in the [`cmath`][cmath] module documents, but the main categories are: - conversion between Cartesian and polar coordinates From 153c0193e6a1b705101d560b3dc40175efa169ab Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:13:12 -0700 Subject: [PATCH 21/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 6d79385c51..1424c6a5ed 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -127,7 +127,8 @@ The Python standard library has a [`math`][math-module] module full of useful fu It also has an equivalent [`cmath`][cmath] module for working with complex numbers. -Details are available in the [`cmath`][cmath] module documents, but the main categories are: +We encourage you to read through the module and experiment, but the main categories are: + - conversion between Cartesian and polar coordinates - exponential and log functions - trig functions From 72fabcadd2514f21386e45381c1f855b5e9a40f4 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:14:03 -0700 Subject: [PATCH 22/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 1424c6a5ed..d9dde3fdfb 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -129,12 +129,14 @@ It also has an equivalent [`cmath`][cmath] module for working with complex numbe We encourage you to read through the module and experiment, but the main categories are: -- conversion between Cartesian and polar coordinates -- exponential and log functions -- trig functions -- hyperbolic functions -- classification functions -- useful constants +- Conversion between Cartesian and polar coordinates, +- Exponential and log functions, +- Trigonometric functions, +- Hyperbolic functions, +- Classification functions, and +- Useful constants. + +Here is an example using some constants: ```python >>> import cmath From 38d37f86ff1f279c4d245fd1eb3b5a74cf55ae1a Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:14:44 -0700 Subject: [PATCH 23/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 1 + 1 file changed, 1 insertion(+) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index d9dde3fdfb..8ab2e4267b 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -168,6 +168,7 @@ It was strange and new in the 16th century. ### Why would anyone use these? It turns out that complex numbers are the simplest way to describe anything that rotates or anything with a wave-like property. +So they are used widely in electrical engineering, audio processing, physics, computer gaming, and navigation - to name only a few applications. You can see things rotate. Complex numbers may not make the world go round, but they are great for explaining what happens as a result of the world going round: look at any satellite image of a major storm. From bf81d3a631fd3aaa223a0e8200010135659f5474 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:15:22 -0700 Subject: [PATCH 24/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 8ab2e4267b..76251f0a2e 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -171,7 +171,8 @@ It turns out that complex numbers are the simplest way to describe anything that So they are used widely in electrical engineering, audio processing, physics, computer gaming, and navigation - to name only a few applications. You can see things rotate. -Complex numbers may not make the world go round, but they are great for explaining what happens as a result of the world going round: look at any satellite image of a major storm. +Complex numbers may not make the world go round, but they are great for explaining _what happens_ as a result of the world going round: look at any satellite image of a major storm. + Less obviously, sound is wave-like, light is wave-like, radio signals are wave-like, The economy of your home country is at least partly wave-like. From c8b8fc1960ff2b22417685cd1adce54fd040d57a Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:15:50 -0700 Subject: [PATCH 25/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 76251f0a2e..120cd19918 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -174,7 +174,8 @@ You can see things rotate. Complex numbers may not make the world go round, but they are great for explaining _what happens_ as a result of the world going round: look at any satellite image of a major storm. -Less obviously, sound is wave-like, light is wave-like, radio signals are wave-like, The economy of your home country is at least partly wave-like. +Less obviously, sound is wave-like, light is wave-like, radio signals are wave-like, and even the economy of your home country is at least partly wave-like. + A lot of this can be done with trig functions (`sin()` and `cos()`) but that gets messy quickly. Complex exponentials are ___much___ easier to work with. From f8ac3cd67aac035dc3e548efd89bb76e79bea2ac Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:16:14 -0700 Subject: [PATCH 26/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 120cd19918..d948af3e5d 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -177,7 +177,8 @@ Complex numbers may not make the world go round, but they are great for explaini Less obviously, sound is wave-like, light is wave-like, radio signals are wave-like, and even the economy of your home country is at least partly wave-like. -A lot of this can be done with trig functions (`sin()` and `cos()`) but that gets messy quickly. +A lot of this wave processing can be done with trig functions (`sin()` and `cos()`) but that gets messy quite quickly. + Complex exponentials are ___much___ easier to work with. ### But I never use complex numbers! From efd391bb344f316e1b9fcf91d411820f92cd1287 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:20:52 -0700 Subject: [PATCH 27/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index d948af3e5d..839f9c4212 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -181,7 +181,8 @@ A lot of this wave processing can be done with trig functions (`sin()` and `cos( Complex exponentials are ___much___ easier to work with. -### But I never use complex numbers! +### But I don't need complex numbers! + Only true if you are living in a cave and foraging for your food. From 1e59be5e125f6218fa8db6f652722aa4e1106cb2 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:21:20 -0700 Subject: [PATCH 28/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 839f9c4212..b69f25c6ea 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -186,7 +186,8 @@ Complex exponentials are ___much___ easier to work with. Only true if you are living in a cave and foraging for your food. -If you read this on any sort of screen, you are utterly dependent on some useful 20th-centry advances. +If you are reading this on any sort of screen, you are utterly dependent on some useful 20th-Century advances made through the use of complex numbers. + 1. __Semiconductor chips__. - These make no sense in classical physics and can only be explained (and designed) by quantum mechanics (QM). From 67d0653454ddd21e95eef3bbd7100628b40fd275 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 27 Nov 2023 08:23:27 -0700 Subject: [PATCH 29/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index b69f25c6ea..009265a368 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -191,7 +191,8 @@ If you are reading this on any sort of screen, you are utterly dependent on some 1. __Semiconductor chips__. - These make no sense in classical physics and can only be explained (and designed) by quantum mechanics (QM). - - In QM, everything is complex-valued by definition. + - In QM, everything is complex-valued by definition. (_its waveforms all the way down_) + 2. __The Fast Fourier Transform algorithm__. - FFT is an application of complex numbers, and it is in _everything_ connected to sound transmission, audio processing, photos, and video. From 93b26d8218f6e53ac508f32fc8163261abd19ceb Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Mon, 27 Nov 2023 11:05:35 -0700 Subject: [PATCH 30/42] fixed several links --- concepts/complex-numbers/about.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 009265a368..aa9f7f6f01 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -14,7 +14,7 @@ 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)` constructor takes two `float` parameters: +The [`complex(real, imag)`][complex] constructor takes two `float` parameters: ```python >>> z1 = complex(1.5, 2.0) @@ -208,3 +208,9 @@ So, you are probably using technology that relies on complex number calculations [complex]: https://docs.python.org/3/library/functions.html#complex [cmath]: https://docs.python.org/3/library/cmath.html [operators]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex +[math-module]: https://docs.python.org/3/library/math.html +[math-complex]: https://www.nagwa.com/en/videos/143121736364/ +[engineering-complex]: https://www.khanacademy.org/science/electrical-engineering/ee-circuit-analysis-topic/ee-ac-analysis/v/ee-complex-numbers +[ints]: https://docs.python.org/3/library/functions.html#int +[floats]: https://docs.python.org/3/library/functions.html#float + From 5b1dcf3f1e88524befb1e2d4f5eb4c9766222234 Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Tue, 28 Nov 2023 14:46:06 -0700 Subject: [PATCH 31/42] some relatively small fixes, mainly typos and formatting --- concepts/complex-numbers/.meta/config.json | 2 +- concepts/complex-numbers/about.md | 23 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/concepts/complex-numbers/.meta/config.json b/concepts/complex-numbers/.meta/config.json index 3d0539aece..e49f8494e2 100644 --- a/concepts/complex-numbers/.meta/config.json +++ b/concepts/complex-numbers/.meta/config.json @@ -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", "cmccandless", "colinleach"], + "authors": ["bethanyg", "colinleach"], "contributors": [] } diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index aa9f7f6f01..481863513e 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -26,7 +26,8 @@ Most engineers are happy with `j`. Most scientists and mathematicians prefer the mathematical notation `i`, but in designing Python the engineers won. -So there are two rules for an imaginary part: +So there are two rules for an imaginary part in Python: + - It is designated with `j` not `i`. - The `j` must immediately follow a number, to prevent Python seeing it as a variable name. If necessary, use `1j`. @@ -59,6 +60,19 @@ To access the parts individually: 1.5 ``` +Either part can be zero and mathematicians may then talk of the number being "wholly real" or "wholly imaginary". +However, it is still a complex number in Python. + +```python +>>> complex(0, 1) +1j +>>> type(complex(0, 1)) + + +>>> complex(1, 0) +(1+0j) +``` + ## Arithmetic Most of the [`operators`][operators] used with floats and ints also work with complex numbers: @@ -87,7 +101,7 @@ Most of the [`operators`][operators] used with floats and ints also work with co >>> 2 ** z1 # another exponentiation (0.5188946835878313+2.7804223253571183j) ->>> 1j ** 2 # j is the square root of -1 +>>> 1j ** 2 # j * j == -1 (-1+0j) ``` @@ -103,9 +117,8 @@ Integer division is ___not___ possible on complex numbers, so the `//` and `%` o There are two functions implemented for numeric types that are very useful when working with complex numbers: -- `.conjugate()` simply flips the sign of the imaginary part of a complex number (_from + to - or vice-versa_). - -Because of the way complex multiplication works, this is more useful than you might think. +- `.conjugate()` simply flips the sign of the imaginary part of a complex number (_from + to - or vice-versa_). + - Because of the way complex multiplication works, this is more useful than you might think. - `abs()` is guaranteed to return a real number with no imaginary part. From b674997274d49cb4c5e2eb28f8bcccca6139b85c Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Tue, 28 Nov 2023 15:12:16 -0700 Subject: [PATCH 32/42] Moved sections around, in the search for a clearer flow of ideas. --- concepts/complex-numbers/about.md | 33 ++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 481863513e..9a97eeebaa 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -14,7 +14,7 @@ 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) @@ -22,13 +22,18 @@ The [`complex(real, imag)`][complex] constructor takes two `float` parameters: (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 ` + j`, or just `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 @@ -44,13 +49,8 @@ NameError: name 'j' is not defined ``` -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 @@ -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: From 02867e1d1ffcf95112f5076e4b73121db0fe4d1c Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Tue, 28 Nov 2023 15:22:11 -0700 Subject: [PATCH 33/42] added bit on `complex(string)` constructor. --- concepts/complex-numbers/about.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 9a97eeebaa..bf2c02edea 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -22,6 +22,20 @@ There are two common ways to create complex numbers. (1.5+2j) ``` +The constructor can also parse string input. +This has the odd limitation that it fails if the string contains spaces. + +```python +>>> complex('4+2j') +(4+2j) + +>>> complex('4 + 2j') +Traceback (most recent call last): + File "", line 1, in +ValueError: complex() arg is a malformed string +``` + + 2) The complex number can be specified as ` + j`, or just `j` if the real part is zero: ```python From 89429253216e11b217e43ab5f84b5175e2db205d Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:02:08 -0600 Subject: [PATCH 34/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index bf2c02edea..4f35ea376f 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -36,7 +36,8 @@ ValueError: complex() arg is a malformed string ``` -2) The complex number can be specified as ` + j`, or just `j` if the real part is zero: +2) The complex number can be specified as ` + j` literal, or just `j` if the real part is zero: + ```python >>> z2 = 2.0 + 1.5j From 0dcf4033f7acebc4cbb3365ca63be90fb3a546e7 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:02:15 -0600 Subject: [PATCH 35/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 4f35ea376f..bdaf8a9fef 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -46,7 +46,8 @@ ValueError: complex() arg is a malformed string ``` The end result is identical to using a constructor. -There are two rules for an imaginary part in Python: +There are two rules for that imaginary part of the complex number: + - 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`. From 6e12c307b8f7b99d60a3b6424a7cee411160957d Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:02:58 -0600 Subject: [PATCH 36/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index bdaf8a9fef..58b443e3b6 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -49,7 +49,8 @@ The end result is identical to using a constructor. There are two rules for that imaginary part of the complex number: -- It is designated with `j` (not `i` as you may see in textbooks). +- It is designated with `j` (not `i` as you may see in math textbooks). + - The `j` must immediately follow a number, to prevent Python seeing it as a variable name. If necessary, use `1j`. ```python From d77353601287b78d771a0c96443c90586d427221 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:03:10 -0600 Subject: [PATCH 37/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 58b443e3b6..c9eade8834 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -44,7 +44,8 @@ ValueError: complex() arg is a malformed string >>> z2 (2+1.5j) ``` -The end result is identical to using a constructor. +The end result is identical to using the `complex()` constructor. + There are two rules for that imaginary part of the complex number: From 7113623560acd4970bf7641a41da1616bfd69cbc Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:03:23 -0600 Subject: [PATCH 38/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index c9eade8834..73faa58922 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -68,7 +68,9 @@ NameError: name 'j' is not defined ``` Most engineers are happy with `j`. -Most scientists and mathematicians prefer the mathematical notation `i`, but in designing Python the engineers won. +Most scientists and mathematicians prefer the mathematical notation `i` for _imaginary_, but that notation conflicts with the use of `i` to mean _current_ in Electrical Engineering. +So in designing Python, the Electrical Engineers won. + To access the parts individually: ```python From 4dfc09d98a6ccac0d7c75cc447e245dcb041552f Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:03:29 -0600 Subject: [PATCH 39/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index 73faa58922..a43488b1ca 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -72,7 +72,8 @@ Most scientists and mathematicians prefer the mathematical notation `i` for _ima So in designing Python, the Electrical Engineers won. -To access the parts individually: +To access the parts of a complex number individually: + ```python >>> z2.real 2.0 From a2aa792b1caca822ddeb6e6a2726325af326b047 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 28 Nov 2023 19:03:37 -0600 Subject: [PATCH 40/42] Update concepts/complex-numbers/about.md Co-authored-by: BethanyG --- concepts/complex-numbers/about.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/concepts/complex-numbers/about.md b/concepts/complex-numbers/about.md index a43488b1ca..dfe067be4e 100644 --- a/concepts/complex-numbers/about.md +++ b/concepts/complex-numbers/about.md @@ -82,7 +82,8 @@ To access the parts of a complex number individually: ``` Either part can be zero and mathematicians may then talk of the number being "wholly real" or "wholly imaginary". -However, it is still a complex number in Python. +However, it is still a complex number in Python: + ```python >>> complex(0, 1) From f648017739d9372b613bd0eab834771e14f75c85 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Tue, 5 Dec 2023 10:20:34 -0800 Subject: [PATCH 41/42] Edited About to serve as Introduction --- concepts/complex-numbers/.meta/config.json | 2 +- concepts/complex-numbers/introduction.md | 137 ++++++++------------- concepts/complex-numbers/links.json | 4 + 3 files changed, 55 insertions(+), 88 deletions(-) diff --git a/concepts/complex-numbers/.meta/config.json b/concepts/complex-numbers/.meta/config.json index e49f8494e2..ca6ccc8811 100644 --- a/concepts/complex-numbers/.meta/config.json +++ b/concepts/complex-numbers/.meta/config.json @@ -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": [] } diff --git a/concepts/complex-numbers/introduction.md b/concepts/complex-numbers/introduction.md index 0ac61d5987..587223db46 100644 --- a/concepts/complex-numbers/introduction.md +++ b/concepts/complex-numbers/introduction.md @@ -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 ``` -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 "", line 1, in + 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 ` + j` literal, or just `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 diff --git a/concepts/complex-numbers/links.json b/concepts/complex-numbers/links.json index 5428d4c8d2..759ef1689f 100644 --- a/concepts/complex-numbers/links.json +++ b/concepts/complex-numbers/links.json @@ -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." } ] From 86d4093c800c2e990c21e0a28d6c1927dbacab23 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Tue, 5 Dec 2023 10:23:31 -0800 Subject: [PATCH 42/42] Titled Introduction as Introduction --- concepts/complex-numbers/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/complex-numbers/introduction.md b/concepts/complex-numbers/introduction.md index 587223db46..a82f47cb6c 100644 --- a/concepts/complex-numbers/introduction.md +++ b/concepts/complex-numbers/introduction.md @@ -1,4 +1,4 @@ -# About +# Introduction `Complex numbers` are not complicated. They just need a less alarming name.