Python package simplifies algebraic equations
Programming Snapshot – SymPy
Whether he's filling the bathtub with water or routing electricity through resistors – Mike Schilli juggles mathematical formulas with the assistance of the Python SymPy package.
I still remember a traumatic event as an elementary school student: A weekend newspaper had set a logic puzzle for kids, the solution of which it promised to publish in the next issue a week later. It involved a bathtub with two taps: one of which filled the tub in 10 minutes, the other in 15. The question was how long would it take to fill the tub if both taps were turned on all the way.
As a little boy, I was absolutely sure that 10 plus 15 equals 25, which is 25 minutes. My father laughed and suggested that couldn't be true, because two taps would fill the bathtub faster than one alone. The next weekend, I was initially triumphant, because there it was – printed in black and white in the following issue – the confirmation that 25 minutes was the correct solution.
But disillusionment followed one week later: After receiving angry reader comments, the newspaper had to admit that it had made a mistake, because it does not take 25 minutes, but only 6 minutes, to fill the tub with both taps. I nearly fell off my chair and decided at that point to become a famous columnist peddling logic puzzles.
Faucets and Resistors
It was only much later, in college, during a lecture on circuit technology, that I came across a similar problem, which somehow translated nicely to a solution to the bathtub problem: parallel arrangement of resistors in a circuit. The electrical engineer measures the resistance of a conductor in ohms, and the higher the value, the more it slows down the flow of electrical current.
Now how do you calculate the equivalent resistance of a parallel circuit with two resistors R1 and R2 as shown in Figure 1? The solution involves the currents I1 and I2 flowing through each resistor. They add up to the total current I after the two branches are reunited. The measured voltage is the same everywhere, that is V, and according to Ohm's law V = R*I. Ergo, with individual currents I1 and I2 adding up to I, the result comes out as V/R1 + V/R2.
If we now replace the two parallel resistors R1 and R2 with a substitute resistor R, we also get I = V/R for the total current, so this gets us: V/R = V/R1 + V/R2. The voltage V can be canceled out to give 1/R = 1/R1 + 1/R2 (Figure 2). After transformation, the following result appears:
R = (R1*R2)/(R1+R2)
Today, computer programs perform the computational work instead of the engineer doing it manually. Listing 1 [1] shows the development of the formula using SymPy, a symbolic algebra package in Python. It can be used to define symbols, which the package later leaves intact when evaluating formulas, instead of immediately replacing variables with values and determining the formula's numerical result. With the simplify()
function, SymPy can simplify expressions using the rules of algebra – just as any mathematically skilled person would do.
Listing 1
parallel.py
01 #!/usr/bin/env python3 02 from sympy import simplify, symbols, pprint 03 r, r1, r2, v, i1, i2 = symbols("r r1 r2 v i1 i2") 04 i1 = v / r1 05 i2 = v / r2 06 r = v / (i1 + i2) 07 pprint(simplify(r))
Thus, line 3 in Listing 1 defines a whole slew of symbols. For the two resistors of the circuit and their equivalent resistance, there are r1
, r2
, and r
. For the applied voltage, there is v
; for the two current components, there are i1
and i2
. The formulas in lines 4 to 6 apply Ohm's law and sum the partial currents to receive at the total current.
The resulting formula for the equivalent resistance r
comes to light when the script is called using the pprint()
(pretty print) function (Figure 3). SymPy has obviously realized that the voltage v
can be canceled out of the fraction, and the result no longer depends on it.
If you use Python 3, you can install packages like sympy and matplotlib, which I'll be using in a moment to illustrate results, easily with the command:
pip3 install <package>
In order to avoid pulling the carpet out from under the feet of other Python scripts on the same host, I prefer doing that in Python's virtual environment [2].
From Circuit to Bathtub
This formula also works for the bathtub problem mentioned earlier: In the numerator, the filling times per tap get multiplied (10*15), and the denominator holds their sum (10+15). According to Adam Riese, this comes out to w150/25 (i.e., six minutes), just like in the example solution in the Sunday paper.
By the way, you can also think of the solution as follows: After one minute, the first tap has filled the tub to one tenth and the second to one fifteenth. Both together fill in one minute 1/10 + 1/15 = 3/30 + 2/30 = 5/30 = 1/6 of the tub, so you can get into the bath after six minutes.
Borderline
What happens if both resistors in the parallel circuit drop down to 0 ohms – that is, they do not slow down the current flow at all, or, in the case of a bathtub with Niagara Falls-style faucets, fill the tub in next to no time? Intuitively, it is clear that this kind of mega-faucet would fill the tub practically as fast when connected in parallel as when operated individually. But if you set the values for (R1*R2)/(R1+R2) in the formula (R1*R2)/(R2)/(R1+R2) to zero and pass the construct to a Python program for computing, you will experience a fatal error: Computers steadfastly refuse to perform divisions by zero, because it is mathematically undefined.
In the present case, however, both the numerator and the denominator contain a value tending towards zero, which sometimes yields interesting (because finite) results. Mind you, a true zero as denominator defies mathematical definition, but the limit for R1 and R2 approaching zero can definitely be computed.
SymPy provides the limit()
function, which takes a symbolic formula, a symbol (such as r1
) and a limit (here
). Listing 2 defines r1 = r2
beforehand, meaning that both variables in the limit()
function tend towards zero. The result for the equivalent resistor, as the values for r1
and r2
approach zero, is that the script returns, as expected, a value of zero (Listing 3).
Listing 2
limit.py
01 #!/usr/bin/env python3 02 from sympy import limit, symbols 03 r1, r2, r = symbols("r1 r2 r") 04 05 r = (r1 * r2) / (r1 + r2) 06 r1 = r2 07 08 # r1/2->0 09 print(limit(r, r1, 0)) 10 11 # 1/x with x->0 12 x = symbols("x") 13 print(limit(1/x, x, 0))
Listing 3
Limit Value
$ ./limit.py 0 oo
The second test case, starting from line 12 in Listing 2, illustrates with an example what happens to another formula, 1/x
, when x
tends towards zero. The output in Listing 3 shows here that the result of the formula tends to infinity in this case, which is what the ASCII output oo
tries to illustrate.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.