
Jonathan S. answered 02/20/23
Experienced Math Educator and Former Google Software Engineer
import numpy as np
import numpy.linalg as la
# Define the vectors and matrices
vec1 = np.array([-1., 4., -9.])
mat1 = np.array([[1., 3., 5.], [7., -9., 2.], [4., 6., 8.]])
# 1. Multiply vec1 by a constant
vec2 = (np.pi/4) * vec1
print("vec2 = ", vec2)
# 2. Apply cosine function to vec2
vec2 = np.cos(vec2)
print("vec2 = ", vec2)
# 3. Add vectors and multiply by scalars
vec3 = vec1 + 2 * vec2
print("vec3 = ", vec3)
# 4. Compute Euclidean norm of vec3
norm_vec3 = la.norm(vec3)
print("norm_vec3 = ", norm_vec3)
# 5. Matrix-vector multiplication
vec4 = np.dot(mat1, vec3)
print("vec4 = ", vec4)
# 6. Transpose of mat1
mat1_transpose = mat1.T
print("mat1_transpose = ", mat1_transpose)
# 7. Determinant of mat1
mat1_det = la.det(mat1)
print("mat1_det = ", mat1_det)
# 8. Trace of mat1
mat1_trace = np.trace(mat1)
print("mat1_trace = ", mat1_trace)
# 9. Smallest element in vec1
smallest_elem_vec1 = np.min(vec1)
print("smallest_elem_vec1 = ", smallest_elem_vec1)