# Algebra week¶

## First meeting¶

After this meeting students should:

Understand how to use the Sympy library to carry out basic Algebraic tasks.

Know what they need to do to prepare for their second tutorial.

### Problem¶

Explain to students that we will be solving the following problem:

Rationalise the following expression: \(\frac{1}{\sqrt{3} + 1}\).

Consider the quadratic \(f(x) = -x ^ 2 + 8 x - 18\).

Calculate the discriminant of the quadratic equation \(f(x)=0\). What does this tell us about the graph of \(f(x)\).

By completing the square, confirm that \((4, -2)\) is the maximum of point of \(f(x)\).

### Solution¶

Ask students to spend 5 minutes if they know/remember how to do this by hand. (This is just to get the students to think about it)

Now show how to get code to do this:

```
>>> import sympy
>>> expression = 1 / (sympy.sqrt(3) + 1)
>>> expression
1/(1 + sqrt(3))
>>> sympy.simplify(expression)
-1/2 + sqrt(3)/2
```

Discuss here how this differs if we used `math.sqrt`

. Explain that
`sympy.simplify`

is essentially acting as a black box here.

Now to carry out the rest of the problem:

```
>>> x = sympy.Symbol("x")
>>> expression = - x ** 2 + 8 * x - 18
>>> expression
-x**2 + 8*x - 18
>>> sympy.discriminant(expression)
-8
```

Confirm results by hand.

Discuss what this implies:

Quadratic equation has no real roots.

Graph does not intersect the \(y=0\) line.

Concave parabola (sign of leading coefficient of quadratic).

Confirm by solving the quadratic equation:

```
>>> equation = sympy.Eq(lhs=expression, rhs=0)
>>> equation
Eq(-x**2 + 8*x - 18, 0)
>>> sympy.solveset(expression, x)
FiniteSet(4 + sqrt(2)*I, 4 - sqrt(2)*I)
```

Now to move on to next part of the problem: completing the square:

```
>>> a, b, c = sympy.Symbol("a"), sympy.Symbol("b"), sympy.Symbol("c")
>>> completed_square = a * (x - b) ** 2 + c
>>> completed_square
a*(-b + x)**2 + c
```

Let us expand and compare the coefficients:

```
>>> sympy.expand(completed_square)
a*b**2 - 2*a*b*x + a*x**2 + c
```

We see that \(a\) is \(-1\). Let us substitute this value in to the expression:

```
>>> completed_square.subs({a: -1})
c - (-b + x)**2
```

We can in fact overwrite the expression:

```
>>> completed_square = completed_square.subs({a: -1})
>>> completed_square
c - (-b + x)**2
```

If we now expand again and compare coefficients:

```
>>> sympy.expand(completed_square)
-b**2 + 2*b*x + c - x**2
```

We see that \(2b=8\). Despite the fact that this equation is relatively
straightforward, let us solve it using `sympy`

:

```
>>> equation = sympy.Eq(lhs=2 * b, rhs=8)
>>> sympy.solveset(equation, b)
FiniteSet(4)
```

We will substitute this value for \(b\) back in to the completed square, and expand again:

```
>>> completed_square = completed_square.subs({b: 4})
>>> completed_square
c - (x - 4)**2
>>> sympy.expand(completed_square)
c - x**2 + 8*x - 16
```

We see that \(c - 16=-18\). Let us again solve that equation using \(sympy\):

```
>>> equation = sympy.Eq(lhs=c - 16, rhs= -18)
>>> sympy.solveset(equation, c)
FiniteSet(-2)
```

We will substitute this value back in:

```
>>> completed_square = completed_square.subs({c: -2})
>>> completed_square
-(x - 4)**2 - 2
>>> sympy.expand(completed_square)
-x**2 + 8*x - 18
```

Come back: with time take any questions.

Point at resources.

## After class email¶

Send the following email after class:

```
Hi all,
A recording of today's class is available at <>.
In this class I went over a demonstration of using Python to solve an
algebraic problem. I did the following mathematical techniques:
- Simplifying an exact numerical expression.
- Calculating the discriminant of a quadratic.
- Solving a symbolic equation.
- Substitute values in to a symbolic expression.
In preparation for your tutorial tomorrow please work through the second
chapter of the Python for mathematics book:
https://vknight.org/pfm/tools-for-mathematics/02-algebra/introduction/main.html
Please get in touch if I can assist with anything,
Vince
```