4. Introducing symbolic algebra

4.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 x^3) e^{-x^2}\)”, I wish I could just say (with no effort):

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

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.

4.2. Preparing your computer to use sympy

4.2.1. On the web

I recommend you use sympy on your computer, but if you have to use it on the web then you can go to https://live.sympy.org/ and use their online notebook system.

The direct link to the live sympy calculator is:

https://live.sympy.org/

4.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
>>> import sympy

4.3. Getting started with a tutorial

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

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

4.4. 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{split}\begin{cases} & y & = & \frac{1}{2} x + 4.2 \\ & y & = & - 3 x - \frac{1}{2} \end{cases}\end{split}\]
  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{split}\begin{cases} & y & = & \frac{1}{3} x^2 - 2 x - 1 \\ & y & = & \frac{1}{2} x + 4.2 \end{cases}\end{split}\]
  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)