Tutorial
Tutorial#
To demonstrate the ways in which we can use a computer to assist with Algebra we will solve the following two problems:
Problem
Rationalise the denominator of \(\frac{1}{\sqrt{2} + 1}\)
Consider the quadratic: \(f(x)=2x ^ 2 + x + 1\):
Calculate the discriminant of the quadratic equation \(2x ^ 2 + x + 1 = 0\). What does this tell us about the solutions to the equation? What does this tell us about the graph of \(f(x)\)?
By completing the square, show that the minimum point of \(f(x)\) is \(\left(-\frac{1}{4}, \frac{7}{8}\right)\)
To do this we will be using a specific collections of tools available in Python. Often specific sets of tools are separated in to things called libraries. We start by telling Python that we want to use the specific library for symbolic mathematics:
import sympy
This allows us to solve the first part of the question. First we create a
variable expression
and assign it a value of \(\frac{1}{\sqrt{2} + 1}\).
expression = 1 / (sympy.sqrt(2) + 1)
expression
Attention
This is not what would happen if we plugged the above in to a basic calculator, it would instead give us 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 we do that:
sympy.simplify(expression)
This implies that:
If we multiply both sides by \({\sqrt{2} + 1}\) we get:
The sympy.simplify
command did not give us much insight in to what happened
but we can further confirm that above manipulation by expanding \(\left(-1 +
\sqrt{2}\right)\times \left(\sqrt{2} + 1\right)\).
Here is how we do that:
sympy.expand((-1 + sympy.sqrt(2)) * (1 + sympy.sqrt(2)))
We see that sympy
allows us to carry out basic expression manipulation. We
will now consider the second part of the question:
Problem
Consider the quadratic: \(f(x)=2x ^ 2 + x + 1\):
Calculate the discriminant of the quadratic equation \(2x ^ 2 + x + 1 = 0\). What does this tell us about the solutions to the equation? What does this tell us about the graph of \(f(x)\)?
By completing the square, show that the minimum point of \(f(x)\) is \(\left(-\frac{1}{4}, \frac{7}{8}\right)\)
We will 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
Tip
Recall that the **
symbol is how we communicate exponentiation.
Attention
We start by communicating to the code that x
is going to be a symbolic variable.
We can immediately use this to compute the discriminant:
sympy.discriminant(expression)
Now we can complement this with our mathematical knowledge: if a quadratic 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\).
We can confirm this by directly creating the equation. We 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
and asking sympy
to solve it:
sympy.solveset(equation)
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.
Let us know complete the square so that we can write:
for some values of \(a, b, c\).
First let us 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
We can expand this:
sympy.expand(completed_square)
We will now use sympy
to solve the various equations that arise from comparing
the coefficients of:
with the completed square.
First, we see that the coefficient of \(x ^ 2\) gives us an equation:
For completeness we will write the code that solves this trivial equation:
equation = sympy.Eq(a, 2)
sympy.solveset(equation, a)
We will 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
Attention
The different types of brackets being used there: both ()
and {}
. This is
important and has specific meaning in Python which we will cover soon.
Now let us look at the expression with our two remaining constants:
sympy.expand(completed_square)
Comparing the coefficients of \(x\) we have the equation:
equation = sympy.Eq(-4 * b, 1)
sympy.solveset(equation, b)
We will now substitute this value of \(b\) back in to our expression.
Attention
We 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
Expanding this to see our expression with the one remaining constant gives:
sympy.expand(completed_square)
We have a final equation for the constant term:
We will use sympy to find the value of \(c\):
sympy.solveset(sympy.Eq(c + sympy.S(1) / 8, 1), c)
As before we will substitute that in and update the value of completed_square
:
completed_square = completed_square.subs({c: 7 / sympy.S(8)})
completed_square
Using this we can see 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)})
Important
In this tutorial we have
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.