Monday, June 21, 2021

Real Python: Simplify Complex Numbers With Python

Most general-purpose programming languages have either no support or limited support for complex numbers. Your typical options are learning some specialized tool like MATLAB or finding a third-party library. Python is a rare exception because it comes with complex numbers built in.

Despite the name, complex numbers aren’t complicated! They’re convenient in tackling practical problems that you’ll get a taste of in this tutorial. You’ll explore vector graphics and sound frequency analysis, but complex numbers can also help in drawing fractals, for example.

In this tutorial, you’ll learn how to:

  • Define complex numbers with literals in Python
  • Represent complex numbers in rectangular and polar coordinates
  • Use complex numbers in arithmetic expressions
  • Take advantage of the built-in cmath module
  • Translate mathematical formulas directly to Python code

If you need a quick refresher or a gentle introduction to the theory of complex numbers, then you can watch Khan Academy’s video series. To download the sample code used throughout this tutorial, click the link below:

Get Sample Code: Click here to get the sample code you’ll use to learn about complex numbers in Python in this tutorial.

Creating Complex Numbers in Python

Creating and manipulating complex numbers in Python isn’t much different from other built-in data types, particularly numeric types. It’s possible because the language treats them as first-class citizens. This means you can express mathematical formulas that involve complex numbers with little overhead.

Python lets you use complex numbers in arithmetic expressions and call functions on them just like you would with other numbers in Python. It leads to elegant syntax that reads almost like a math textbook.

Complex Number Literal

The quickest way to define a complex number in Python is by typing its literal directly in the source code:

>>>
>>> z = 3 + 2j

Although this looks like an algebraic formula, the expression to the right of the equals sign is already a fixed value that needs no further evaluation. When you check its type, you’ll confirm that it’s indeed a complex number:

>>>
>>> type(z)
<class 'complex'>

How is that different from adding two numbers with the plus operator? A clear giveaway is the letter j glued to the second number, which completely changes the meaning of the expression. If you removed the letter, you’d get a familiar integer result instead:

>>>
>>> z = 3 + 2

>>> type(z)
<class 'int'>

By the way, you can use floating-point numbers to create complex numbers, too:

>>>
>>> z = 3.14 + 2.71j
>>> type(z)
<class 'complex'>

Complex number literals in Python mimic the mathematical notation, which is also known as the standard form, the algebraic form, or sometimes the canonical form, of a complex number. In Python, you can use either lowercase j or uppercase J in those literals.

If you learned about complex numbers in math class, you might have seen them expressed using an i instead of a j. If you’re curious about why Python uses j instead of i, then you can expand the collapsible section below to learn more.

The traditional notation for complex numbers uses the letter i instead of j since it stands for the imaginary unit. You might feel a slight discomfort with Python’s convention if you have a mathematical background. However, there are a few reasons that can justify Python’s controversial choice:

  • It’s a convention already adopted by engineers to avoid name collisions with electric current, which is denoted with the letter i.
  • In computing, the letter i is often used for the indexing variable in loops.
  • The letter i can be easily confused with l or 1 in source code.

This was brought up on Python’s bug tracker over a decade ago, and Python’s creator, Guido van Rossum himself, closed the issue with this comment:

This will not be fixed. For one thing, the letter ‘i’ or upper case ‘I’ look too much like digits. The way numbers are parsed either by the language parser (in source code) or by the built-in functions (int, float, complex) should not be localizable or configurable in any way; that’s asking for huge disappointments down the road. If you want to parse complex numbers using ‘i’ instead of ‘j’, you have plenty of solutions available already. (Source)

So there you have it. Unless you want to start using MATLAB, you’ll have to live with using j to denote your complex numbers.

The algebraic form of a complex number follows the standard rules of algebra, which is convenient in performing arithmetic. For example, addition has a commutative property, which lets you swap the order of the two parts of a complex number literal without changing its value:

>>>
>>> 3 + 2j == 2j + 3
True

Similarly, you can substitute addition for subtraction in a complex number literal because the minus sign is just a shorthand notation for an equivalent form:

>>>
>>> 3 - 2j == 3 + (-2j)
True

Read the full article at https://realpython.com/python-complex-numbers/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...