# Matrix

## Create a matrix $$\begin{bmatrix} 1 & 2 \\ -2 & 3 \\ 3 & 4 \end{bmatrix}$$


```python
# Create a matrix
from sympy import *

init_printing(use_unicode=True)

Matrix([[1, 2], [-2, 3], [3, 4]])
```




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



## Create some matrix
$$M = \begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix}$$
$$N = \begin{bmatrix} -1 \\ 1 \end{bmatrix}$$


```python
# Manipulate the matrix
M = Matrix([[1, 2], [-2, 1]])
N = Matrix([-1, 1])
N
```




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



## Manipulate the matrix
$$M \times N$$


```python
M*N
```




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




```python
# Basic Operations
# Get the shape of the matrix M
shape(M)
```


```python
# Accessing rows
M.row(0)
```




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




```python
# Accessing columns
M.col(-1)
```




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



## Addition of M and N $$\begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix} + \begin{bmatrix} -1 & 1 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 0 & 3 \\ -1 & 3 \end{bmatrix}$$


```python
# Addition of matrices
M = Matrix([[1, 2], [-2, 1]])
N = Matrix([[-1, 1], [1, 2]])
M + N
```




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



## Multiplication of M and N $$\begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix} \times \begin{bmatrix} -1 & 1 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 1 & 5 \\ 3 & 0 \end{bmatrix}$$


```python
# Multiplication of matrices
M*N
```




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



## Power of matrix M $$\begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix}^2 = \begin{bmatrix} -3 & 4 \\ -4 & -3 \end{bmatrix}$$


```python
# Power of a matrix
M**2
```




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



## Inverse of matrix M $$\begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix}^{-1} = \begin{bmatrix} \frac{1}{5} & -\frac{2}{5} \\ \frac{2}{5} & \frac{1}{5} \end{bmatrix}$$


```python
# Inverse of a matrix
M**-1
```




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



## Transpose of matrix M $$MT = \begin{bmatrix} 1 & -2 \\ 2 & 1 \end{bmatrix}$$


```python
# Transpose of a matrix
M.T
```




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



## Create a matrix identity $$\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{bmatrix}$$


```python
# Matrix Constructor
# Identity Matrix
eye(3)
```




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



## Create all zeros matrix $$\begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix}$$


```python
# All zeros matrix
zeros(2, 2)
```




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



## Create all ones matrix $$\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix}$$


```python
# All ones matrix
ones(2, 2)
```




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



## Create a diagonal matrix $$\begin{bmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{bmatrix}$$


```python
# Diagonal Matrix
diag(1, 2, 3)
```




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



## Determinant of matrix M $$a \times d - b \times c$$ $$=1 \times 1 - 2 \times (-2) = 5$$


```python
# Determinant of a matrix
M.det()
```

2332c1142f4905f60f732795fa91e508362be3bc
ipynb
Jupyter Notebook
matrices.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
2021-11-30T10:05:29.000Z
2021-12-30T13:39:08.000Z
matrices.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
matrices.ipynb
ricoen/learn-math
fc84bc4d0dc353f8ccb3c52e36155069e9ca5af4
MIT
Qwen/Qwen-72B
1. YES
2. YES