numpy
arrays, linear algebra and vectorization
Numpy arrays | View | Download | |
Broadcasting arrays | View | Download | |
Linear Algebra | View | Download | |
The Mandelbrot set | View | Download | |
Universal Functions | View | Download |
numpy
functions but not others.
When developing your code (but not when submitting it)
you should include the following import statement at the top:
from numpy import max, sum, abs, array, zeros
from numpy.linalg import inv
max(a)
for the maximum of the array
a
. (It would be more usual to have import numpy as np
at the top and to write np.max(a)
, but that will not work in
this context.) The same applies for the functions sum
, abs
,
array
, zeros
and inv
. You should not use
any other numpy
functions in your code for this task, and you should
remove the import statement from the code that you submit.
hilbert_matrix(n)
which returns the Hilbert matrix $H_n$.row_sum_norm(A)
which returns the row sum norm of a matrix $A$.condition_number(A)
which returns the condition number of a matrix $A$.numpy
function.
The relevant function takes two arguments, one of which is the matrix $A$. You may need
to search some more or read the documentation to find out what the other argument needs to be.
import
statement as necessary, check that your
function gives the same answer as the numpy
function for some small matrices
of your choice, and also for Hilbert matrices of sizes $2$ to $10$.
row_sum_norm(A)
and condition_number(A)
into the online test on Moodle. Do not include the import
statement
in the code that you submit. Also, do not include any print
statements or test
cases; just submit the definitions of the two functions. Your functions should be named
exactly as given here, and should take exactly the arguments given here.
numpy
functions that
you like. You should revert to the usual setup: put
import numpy as np
at the top of your code, and then write np.max()
,
np.array()
and so on.
onion(v)
which returns the onion matrix
corresponding to v
, as a numpy
array. You can assume that
numpy
has been imported as np
.
a(n,k)
that calculates $a_k$ for the given value
of $n$. Any correct function will be accepted, but ideally you should try to write a
function that uses matrix methods and is efficiently vectorized. You can assume that
numpy
has been imported as np
. When you are sure that your code
is correct, you should again paste it into the online test on Moodle.
np.max(np.abs(T))
is less than $10^{-9}$.
matrix_exp(A)
which implements this method. You can assume
that numpy
has been imported as np
.
np.exp(A)
is not a solution to the above question.
Explain why np.exp(A)
gives an answer for non-square matrices, but
matrix_exp(A)
just gives an error.