Answered step by step
Verified Expert Solution
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
marks
The matrix has the form
where vecvecvec The entries of vecvec and vec are all assumed to be nonzero.
a Write a pseudocode for computing the product of this matrix with a vector. That is given
vectors vecvec and vec that describe the matrix A and given a vector we want to have
an algorithm to compute the matrixvector product vecAvec that avoids multiplying by and
adding, zeros. Your pseudocode should have the following form:
Input: vector vec; vectors vecvecvec that define matrix A according to definition
Insert pseudocode here
Output: vector vec such that vecAvec
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 "BigOh notation, what is the asymptotic
behaviour of your algorithm as increases? How is that different from ordinary matrixvector
multiplication?
c Implement your pseudocode following the starter code in the repository.
d Write a script that does the following:
it generates random vectors vecvecvecvec of size ;
it calls your matrixvector multiplication function and measures and stores the wall time
it takes to complete;
it computes the matrixvector product using the builtin function and measures and
stores the wall time it takes to complete;
it repeats steps for matrix sizes starting from
it plots the wall times for each method as a function of on the appropriate scale to
verify the order of complexity you found at b;
for comparison, it plots lines corresponding to and
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 LaTeXPDF document.
circprodstartercode.py
import numpy as np
def circprodabcx:
for i in range:
yi
return y
runcircstartercode.py
import numpy as np
import matplotlib.pyplot as plt
from time import time
from circprod import
wtimes
for k in range:
n
a nprandom.randn
b nprandom.randn
c nprandom.randn
x nprandom.randn
start
y circprodabcx
elapsed
wtimes
A
for i in range:
Aii
for i in range:
Aii
Aii
An
An
start
y A @ x
elapsed
wtimes
pltwtimes:wtimes:k
pltwtimes:wtimes:b
plt
plt
pltshow
circprodtest.py
import numpy as np
from memoryprofiler import memoryusage
from circprod import
def testcircprod:
n
a nprandom.randn
b nprandom.randn
c nprandom.randn
x nprandom.randn
memusage, y memoryusageproccircprod,abcxretvalTrue,maxusageTrue
sparse memusage
n
a nprandom.randn
b nprandom.randn
c nprandom.randn
x nprandom.randn
y circprodabcx
A npzerosnn
for i in rangen:
Aii ai
for i in rangen:
Aii ci
Aii bi
An b
An cn
y A @ x
correct nplinalg.normyye
assert sparse and correct
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started