# 1.2.1 - Basics of Linear Algebra

Last updated: February 16th, 2019

# Basics of Linear Algebra with numpy - Exercises¶

In [ ]:
import numpy as np


### Exercise 1¶

Using NumPy, create the following matrices:

• $A = \begin{bmatrix} 7 & 7 & 7 \\ 7 & 7 & 7 \\ 7 & 7 & 7 \\ \end{bmatrix}$

• $B = \begin{bmatrix} 5 & 3 & 1 \\ 7 & 2 & 8 \\ 4 & 6 & 9 \\ \end{bmatrix}$

• $C = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{bmatrix}$

In [ ]:
# your code goes here


You can try using np.array, np.full, np.identity and np.eye.

In [ ]:
A = np.full((3, 3), 7)

B = np.array([[5, 3, 1],
[7, 2, 8],
[4, 6, 9]])

C = np.identity(3) # also, np.eye(3, 3)


### Exercise 2¶

Perform a Matrix multiplication between your A and B matrices.

In [ ]:
# your code goes here


You can try using the @ operator or np.dot.

In [ ]:
A @ B

In [ ]:
np.dot(A, B)


### Exercise 3¶

Get the determinant of your A and B matrices.

In [ ]:
# your code goes here


You can try using np.linalg.det(matrix).

In [ ]:
np.linalg.det(A)

In [ ]:
np.linalg.det(B)


### Exercise 4¶

Get the inverse of your A and B matrices, if possible.

In [ ]:
# your code goes here


You can try using np.linalg.inv(matrix).

Note: Trying to calculate the inverse of A will raise an error, because it's a singular matrix (determinant == 0).

In [ ]:
np.linalg.inv(A)

In [ ]:
np.linalg.inv(B)


### Exercise 5¶

Create a Numpy array with 15 evenly spaced numbers over the $[-1, 1]$ interval.

In [ ]:
# your code goes here


You can try using np.linspace.

In [ ]:
np.linspace(-1, 1, 15)