Find functions in scipy.linalg.blas for performing the following 3 functions and demonstrate their use with the values given below. Do not use any other packages.
(a) dot product $z = \mathbf x \cdot \mathbf y$
(b) matrix-vector product $\mathbf b = \mathbf A \mathbf x$
(c) matrix-matrix product $\mathbf C = \mathbf A \mathbf B$
Hints:
import scipy.linalg.blas as blas
x = [1,2,3]
y = [4,5,6]
A = [[1,0,1],[0,-1,1],[-1,1,0]]
b = [-1,1,2]
B = [[0,2,1],[1,-1,1],[1,1,-1]]
blas.ddot(x,y)
32.0
blas.dgemv(1,A,x)
array([4., 1., 1.])
blas.dgemm(1, A,B)
array([[ 1., 3., 0.], [ 0., 2., -2.], [ 1., -3., 0.]])
# verify with numpy
import numpy as np
np.array(A)@np.array(b), np.array(A)@np.array(b).T
(array([1, 1, 2]), array([1, 1, 2]))
(a) Demonstrate that you can get the same result as above for $\mathbf A \mathbf x$, by using only the dot product function
(b) Demonstrate that you can get the same result as above for $\mathbf A \mathbf B$, by using the matrix-vector multiplication function one column at a time
Hint: you will probably need to manually extract the columns rather than using a simple slice to access.
blas.ddot(A[0],x), blas.ddot(A[1],x), blas.ddot(A[2],x)
(4.0, 1.0, 1.0)
blas.dgemv(1, A, [B[0][0],B[1][0],B[2][0]]), blas.dgemv(1, A, [B[0][1],B[1][1],B[2][1]]), blas.dgemv(1, A, [B[0][2],B[1][2],B[2][2]])
(array([1., 0., 1.]), array([ 3., 2., -3.]), array([ 0., -2., 0.]))