Solving Algebra Problems using Python’s Symbolic Math

Jan Barrera
4 min readJul 24, 2020
High school algebra textbook published by McGraw-Hill ©2010.

We had a lot of high school and university textbooks lying around the house and I thought that I should try out Sympy, python’s symbolic math library in solving several algebra problems.

Symbolic computation has been around for several years already. Maple, Matlab, MathCad, and Mathematica have their own symbolic libraries since the early 2000's, albeit not free (and costly at that). Sympy on the other hand is full open-source and even has an online web application — https://live.sympy.org, where you can use for free.

Sympy comes with the Anaconda package and can readily be used with Jupyter or Spyder. Just download Anaconda from here.

The following are selected algebra problems solved with a few lines of python programming using Sympy library.

Solving System of Equations

A system of equations can be solved by initially solving 1 variable and then using substitution to solve for the other variables.

In Sympy, we can use symbols, Eq and solve.

from sympy import Eq
from sympy import symbols
from sympy import solve

Setup the symbolic equations

x,y,z = symbols('x y z')
eq1 = Eq(-5*x + y - 4*z, 60)
eq2 = Eq(2*x + 4*y + 3*z, -12)
eq3 = Eq(6*x - 3*y - 2*z, -52)

Then let Sympy solve the equations

solve([eq1,eq2, eq3],(x,y,z))

and we get… {x: -8, y: 4, z: -4}. That was pretty easy with just 8 lines of code!

Simplifying Polynomials

We can simplify polynomials by distributing the terms and then performing the necessary operations.

In Sympy, we just use symbols and simplify.

from sympy import symbols
from sympy import simplify

Then we properly translate the polynomial and then let Sympy do the distributing.

r=symbols('r')
simplify( (4*r**3 - 8*r**2 - 13*r + 20) / (2*r -5) )

and we get

Factoring Polynomials

Here is a challenge problem. Factoring a polynomial requires determining the GCF or the greatest common factor.

In Sympy, we can use symbols and factor.

from sympy import symbols
from sympy import factor

Then let Sympy factor the polynomial

x,n = symbols('x n')
factor(36*x**(2*n) + 12*x**n + 1)

and we get

Solving for Roots of Polynomials

Here is a word problem that involves solving for polynomial roots. Solving this type of problem requires factoring and equating the factors to zero to get the roots.

Solving in Sympy just requires setting up the symbols and then solve.

from sympy import symbols
from sympy import factor

Then let Sympy solve the equation

t = symbols('t')
solve(-t**4 + 9*t**2 + 400)

and the roots are: -5, 5, -4i, 4i. Time should be a positive real number so it would take 5 years when the population becomes zero.

Solving Trigonometric Identities

Solving trigonometric identities requires knowledge of several formulas, such as Pythagorean identities, reciprocal identities and quotient identities.

We however can just symbols, trigonometric functions and simplify.

from sympy import symbols
from sympy import simplify
from sympy import sin
from sympy import cos
from sympy import csc

Then let Sympy figure it out.

x=symbols('x')
simplify(sin(x)*csc(x) - cos(x)**2)

and we get

Mathematics teaches us to develop analytical thinking in solving problems. Most solutions to common problems however are procedural and can be approached via alternative means… and maybe one of them is programming in symbols.

--

--