Tutorial#

To demonstrate the ways in which a computer can assist with Algebra, in this tutorial you will solve the following two problems:

Problem

  1. Rationalise the denominator of \(\frac{1}{\sqrt{2} + 1}\)

  2. Consider the : \(f(x)=2x ^ 2 + x + 1\):

    1. Calculate the discriminant of the equation \(2x ^ 2 + x + 1 = 0\). What does this tell you about the solutions to the equation? What does this tell you about the graph of \(f(x)\)?

    2. By , show that the minimum point of \(f(x)\) is \(\left(-\frac{1}{4}, \frac{7}{8}\right)\)

To do this, a specific collection of tools available in Python will be used. Often specific sets of tools are separated in to things called libraries. Start by telling Python that you want to use the specific library for symbolic mathematics:

To do this, a specific collection of tools available in Python will be used. Often specific sets of tools are separated in to things called libraries. Start by telling Python that we want to use the specific library for symbolic mathematics:

import sympy

This will allow you to solve the first part of the question. Create a variable expression and assign it a value of \(\frac{1}{\sqrt{2} + 1}\).

expression = 1 / (sympy.sqrt(2) + 1)
expression
\[\displaystyle \frac{1}{1 + \sqrt{2}}\]

Attention

This is not what would happen if you plugged the above in to a basic calculator, it would instead give you a value of:

float(expression)
0.41421356237309503

The sympy library has a diverse set of tools available, one of which is to algorithmically attempt to simplify an expression. Here is how to do that:

sympy.simplify(expression)
\[\displaystyle -1 + \sqrt{2}\]

This implies that:

\[ \begin{split} \frac{1}{\sqrt{2} + 1} = -1 + \sqrt{2} \end{split} \]

Multiplying both sides by \({\sqrt{2} + 1}\) gives:

\[1=\frac{1}{\sqrt{2} + 1}\times \left(\sqrt{2} + 1\right) = \left(-1 + \sqrt{2}\right)\times \left(\sqrt{2} + 1\right)\]

The sympy.simplify command did not give much insight in to what happened but you can confirm the above manipulation by expanding \(\left(-1 + \sqrt{2}\right)\times \left(\sqrt{2} + 1\right)\). Here is how to do that:

sympy.expand((-1 + sympy.sqrt(2)) * (1 + sympy.sqrt(2)))
\[\displaystyle 1\]

The sympy library allows you to carry out basic expression manipulation. Now consider the second part of the question:

Problem

  1. Consider the : \(f(x)=2x ^ 2 + x + 1\):

  2. Calculate the of the equation \(2x ^ 2 + x + 1 = 0\). What does this tell you about the solutions to the equation? What does this tell you about the graph of \(f(x)\)?

  3. By , show that the minimum point of \(f(x)\) is \(\left(-\frac{1}{4}, \frac{7}{8}\right)\)

Start by reassigning the value of the variable expression to be the expression: \(2x ^ 2 + x + 1\).

x = sympy.Symbol("x")
expression = 2 * x ** 2 + x + 1
expression
\[\displaystyle 2 x^{2} + x + 1\]

Attention

Ths first line communicates to the code that x is going to be a symbolic variable.

Tip

Recall that the ** symbol is how you communicate exponentiation.

You can immediately use this to compute the discriminant:

sympy.discriminant(expression)
\[\displaystyle -7\]

Now, complement this with mathematical knowledge: if a has a negative discriminant then it does not have any roots and all the values are of the same sign as the coefficient of \(x ^ 2\). Which in this case is \(2>0\). Confirm this by directly creating the equation. Do this by creating a variable equation and assigning it the equation which has a lhs and a rhs:

equation = sympy.Eq(lhs=expression, rhs=0)
equation
\[\displaystyle 2 x^{2} + x + 1 = 0\]

Now ask sympy to solve it:

sympy.solveset(equation)
\[\displaystyle \left\{- \frac{1}{4} - \frac{\sqrt{7} i}{4}, - \frac{1}{4} + \frac{\sqrt{7} i}{4}\right\}\]

Indeed the only solutions are imaginary numbers: this confirms that the graph of \(f(x)\) is a convex parabola that is above the \(y=0\) line. Now complete the square so that you can write:

\[ f(x) = a (x - b) ^ 2 + c \]

for some values of \(a, b, c\). Create variables that have those 3 constants as value but also create a variable completed_square and assign it the general expression:

a, b, c = sympy.Symbol("a"), sympy.Symbol("b"), sympy.Symbol("c")
completed_square = a * (x - b) ** 2 + c
completed_square
\[\displaystyle a \left(- b + x\right)^{2} + c\]

Expand this:

sympy.expand(completed_square)
\[\displaystyle a b^{2} - 2 a b x + a x^{2} + c\]

Use sympy to solve the various equations that arise from comparing the coefficients of:

\[ f(x) = 2x ^2 + x + 1 \]

with the completed square. First, you see that the coefficient of \(x ^ 2\) gives you an equation:

\[ a = 2 \]

For completeness write the code that solves this trivial equation:

equation = sympy.Eq(a, 2)
sympy.solveset(equation, a)
\[\displaystyle \left\{2\right\}\]

Now substitute this value of \(a\) in to the completed square and update the variable with the new value:

completed_square = completed_square.subs({a: 2})
completed_square
\[\displaystyle c + 2 \left(- b + x\right)^{2}\]

Attention

The different types of brackets being used there: both () and {}. This is important and has specific meaning in Python which will be covered in future chapters.

Now look at the expression with the two remaining constants:

sympy.expand(completed_square)
\[\displaystyle 2 b^{2} - 4 b x + c + 2 x^{2}\]

Comparing the coefficients of \(x\) gives:

\[ - 4 b = 1 \]
equation = sympy.Eq(-4 * b, 1)
sympy.solveset(equation, b)
\[\displaystyle \left\{- \frac{1}{4}\right\}\]

Substitute this value of \(b\) back in to our expression.

Attention

Make a point to tell sympy to treat \(1 / 4\) symbolically and to not calculate the numeric value:

completed_square = completed_square.subs({b: -1 / sympy.S(4)})
completed_square
\[\displaystyle c + 2 \left(x + \frac{1}{4}\right)^{2}\]

Expand this to see the expression with the one remaining constant gives:

sympy.expand(completed_square)
\[\displaystyle c + 2 x^{2} + x + \frac{1}{8}\]

This gives a final equation for the constant term:

\[ c + 1 / 8 = 1 \]

Now use sympy to find the value of \(c\):

sympy.solveset(sympy.Eq(c + sympy.S(1) / 8, 1), c)
\[\displaystyle \left\{\frac{7}{8}\right\}\]

As before substitute in and update the value of completed_square:

completed_square = completed_square.subs({c: 7 / sympy.S(8)})
completed_square
\[\displaystyle 2 \left(x + \frac{1}{4}\right)^{2} + \frac{7}{8}\]

Using this shows that the there are indeed no values of \(x\) which give negative values of \(f(x)\) as \(f(x)\) is a square added to a constant. The minimum is when \(x=-1/4\) which gives: \(f(-1/4)=7/8\):

completed_square.subs({x: -1 / sympy.S(4)})
\[\displaystyle \frac{7}{8}\]

Important

This tutorial has:

  • Created symbolic expressions.

  • Obtained approximate values for numerical symbolic expressions.

  • Expanded and simplified symbolic expressions.

  • Created symbolic equations.

  • Solve symbolic equations.

  • Substituted values in to symbolic expressions.