<a href="https://colab.research.google.com/github/vrry/learning-phyton/blob/master/03_03_Matrices.ipynb" target="_parent"></a>



# Introduction to Matrices
In general terms, a matrix is an array of numbers that are arranged into rows and columns.

## Matrices and Matrix Notation
A matrix arranges numbers into rows and columns, like this:

\begin{equation}A = \begin{bmatrix}
  1 & 2 & 3 \\
  4 & 5 & 6
 \end{bmatrix}
\end{equation}

Note that matrices are generally named as a capital letter. We refer to the *elements* of the matrix using the lower case equivalent with a subscript row and column indicator, like this:

\begin{equation}A = \begin{bmatrix}
  a_{1,1} & a_{1,2} & a_{1,3} \\
  a_{2,1} & a_{2,2} & a_{2,3}
 \end{bmatrix}
\end{equation}

In Python, you can define a matrix as a 2-dimensional *numpy.**array***, like this:


```python
import numpy as np

A = np.array([[1,2,3],
              [4,5,6]])
print (A)
```

    [[1 2 3]
     [4 5 6]]


You can also use the *numpy.**matrix*** type, which is a specialist subclass of ***array***:


```python
import numpy as np

M = np.matrix([[1,2,3],
               [4,5,6]])
print (M)
```

    [[1 2 3]
     [4 5 6]]


There are some differences in behavior between ***array*** and ***matrix*** types - particularly with regards to multiplication (which we'll explore later). You can use either, but most experienced Python programmers who need to work with both vectors and matrices tend to prefer the ***array*** type for consistency.

## Matrix Operations
Matrices support common arithmetic operations.

### Adding Matrices
To add two matrices of the same size together, just add the corresponding elements in each matrix:

\begin{equation}\begin{bmatrix}1 & 2 & 3 \\4 & 5 & 6\end{bmatrix}+ \begin{bmatrix}6 & 5 & 4 \\3 & 2 & 1\end{bmatrix} = \begin{bmatrix}7 & 7 & 7 \\7 & 7 & 7\end{bmatrix}\end{equation}

In this example, we're adding two matrices (let's call them ***A*** and ***B***). Each matrix has two rows of three columns (so we describe them as 2x3 matrices). Adding these will create a new matrix of the same dimensions with the values a<sub>1,1</sub> + b<sub>1,1</sub>, a<sub>1,2</sub> + b<sub>1,2</sub>, a<sub>1,3</sub> + b<sub>1,3</sub>,a<sub>2,1</sub> + b<sub>2,1</sub>, a<sub>2,2</sub> + b<sub>2,2</sub>, and a<sub>2,3</sub> + b<sub>2,3</sub>. In this instance, each pair of corresponding elements(1 and 6, 2, and 5, 3 and 4, etc.) adds up to 7.

Let's try that with Python:


```python
import numpy as np

A = np.array([[1,2,3],
              [4,5,6]])
B = np.array([[6,5,4],
              [3,2,1]])
print(A + B)
```

    [[7 7 7]
     [7 7 7]]


### Subtracting Matrices
Matrix subtraction works similarly to matrix addition:

\begin{equation}\begin{bmatrix}1 & 2 & 3 \\4 & 5 & 6\end{bmatrix}- \begin{bmatrix}6 & 5 & 4 \\3 & 2 & 1\end{bmatrix} = \begin{bmatrix}-5 & -3 & -1 \\1 & 3 & 5\end{bmatrix}\end{equation}

Here's the Python code to do this:


```python
import numpy as np

A = np.array([[1,2,3],
              [4,5,6]])
B = np.array([[6,5,4],
              [3,2,1]])
print (A - B)
```

    [[-5 -3 -1]
     [ 1  3  5]]


#### Conformability
In the previous examples, we were able to add and subtract the matrices, because the *operands* (the matrices we are operating on) are ***conformable*** for the specific operation (in this case, addition or subtraction). To be conformable for addition and subtraction, the operands must have the same number of rows and columns. There are different conformability requirements for other operations, such as multiplication; which we'll explore later.

### Negative Matrices
The nagative of a matrix, is just a matrix with the sign of each element reversed:

\begin{equation}C = \begin{bmatrix}-5 & -3 & -1 \\1 & 3 & 5\end{bmatrix}\end{equation}

\begin{equation}-C = \begin{bmatrix}5 & 3 & 1 \\-1 & -3 & -5\end{bmatrix}\end{equation}

Let's see that with Python:


```python
import numpy as np

C = np.array([[-5,-3,-1],
              [1,3,5]])
print (C)
print (-C)
```

    [[-5 -3 -1]
     [ 1  3  5]]
    [[ 5  3  1]
     [-1 -3 -5]]


### Matrix Transposition
You can *transpose* a matrix, that is switch the orientation of its rows and columns. You indicate this with a superscript **T**, like this:

\begin{equation}\begin{bmatrix}1 & 2 & 3 \\4 & 5 & 6\end{bmatrix}^{T} = \begin{bmatrix}1 & 4\\2 & 5\\3 & 6 \end{bmatrix}\end{equation}

In Python, both *numpy.**array*** and *numpy.**matrix*** have a **T** function:


```python
import numpy as np

A = np.array([[1,2,3],
              [4,5,6]])
print(A.T)
# or use
print('\n', A.transpose())
```

    [[1 4]
     [2 5]
     [3 6]]
    
     [[1 4]
     [2 5]
     [3 6]]



```python

```

9e9a20a81af0544e81df8ebf6bcac45d3a0225d8
ipynb
Jupyter Notebook
03_03_Matrices.ipynb
verryp/learning-phyton
103470ae49652755f146baaa353d3c72e6588a4a
MIT
03_03_Matrices.ipynb
verryp/learning-phyton
103470ae49652755f146baaa353d3c72e6588a4a
MIT
03_03_Matrices.ipynb
verryp/learning-phyton
103470ae49652755f146baaa353d3c72e6588a4a
MIT
Qwen/Qwen-72B
1. YES
2. YES