1. Vectors

import numpy as np
# Make a column vector
v = np.array([[3], [1], [-7]])
# Take the transpose
v.T;
# What is the shape of this object?
v = np.array([[3, 1, -7]])
# ... versus the shape of this object?
v = np.array([3, 1, -7])

2. Vector norm

Many different ways to compute it.

import numpy as np
v = np.array([1,7,3,0,1]); print(v)
np.linalg.norm(v)
np.sqrt(np.dot(v,v))
np.sqrt(v.T @ v)
np.sqrt(np.sum(v*v))
np.sqrt(np.sum(v**2))

3. Unit Vector

import numpy as np
v = np.array([1,7,3,0,1]); print(v)

Make it a unit vector

v = "SOMETHING"

Check it?

"SOMETHING"

4. Inner product

import numpy as np
v1 = np.array([1,2,3])
v2 = np.array([3,2,-1])
inner_product = "SOMETHING"
inner_product = "SOMETHING ELSE"
inner_product = "SOMETHING ELSE AGAIN"

5. Linear projection

import numpy as np

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

# Create the plot
import matplotlib.pyplot as plt
plt.figure()
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1,   color='red')
plt.quiver(0, 0, u[0], u[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.xlim(-1, 5)  # Set plot limits a bit beyond the vectors
plt.ylim(-1, 5)
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')

length_of_projection_of_v_onto_u = "SOMETHING"

print('Length of projection of v onto u = ', length_of_projection_of_v_onto_u)

component_of_v_in_direction_of_u = "SOMETHING"

plt.quiver(0, 0, component_of_v_in_direction_of_u[0], component_of_v_in_direction_of_u[1], angles='xy', scale_units='xy', scale=1,   color='red');

6. Orthogonality

import numpy as np

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

# Create the plot
import matplotlib.pyplot as plt
plt.figure()
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1,   color='red')
plt.quiver(0, 0, u[0], u[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.xlim(-1, 5)  # Set plot limits a bit beyond the vectors
plt.ylim(-1, 5)
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')

component_of_v_in_direction_of_u = "SOMETHING"
w_orthogonal                     = "SOMETHING"

print('w part orthogonal to u    = ',w_orthogonal)

plt.quiver(0, 0, w_orthogonal[0], w_orthogonal[1], angles='xy', scale_units='xy', scale=1, color='blue');

7. Matrices

Matrix-vector multiplication (Example 1)

Define an example matrix and a vector.

import numpy as np
M = np.array([[5,3], [2,6]])
v = np.array([[1],[1]])

print(M)
print(v)

… and multiply them.

M@v

Matrix-vector multiplication (Example 2)

Make a more complicated matrix and vector and multiply them.

W = np.array([[1,7,3,0], [2,-1,2,-1], [1,1,1,1]])
print(W)

v = np.array([[1], [2], [-3], [0]])
print(v)

… and multiply them

# SOMETHING

Implement matrix-vector multiplication as the dot product with each row.

# First  row of W . v
# Second row of W . v
# Third  row of W . v

Implement matrix-vector multiplication as linear combination of columns.

# v[0] * first  column of W
# v[1] * second column of W
# v[2] * third  column of W
# v[3] * fourth column of W