```python
from sympy import *
init_printing(use_latex='mathjax')
x, y, z = symbols('x,y,z')
r, theta = symbols('r,theta', positive=True)
```

## Matrices

The SymPy `Matrix` object helps us with small problems in linear algebra.


```python
rot = Matrix([[r*cos(theta), -r*sin(theta)],
              [r*sin(theta),  r*cos(theta)]])
rot
```

### Standard methods


```python
rot.det()
```


```python
rot.inv()
```


```python
rot.singular_values()
```

### Exercise

Find the inverse of the following Matrix:

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


```python
# Create a matrix and use the `inv` method to find the inverse


```

### Operators

The standard SymPy operators work on matrices.


```python
rot * 2
```


```python
rot**2
```


```python
v = Matrix([[x], [y]])
v
```


```python
rot * v
```

### Exercise

In the last exercise you found the inverse of the following matrix


```python
M = Matrix([[1, x], [y, 1]])
M
```


```python
M.inv()
```

Now verify that this is the true inverse by multiplying the matrix times its inverse.  Do you get the identity matrix back?


```python
# Multiply `M` by its inverse.  Do you get back the identity matrix?

```

### Exercise

What are the eigenvectors and eigenvalues of `M`?


```python
# Find the methods to compute eigenvectors and eigenvalues. Use these methods on `M`


```

### NumPy-like Item access


```python
rot[0, 0]
```


```python
rot[:, 0]
```


```python
rot[1, :]
```

### Mutation

We can change elements in the matrix.


```python
rot[0, 0] += 1
rot
```


```python
simplify(rot.det())
```


```python
rot.singular_values()
```

### Exercise

Play around with your matrix `M`, manipulating elements in a NumPy like way.  Then try the various methods that we've talked about (or others).  See what sort of answers you get.


```python
# Play with matrices


```

6c4909176f1fb344ce5dc27380d9d04783219f17
ipynb
Jupyter Notebook
tutorial_exercises/04-Matrices.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/04-Matrices.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/04-Matrices.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES