Exercise 3: linear algebra and BLAS¶

Due date: see canvas.

Upload pdf output of notebook into canvas

1. BLAS API¶

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:

  • the functions may do more than you need; there may be optional inputs you can ignore, and scalar inputs you can set to 1.
  • the lists as seen below for vectors are interpreted as column vectors, but in the matrices the inner lists are rows
In [2]:
import scipy.linalg.blas as blas
In [3]:
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]]
In [4]:
blas.ddot(x,y)
Out[4]:
32.0
In [5]:
blas.dgemv(1,A,x)
Out[5]:
array([4., 1., 1.])
In [6]:
blas.dgemm(1, A,B)
Out[6]:
array([[ 1.,  3.,  0.],
       [ 0.,  2., -2.],
       [ 1., -3.,  0.]])
In [7]:
# verify with numpy

import numpy as np

np.array(A)@np.array(b), np.array(A)@np.array(b).T
Out[7]:
(array([1, 1, 2]), array([1, 1, 2]))

2. Matrix multiplication parts¶

(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.

In [8]:
blas.ddot(A[0],x), blas.ddot(A[1],x), blas.ddot(A[2],x)
Out[8]:
(4.0, 1.0, 1.0)
In [9]:
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]])
Out[9]:
(array([1., 0., 1.]), array([ 3.,  2., -3.]), array([ 0., -2.,  0.]))