# Tutorial

We will solve the following problem using a computer to assist with the technical aspects:

```{admonition} Problem

The matrix $A$ is given by $A=\begin{pmatrix}a & 1 & 1\\ 1 & a & 1\\ 1 & 1 & 2\end{pmatrix}$.

1. Find the determinant of $A$
2. Hence find the values of $a$ for which $A$ is singular.
3. For the following values of $a$, when possible obtain $A ^ {- 1}$ and confirm
   the result by computing $AA^{-1}$:
    1. $a = 0$;
    2. $a = 1$;
    3. $a = 2$;
    4. $a = 3$.

```

`sympy` is once again the library we will use for this.

We will start by our matrix $A$:


```python
import sympy as sym

a = sym.Symbol("a")
A = sym.Matrix([[a, 1, 1], [1, a, 1], [1, 1, 2]])
```

We can now create a variable `determinant` and assign it the value of the
determinant of $A$:


```python
determinant = A.det()
determinant
```




$\displaystyle 2 a^{2} - 2 a$



A matrix is singular if it has determinant 0. We can find the values of $a$ for
which this occurs:


```python
sym.solveset(determinant, a)
```




$\displaystyle \left\{0, 1\right\}$



Thus it is not possible to find the inverse of $A$ for $a\in\{0, 1\}$.

However for $a = 2$:


```python
A.subs({a: 2})
```




$\displaystyle \left[\begin{matrix}2 & 1 & 1\\1 & 2 & 1\\1 & 1 & 2\end{matrix}\right]$




```python
A.subs({a: 2}).inv()
```




$\displaystyle \left[\begin{matrix}\frac{3}{4} & - \frac{1}{4} & - \frac{1}{4}\\- \frac{1}{4} & \frac{3}{4} & - \frac{1}{4}\\- \frac{1}{4} & - \frac{1}{4} & \frac{3}{4}\end{matrix}\right]$



To carry out matrix multiplication we use the `@` symbol:


```python
A.subs({a: 2}).inv() @ A.subs({a: 2})
```




$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$



and for $a = 3$:


```python
A.subs({a: 3}).inv()
```




$\displaystyle \left[\begin{matrix}\frac{5}{12} & - \frac{1}{12} & - \frac{1}{6}\\- \frac{1}{12} & \frac{5}{12} & - \frac{1}{6}\\- \frac{1}{6} & - \frac{1}{6} & \frac{2}{3}\end{matrix}\right]$




```python
A.subs({a: 3}).inv() @ A.subs({a: 3})
```




$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$



```{important}
In this tutorial we have

- Created a matrix.
- Calculated the determinant of the matrix.
- Substituted values in the matrix.
- Inverted the matrix.
```

55379c4199ba86e74f21c2ffed5a7996f9980b7e
ipynb
Jupyter Notebook
book/tools-for-mathematics/04-matrices/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
2020-09-24T21:02:41.000Z
2020-10-14T08:37:21.000Z
book/tools-for-mathematics/04-matrices/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
2020-09-21T15:54:23.000Z
2021-12-19T23:26:15.000Z
book/tools-for-mathematics/04-matrices/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
2020-10-02T09:21:27.000Z
2021-07-08T14:46:27.000Z
Qwen/Qwen-72B
1. YES
2. YES