Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 3 6 0 marks The matrix A i n R n n has the form where vec ( a ) , vec ( b

Question 3
60 marks
The matrix AinRnn has the form
where vec(a),vec(b),vec(c)inRn. The entries of vec(a),vec(b), and vec(c) are all assumed to be nonzero.
(a) Write a pseudo-code for computing the product of this matrix with a vector. That is, given
vectors vec(a),vec(b), and vec(c) that describe the matrix A and given a vector xinRn, we want to have
an algorithm to compute the matrix-vector product vec(y)=Avec(x) that avoids multiplying by, and
adding, zeros. Your pseudo-code should have the following form:
Input: vector vec(x)inRn; vectors vec(a),vec(b),vec(c)inRn that define matrix A according to definition (1).
Insert pseudo-code here
Output: vector vec(y)inRn such that vec(y)=Avec(x)
(b) Analyse the complexity of the algorithm from part (b). That is, determine how many flops
are required to compute the product. In terms of "Big-Oh" notation, what is the asymptotic
behaviour of your algorithm as n increases? How is that different from ordinary matrix-vector
multiplication?
(c) Implement your pseudo-code following the starter code in the repository.
(d) Write a script that does the following:
it generates random vectors vec(a),vec(b),vec(c),vec(x) of size n;
it calls your matrix-vector multiplication function and measures and stores the wall time
it takes to complete;
it computes the matrix-vector product using the built-in function o+ and measures and
stores the wall time it takes to complete;
it repeats steps 1-3 for matrix sizes n=2k, starting from k=2.
it plots the wall times for each method as a function of n on the appropriate scale to
verify the order of complexity you found at (b);
for comparison, it plots lines corresponding to n and n2.
Make sure your script uses matrices large enough to clearly see the scaling (order of complex-
ity). Follow the starter code in the repository. Save the plot that your script produces
and include it in your LaTeX/PDF document.
[circ_prod_starter_code.py]
import numpy as np
def circ_prod(a,b,c,x):
...
for i in range(...):
y[i]=
return y
[run_circ_starter_code.py]
import numpy as np
import matplotlib.pyplot as plt
from time import time
from circ_prod import *
wtimes =
for k in range(...):
n =
a = np.random.rand(n,1)
b = np.random.rand(n,1)
c = np.random.rand(n,1)
x = np.random.rand(n,1)
start =
y0= circ_prod(a,b,c,x)
elapsed0=
wtimes[...]=
A =
for i in range(...):
A[i,i]=
for i in range(...):
A[i,i+1]=
A[i+1,i]=
A[0,n-1]=
A[n-1,0]=
start =
y1= A @ x
elapsed1=
wtimes[...]=
plt....(wtimes[:,0],wtimes[:,1],'-*k')
plt....(wtimes[:,0],wtimes[:,2],'-*b')
plt....(...)
plt....(...)
plt.show()
[circ_prod_test.py]
import numpy as np
from memory_profiler import memory_usage
from circ_prod import *
def test_circ_prod():
n =10000
a = np.random.rand(n,1)
b = np.random.rand(n,1)
c = np.random.rand(n,1)
x = np.random.rand(n,1)
mem_usage, y = memory_usage(proc=(circ_prod,(a,b,c,x)),retval=True,max_usage=True)
sparse = mem_usage 70.0
n =100
a = np.random.rand(n,1)
b = np.random.rand(n,1)
c = np.random.rand(n,1)
x = np.random.rand(n,1)
y0= circ_prod(a,b,c,x)
A = np.zeros((n,n))
for i in range(n):
A[i,i]= a[i]
for i in range(n-1):
A[i,i+1]= c[i]
A[i+1,i]= b[i+1]
A[0,n-1]= b[0]
A[n-1,0]= c[n-1]
y1= A @ x
correct = np.linalg.norm(y0-y1,2)1e-10
assert sparse and correct
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions