<jupyter_start><jupyter_text>(algebra_tutorial)= TutorialTo demonstrate the ways in which we can use a computer to assist with Algebra wewill solve the following two problems:```{admonition} Problem1. Rationalise the denominator of $\frac{1}{\sqrt{2} + 1}$2. Consider the quadratic: $f(x)=2x ^ 2 + x + 1$:    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)$?    2. 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**. Westart by telling Python that we want to use the specific library for **symbolicmathematics**:<jupyter_code>import sympy<jupyter_output><empty_output><jupyter_text>This allows us to solve the first part of the question. First we create avariable `expression` and **assign** it a value of $\frac{1}{\sqrt{2} + 1}$.<jupyter_code>expression = 1 / (sympy.sqrt(2) + 1)
expression<jupyter_output><empty_output><jupyter_text>```{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:```<jupyter_code>float(expression)<jupyter_output><empty_output><jupyter_text>The `sympy` library has a diverse set of tools available, one of which is toalgorithmically attempt to simplify an expression. Here is how we do that:<jupyter_code>sympy.simplify(expression)<jupyter_output><empty_output><jupyter_text>This implies that:$$    \frac{1}{\sqrt{2} + 1} = -1 + \sqrt{2}$$If we multiply both sides by ${\sqrt{2} + 1}$ we get:$$    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 us much insight in to what happenedbut 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:<jupyter_code>sympy.expand((-1 + sympy.sqrt(2)) * (1 + sympy.sqrt(2)))<jupyter_output><empty_output><jupyter_text>We see that `sympy` allows us to carry out basic expression manipulation. Wewill now consider the second part of the question:```{admonition} Problem2. Consider the quadratic: $f(x)=2x ^ 2 + x + 1$:  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)$?  2. 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 theexpression: $2x ^ 2 + x + 1$.<jupyter_code>x = sympy.Symbol("x")
expression = 2 * x ** 2 + x + 1
expression<jupyter_output><empty_output><jupyter_text>```{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:<jupyter_code>sympy.discriminant(expression)<jupyter_output><empty_output><jupyter_text>Now we can complement this with our mathematical knowledge: if a quadratic has anegative discriminant then it does not have any roots and all the values are ofthe 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 avariable `equation` and assigning it the equation which has a `lhs` and a `rhs`:<jupyter_code>equation = sympy.Eq(lhs=expression, rhs=0)
equation<jupyter_output><empty_output><jupyter_text>and asking `sympy` to solve it:<jupyter_code>sympy.solveset(equation)<jupyter_output><empty_output><jupyter_text>Indeed the only solutions are imaginary numbers: the graph of $f(x)$ is thus a convex parabola.Let us know complete the square so that we can write:$$    f(x) = a (x - b) ^ 2 + c$$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:<jupyter_code>a, b, c = sympy.Symbol("a"), sympy.Symbol("b"), sympy.Symbol("c")
completed_square = a * (x - b) ** 2 + c
completed_square<jupyter_output><empty_output><jupyter_text>We can expand this:<jupyter_code>sympy.expand(completed_square)<jupyter_output><empty_output><jupyter_text>We will now use `sympy` to solve the various equations that arise from comparingthe coefficients of:$$    f(x) = 2x ^2 + x + 1$$with the completed square.First, we see that the coefficient of $x ^ 2$ gives us an equation:$$    a = 2$$For completeness we will write the code that solves this trivial equation:<jupyter_code>equation = sympy.Eq(a, 2)
sympy.solveset(equation, a)<jupyter_output><empty_output><jupyter_text>We will now substitute this value of $a$ in to the completed square and update the variable with the new value:<jupyter_code>completed_square = completed_square.subs({a: 2})
completed_square<jupyter_output><empty_output><jupyter_text>```{attention}The different types of brackets being used there: both `()` and `{}`. This isimportant and has specific meaning in Python which we will cover soon.```Now let us look at the expression with our two remaining constants:<jupyter_code>sympy.expand(completed_square)<jupyter_output><empty_output><jupyter_text>Comparing the coefficients of $x$ we have the equation:$$  - 4 b = 1$$<jupyter_code>equation = sympy.Eq(-4 * b, 1)
sympy.solveset(equation, b)<jupyter_output><empty_output><jupyter_text>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 notcalculate the numeric value:```<jupyter_code>completed_square = completed_square.subs({b: -1 / sympy.S(4)})
completed_square<jupyter_output><empty_output><jupyter_text>Expanding this to see our expression with the one remaining constant gives:<jupyter_code>sympy.expand(completed_square)<jupyter_output><empty_output><jupyter_text>We have a final equation for the constant term:$$    c + 1 / 8 = 1$$We will use sympy to find the value of $c$:<jupyter_code>sympy.solveset(sympy.Eq(c + sympy.S(1) / 8, 1), c)<jupyter_output><empty_output><jupyter_text>As before we will substitute that in and update the value of `completed_square`:<jupyter_code>completed_square = completed_square.subs({c: 7 / sympy.S(8)})
completed_square<jupyter_output><empty_output><jupyter_text>Using this we can see that the there are indeed no values of $x$ which givenegative 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$:<jupyter_code>completed_square.subs({x: -1 / sympy.S(4)})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11608047474308872, 'fraction_numerical': 0.01548704588218266, 'mean_word_length': 5.689254598257502, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '235870', 'license': 'MIT', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2165, 'n_words': 784, 'path': 'book/tools-for-mathematics/02-algebra/tutorial/.main.md.bcp.ipynb', 'repo_name': 'daffidwilde/pfm', 'size': '6909'}