2. Introducing symbolic algebra

2.1. Wouldn’t it be nice…

You might have struggled with handling some long gnarly mathematical expressions, and you might have wondered either:

Wouldn’t it be nice if they just gave me the numbers, so I can punch them into a calculator, and I don’t have to figure out the general answer with all the \(x\) and \(y\) variables…

For example, if my boss asks me “find all the values of x for which this equation is true”:

\[\begin{split}& x^{5} + x^{4} \left(- 3 \pi - \sqrt{7} + 0.4\right) + x^{3} \left(-12.8 - 1.2 \pi - 0.4 \sqrt{7} + 3 \sqrt{7} \pi\right) \\ & + x^{2} \left(-19.2 + 1.2 \sqrt{7} \pi 12.8 \sqrt{7} + 38.4 \pi\right) \\ & + x \left(- 38.4 \sqrt{7} \pi + 19.2 \sqrt{7} + 57.6 \pi\right) - 57.6 \sqrt{7} \pi \\ & = 0\end{split}\]

I wish I could just say: “sure, boss, the solutions are \((2.64575131107, -2, 4, -2.4, 9.42477796077)\)”

Or wouldn’t it be nice if there were a computer program which could do all those algebra or calculus manipulations for me preserving the variables without plugging numbers in – all those messy expressions they had me do in algebra class…

For example, if my boss asked me: “find me the derivative of \(\sin(7 g x^3) e^{-(x-\mu)^2/(2\sigma^2)}\)”, I wish I could just say (with no effort):

\[\frac{d \left(\sin(7 g x^3) e^\frac{-(x-\mu)^2}{2\sigma^2}\right)}{dx} = 21 g x^{2} e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}} \cos{\left(7 g x^{3} \right)} - \frac{\left(- 2 \mu + 2 x\right) e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}} \sin{\left(7 g x^{3} \right)}}{2 \sigma^{2}}\]

The first of these wishes is the subject of numerical analysis, the second is the subject of symbolic math.

In this working group we will be working with a lot of algebraic expressions, and one way in which we will enhance this beyond what is done in school will be to use symbolic math.

We will conclude every segment with an interactive use of the SymPy symbolic algebra system to have the computer redo our hard-work calculations.

Note

That second problem, the symbolic math one, can be calculated like this with sympy:

g, mu, sigma = symbols('g mu sigma')
expr = sin(7*g*x**3) * exp(-(x-mu)**2 / (2*sigma**2))
expr
diff(expr, x)

and the result is:

\[\begin{split}&\frac{d e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}} \sin{\left(7 g x^{3} \right)}}{dx} \rightarrow \\ &21 g x^{2} e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}} \cos{\left(7 g x^{3} \right)} - \frac{\left(- 2 \mu + 2 x\right) e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}} \sin{\left(7 g x^{3} \right)}}{2 \sigma^{2}}\end{split}\]

2.2. Preparing your computer to use sympy

2.2.1. On the web

I recommend that at some point you learn to use sympy on your computer with a full python environment. But for the purpose of this course we will use the public web installation called SymPy Live Shell – https://live.sympy.org/ and enter expressions there.

2.2.2. On a desktop or laptop computer

On a debian-based linux distribution you can run:

$ sudo apt install python3-sympy

Alternatively, on just about any computer you can run:

$ pip3 install sympy

You should now be able to type python3 and then in the python interpreter:

>>> import sympy
>>> from sympy import *
>>> init_printing()
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

This will allow you to use \(x, y, z, t, k, m, n, f, g, h\) as symbolic variables. (Note that sometimes we will use ‘m’ as the slop in a straight line equation \(y = m x + b\), so we will have to redefine it with:

m, b = symbols('m b')

2.3. Getting started with a tutorial

We will work with the SymPy Live Shell and go through a bunch of expressions. They come from a tutorial and are well chosen, so we will work through them, discussing what it has done after we do each expression.

First we can warm up by putting in simple arithmetic, but we won’t do more than one or two expressions, like:

22 / 7
355 / 113

Then we will expand some expressions:

a, b, c = symbols('a b c')
(a + b)**2
expand((a + b)**2)
expand((a + b)**3)
expand((a + b)**4)
expand((a + b)**5)
expand((a + b)**6)
expand((a + b)**7)
expand((a + b)**8)

Then let us follow their algebra examples:

x
(x+2)/((x+3)*(x**2-4))
simplify((x**2 - 1)/((x+3)*(x-1)))

If you are curious about how that expression simplified one you can factor \(x^2 - 4\) in your head, or you can put into sympy:

factor(x**2 - 4)

A couple of not-too-useful ones:

div(x**2 - 4 + x, x-2)
# my comment for div(): polynomial division with remainders is
# not interesting
gcd(2*x**2 + 6*x, 12*x)
lcm(2*x**2 + 6*x, 12*x)
# my comment for gcd() and lcm(): this mostly goes to show that
# polynomials have a lot in common with integers - I make a brief
# passing mention of "rings" as an algebraic structure.

Now some polynomial and rational functions. Bring these up side-by-side, one at a time, in desmos and in sympy:

desmos: x^4 / 2 + 5 x^3 / 12 - x^2 / 3
sympy: factor(x**4/2 + 5*x**3/12 - x**2/3)
desmos: x^2 + 4 x y + 4 y^2 = 0
sympy: factor(x**2 + 4*x*y + 4*y**2)
desmos: x^2 + 4 x + 6
sympy: solve(x**2 + 4*x*y + 4*y**2, y)
desmos: x^3 + 4 x + 6
solve(x**3 + 4*x + 6, x)

desmos: y^2 - x^3 + 1 = 0
desmos: y + x^2 - 1 = 0
sympy: solve_poly_system([y**2 - x**3 + 1, y + x**2 - 1], x, y)

That last expression corresponds to the system of polynomial equations:

\begin{eqnarray} \begin{cases} y^2 - x^3 + 1 & = 0 \cr y + x^2 - 1 & = 0 \cr \end{cases} \end{eqnarray}

The solutions, as you see in sympy, are:

\[\begin{eqnarray} \begin{cases} \left( 1, \ 0\right) \cr \left( - \frac{\sqrt{57} \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{24} - \frac{\sqrt{57} \sqrt[3]{6 \sqrt{57} + 46}}{12} + \frac{3 \sqrt[3]{6 \sqrt{57} + 46}}{4} + \frac{3 \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{8}, \ - \frac{\sqrt[3]{6 \sqrt{57} + 46}}{3} - \frac{1}{3} - \frac{4}{3 \sqrt[3]{6 \sqrt{57} + 46}}\right), \cr \left( - \frac{3 \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16} - \frac{3 \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{\sqrt{57} \sqrt[3]{6 \sqrt{57} + 46}}{24} + \frac{\sqrt{57} \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{48} - \frac{\sqrt{19} i \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16} - \frac{3 \sqrt{3} i \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{\sqrt{19} i \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{3 \sqrt{3} i \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16}, \ - \frac{1}{3} - \frac{4}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{6 \sqrt{57} + 46}} - \frac{\left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{6 \sqrt{57} + 46}}{3}\right), \cr \ \left( - \frac{3 \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16} - \frac{3 \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{\sqrt{57} \sqrt[3]{6 \sqrt{57} + 46}}{24} + \frac{\sqrt{57} \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{48} - \frac{3 \sqrt{3} i \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16} - \frac{\sqrt{19} i \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{3 \sqrt{3} i \sqrt[3]{6 \sqrt{57} + 46}}{8} + \frac{\sqrt{19} i \left(6 \sqrt{57} + 46\right)^{\frac{2}{3}}}{16}, \ - \frac{1}{3} - \frac{\left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{6 \sqrt{57} + 46}}{3} - \frac{4}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{6 \sqrt{57} + 46}}\right) \cr \end{cases} \end{eqnarray}\]

Ouch: that second solution is much too big!

We laso note that a system of polynomial equations has several solutions - specifically, our equations have degrees 3 and 2, so we have up to \(3\times 2 = 6\) solutions.

This means that there can be more solutions than the two we saw in our plot. - in fact there are 5 solutions, two real and three complex.

Let us crank them out as numbers. One way to do that is to notice that python gives us the result in a list of lists, as you can see if you type print(result)

Then using sympy’s N() or .evalf() approaches we can see that a numerical approximation for result[1] (the second real point) is:

result[1][0]
result[1][0].evalf()
result[1][1]
result[1][1].evalf()
# or, more concisely using python list comprehensions:
[N(val) for val in result[1]]

So our second intersection point is \((1.76929235423863, -2.13039543476728)\) which seems to match what we see visually in desmos.

We can use nested list comprehensions to see them all:

[[N(val) for val in specific_result] for specific_result in result]

and we see that the 4 solutions are:

\[\begin{eqnarray} \begin{cases} ( 1.0, \ 0) \cr (1.76929235423863, \ -2.13039543476728) \cr (-0.884646177119316 + 0.589742805022206 i, \ 0.565197717383639 + 1.04342743589303 i) \cr (-0.884646177119316 - 0.589742805022206 i, \ 0.565197717383639 - 1.04342743589303 i) \end{cases} \end{eqnarray}\]

2.4. Further work

The sympy system is well documented. You can start from https://sympy.org/ and follow their documentation link to reach the tutorial page at https://docs.sympy.org/latest/tutorials/index.html#tutorials

The class now moves jumps away from this chapter to their tutorial, starting with the examples at:

https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html

and continue to their examples of simplification and factoring:

https://docs.sympy.org/latest/tutorials/intro-tutorial/simplification.html

We can stop now, since we will return to sympy examples as we cover those topics.

2.5. [unsorted] Some expressions we will do

  1. Simple systems of two linear equations with two unknows, starting with two equations in the form \(y = m x + b\), rewriting them as \(m x - y + b = 0\), and then putting them into sympy. Have the students pick all the coefficients. For example:

    \begin{eqnarray} \begin{cases} y \; = & \frac{1}{2} x + 4.2 \cr y \; = & - 3 x - \frac{1}{2} \cr \end{cases} \end{eqnarray}
  2. System of a linear equation and a quadratic equation. Have the students pick all the coefficients. You could keep one of the lines from before. For example:

    \begin{eqnarray} \begin{cases} y \; = & \frac{1}{3} x^2 - 2 x - 1 \cr y \; = & \frac{1}{2} x + 4.2 \cr \end{cases} \end{eqnarray}
  3. The famous gaussian integral:

    \[\int_{-\infty}^\infty e^{-x^2} dx\]
solve([2*x+a*y-z-1, -7*x+y+2*z+1, x-b*y-z+3], x, y, z)
  1. an integral:

integrate(x**2 + x + 1, x)
  1. prime numbers

nextprime(10**3)
nextprime(10**30)
nextprime(10**300)