# How

## Create a matrix

We create a matrix using the `sympy.Matrix` tool. We combine this with
square brackets `[]` which we nest so that every row is also inside square
brackets.

````{tip}
```
sympy.Matrix([values])
```
````

For example, the following creates the matrix:

$$
    B = \begin{pmatrix}
            1 & 2 & 3 & 4\\
            5 & 6 & 7 & 8\\
            9 & 10 & 11 & 12
        \end{pmatrix}
$$


```python
import sympy as sym

B = sym.Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
B
```




$\displaystyle \left[\begin{matrix}1 & 2 & 3 & 4\\5 & 6 & 7 & 8\\9 & 10 & 11 & 12\end{matrix}\right]$



```{attention}
It is possible to write the code in a more readable way as long as an incomplete
line ends with an open bracket:
```


```
B = sym.Matrix(
    [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12]
    ]
)
```

## Calculate the determinant of a matrix

To calculate the determinant of a matrix, we use the `.det` tool. For example to
calculate the determinant of:

````{tip}
```
matrix = sympy.Matrix([values])
matrix.det()
```
````

For example, the determinant of the following matrix:

$$
    \begin{pmatrix}
    1 & 5\\
    5 & 1
    \end{pmatrix}
$$


```python
matrix = sym.Matrix([[1, 5], [5, 1]])
matrix.det()
```




$\displaystyle -24$



## Calculate the inverse of a matrix

To calculate the inverse of a matrix, we use the `.inv` tool.

````{tip}
```
matrix = sympy.Matrix([values])
matrix.inv()
```
````

For example to
calculate the inverse of:

$$
    \begin{pmatrix}
        1 / 2 & 1\\
        5     & 0
    \end{pmatrix}
$$


```python
matrix = sym.Matrix([[sym.S(1) / 2, 1], [5, 0]])
matrix.inv()
```




$\displaystyle \left[\begin{matrix}0 & \frac{1}{5}\\1 & - \frac{1}{10}\end{matrix}\right]$



## Multiply matrices by a scalar

To multiple a matrix by a scalar we use the `*` operator. For example to
multiply the following matrix by $6$:

$$
    \begin{pmatrix}
        1 / 5 & 1\\
        1 & 1
    \end{pmatrix}
$$


```python
matrix = sym.Matrix([[sym.S(1) / 5, 1], [1, 1]])
6 * matrix
```




$\displaystyle \left[\begin{matrix}\frac{6}{5} & 6\\6 & 6\end{matrix}\right]$



## Add matrices together

To add matrices we use the `+` operator. For example to compute:

$$
    \begin{pmatrix}
        1 / 5 & 1\\
        1 & 1
    \end{pmatrix} +
    \begin{pmatrix}
        4 / 5 & 0\\
        0 & 0
    \end{pmatrix}
$$


```python
matrix = sym.Matrix([[sym.S(1) / 5, 1], [1, 1]])
other_matrix = sym.Matrix([[sym.S(4) / 5, 0], [0, 0]])
matrix + other_matrix
```




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



## Multiply matrices together

To multiply matrices together we use the `@` operator. For example to compute:


$$
    \begin{pmatrix}
        1 / 5 & 1\\
        1 & 1
    \end{pmatrix}
    \begin{pmatrix}
        4 / 5 & 0\\
        0 & 0
    \end{pmatrix}
$$


```python
matrix @ other_matrix
```




$\displaystyle \left[\begin{matrix}\frac{4}{25} & 0\\\frac{4}{5} & 0\end{matrix}\right]$



## How to create a vector

A vector is essentially a matrix with a single row or column. For example to
create the vector:

$$
    \begin{pmatrix}
    3 \\
    2 \\
    1
    \end{pmatrix}
$$


```python
vector = sym.Matrix([[3], [2], [1]])
vector
```




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



## How to solve a linear system

To solve a given linear system that can be represented in matrix form, we create
the corresponding matrix and vector and use the matrix inverse. For example to
solve the following equations:

$$
    \begin{array}
        a x + 2y = 3\\
        3x + y + 2z = 4\\
        - y + z = 1\\
    \end{array}
$$


```python
A = sym.Matrix([[1, 2, 0], [3, 1, 2], [0, -1, 1]])
b = sym.Matrix([[3], [4], [1]])
A.inv() @ b
```




$\displaystyle \left[\begin{matrix}- \frac{5}{3}\\\frac{7}{3}\\\frac{10}{3}\end{matrix}\right]$



1ca9051b76c3206ff4078b300783d4a8bbf75cb6
ipynb
Jupyter Notebook
book/tools-for-mathematics/04-matrices/how/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/04-matrices/how/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/04-matrices/how/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
Qwen/Qwen-72B
1. YES
2. YES