Tutorial
Tutorial#
To demonstrate the ways in which a computer can assist with Algebra, in this tutorial you will solve the following two problems:
Problem
Rationalise the denominator of \(\frac{1}{\sqrt{2} + 1}\)
Consider the : \(f(x)=2x ^ 2 + x + 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)\)?
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
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)
This implies that:
Multiplying both sides by \({\sqrt{2} + 1}\) gives:
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)))
The sympy
library allows you to carry out basic expression
manipulation. Now consider the second part of the question:
Problem
Consider the : \(f(x)=2x ^ 2 + x + 1\):
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)\)?
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
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)
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
Now ask 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. Now complete the square so that you can write:
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
Expand this:
sympy.expand(completed_square)
Use sympy
to solve the various equations that arise from comparing the
coefficients of:
with the completed square. First, you see that the coefficient of \(x ^ 2\) gives you an equation:
For completeness write the code that solves this trivial equation:
equation = sympy.Eq(a, 2)
sympy.solveset(equation, a)
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 will be
covered in future chapters.
Now look at the expression with the two remaining constants:
sympy.expand(completed_square)
Comparing the coefficients of \(x\) gives:
equation = sympy.Eq(-4 * b, 1)
sympy.solveset(equation, b)
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
Expand this to see the expression with the one remaining constant gives:
sympy.expand(completed_square)
This gives a final equation for the constant term:
Now use sympy to find the value of \(c\):
sympy.solveset(sympy.Eq(c + sympy.S(1) / 8, 1), c)
As before substitute in and update the value of completed_square
:
completed_square = completed_square.subs({c: 7 / sympy.S(8)})
completed_square
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)})
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.