STEM with Python – Part 6: Algebra – Equations

Time to learn SymPy.

Pythion’s real value can be seen when you take advantage of the language’s rich resource of STEM libraries. SymPy is one of them. Since I’m currently learning algebra by myself, my Pythin-learning path collides its path with the other. STEM forever!!

Since I’ve spent a fair share of my private time on learning Python lately (I have to learn Kotlin and Java more, though), it’s time to dive into the language’s rich resource of mathematical libraries that make it shine in the crowd.

What we have done in the previous Python’s mathematical posts was something you can do in other languages since they didn’t take advantage of its libraries. Now, let’s start utilizing Python’s mathematically powerful libraries that define Python Python.

In addition to that, as I mentioned before, my other STEM series, Algebra, perfectly collides its path with this Pythion series. So, this is a great win-win situation for my STEM universe!

From this point on, just follow the instructions, and you’ll be fine (I guess).

Install SymPy:

johnito@skynewubuntuserver:~$ pip install sympy
Collecting sympy
  Downloading sympy-1.11.1-py3-none-any.whl (6.5 MB)
     |████████████████████████████████| 6.5 MB 2.9 MB/s
Collecting mpmath>=0.19
  Downloading mpmath-1.2.1-py3-none-any.whl (532 kB)
     |████████████████████████████████| 532 kB 12.1 MB/s
Installing collected packages: mpmath, sympy

Equations with one solution:

A simple equation that contains one variable, such as x-4-2=0, can be solved with the SYmPy’s solve() function.

image 01 (equation1.py)

Actual code:

from sympy import symbols, solve

x = symbols('x')
expr = x-4-2

sol = solve(expr)

print(sol)

The output:

[6]

Another example:

This time, we’ll use Eq() function.

image 02 (equation2.py)

Actual code:

from sympy import symbols, Eq, solve

y = symbols('y')
eq1 = Eq(y + 3 + 8)

sol = solve(eq1)

print(sol)

The output:

equation2.py:4: SymPyDeprecationWarning:

Eq(expr) with a single argument with the right-hand side
defaulting to 0 is deprecated. Use Eq(expr, 0) instead.

See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-eq-expr
for details.

This has been deprecated since SymPy version 1.5. It
will be removed in a future version of SymPy.

  eq1 = Eq(y + 3 + 8)
[-11]

Equations with two solutions:

Here’s a quadratic equation sample: x² -5x + 6 = 0

Two solutions will be the output result.

image 03 (equation3.py)

Actual code:

from sympy import symbols, Eq, solve

x = symbols('x')
eq1 = Eq(x**2-5*x+6)

sol = solve(eq1)

print(sol)

The output:

equation3.py:4: SymPyDeprecationWarning:

Eq(expr) with a single argument with the right-hand side
defaulting to 0 is deprecated. Use Eq(expr, 0) instead.

See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-eq-expr
for details.

This has been deprecated since SymPy version 1.5. It
will be removed in a future version of SymPy.

  eq1 = Eq(x**2-5*x+6)
[2, 3]

Leave a Reply