Exercises
Ex. 1 → Two matrices A, B are called similar, if there exists a matrix S, such that B = S-1 A S. A and B have the same eigenvalues. Write a test checking that two matrices are similar, by comparing their eigenvalues. Is it a functional or a unit test?
Ex. 2 → Create two vectors of large dimension. Compare the execution time of various ways to compute their dot
product:
- SciPy function:
dot(v,w)
- Generator and sum:
sum((x*y for x,y in zip(v,w)))
- Comprehensive list and sum:
sum([x*y for x,y in zip(v,w)])
Ex. 3 → Let u be a vector. The vector v with components
is called a moving average of u. Determine which of the two alternatives to compute v is faster:
v = (u[:-2] + u[1:-1] + u[2:]) / 3
or
v = array([(u[i] + u[i + 1] + u[i + 2]) / 3 for i in range(len(u)-3)])