Learn

Now that we know how to use NumPy arrays to create vectors and matrices, we can learn how to perform various linear algebra operations using Python.

To multiply a vector or matrix by a scalar, we use inbuilt Python multiplication between the NumPy array and the scalar:

A = np.array([[1,2],[3,4]])
4 * A

Written out mathematically, this is:

$4 \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}= \begin{bmatrix} 4 & 8 \\ 12 & 16 \end{bmatrix}$

Therefore, our output in Python is:

[[ 4  8]
[12 16]]

To add equally sized vectors or matrices, we can again use inbuilt Python addition between the NumPy arrays.

A = np.array([[1,2],[3,4]])
B = np.array([[-4,-3],[-2,-1]])
A + B

Written out mathematically, this is:

$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}+ \begin{bmatrix} -4 & -3 \\ -2 & -1 \end{bmatrix}= \begin{bmatrix} -3 & -1 \\ 1 & 3 \end{bmatrix}$

Therefore, our output in Python is:

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

Vector dot products can be computed using the np.dot() function:

v = np.array([-1,-2,-3])
u = np.array([2,2,2])
np.dot(v,u)

Written out mathematically, this is:

$\begin{bmatrix} -1 \\ -2\\ -3 \\ \end{bmatrix}\cdot \begin{bmatrix} 2 \\ 2 \\ 2 \end{bmatrix} = -12$

Therefore, our output in Python is:

-12

Matrix multiplication is computed using either the np.matmul() function or using the @ symbol as shorthand. It is important to note that using the typical Python multiplication symbol * will result in an elementwise multiplication instead.

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

# one way to matrix multiply
np.matmul(A,B)
# another way to matrix multiply
[email protected]

Written out mathematically, this is:

$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\cdot \begin{bmatrix} -4 & -3 \\ -2 & -1 \end{bmatrix}= \begin{bmatrix} -8 & -5 \\ -20 & -13 \end{bmatrix}$

Therefore, our Python output is:

[[ -8  -5]
[-20 -13]]

### Instructions

1.

In script.py, we are given three matrices.

Calculate the following equation using NumPy:

$D = 4A - 2B$

Save your equation to a variable called D and print it in the output terminal. Try calculating by hand before or after using Python to verify you understand how the operation works!

2.

Calculate the following matrix multiplication equation using NumPy:

$E = AC$

Save your equation to a variable called E and print it in the output terminal. Try calculating by hand before or after using Python to verify you understand how the operation works!

What is the shape of our resulting matrix? Why is this?

3.

Calculate the following matrix multiplication equation using NumPy:

$F = CA$

Save your equation to a variable called F and print it in the output terminal. Try calculating by hand before or after using Python to verify you understand how the operation works!

What is the shape of our resulting matrix? Is the shape of matrix F different from the shape of matrix E? Why is this?