e.g. \[ x = \left( \begin{array}{cccc} x_{11} & x_{12} & x_{13} & x_{14} \\ x_{21} & x_{22} & x_{23} & x_{24} \\ x_{31} & x_{32} & x_{33} & x_{34} \\ x_{41} & x_{42} & x_{43} & x_{44} \end{array} \right) \]
mdiag(x) = [x11, x22, x33, x44]
mdiag(x,-1) = [x12,x23,x34]
mdiag(x,1) = [x21, x32, x43]
The formal definition of matrix multiplication for two matrices \(x\) and \(y\) is
\[ (xy)_{ij} = \sum_{k=1}^m x_{ik} y_{kj} \] where \(m\) is the number of columns of \(x\) and the number of rows of \(y\) (these dimensions must be the same, or the matrices are non-conformable and can’t be multiplied).
As an example, if
\[ x = \left( \begin{array}{cc} a & b \\ c & d \end{array} \right), \qquad y = \left( \begin{array}{cc} e & f \\ g & h \end{array} \right) \]
then their product \(z\) is
\[ \left( \begin{array}{cc} ae + bg & af + bh \\ ec + dg & cf + dh \end{array} \right) \]
remove_all(x,v)
function that removes all occurrences of a value v
within a list x
and returns the result. It should still work (i.e. it should return x
unchanged) if there are no occurrences in the list.example:
x = ['a','z','y','z']
remove_all(x,'z')
## answer: returns
['a','y']
prime1
that tests whether a specified natural number n
is prime and returns a boolean value. In order to do this, you need to test whether it is divisible by any number between 2 and \(\sqrt{n}\) (hint: your range should end at round(math.sqrt(n))+1
; don’t forget to import math
).allprimes
that uses a for
or while
loop and calls prime1
to generate and return a list of all of the primes between 2 and a specified natural number n
(inclusive)More efficient: Sieve of Eratosthenes