Matrix diagonal

  1. Extract the diagonal of a square matrix
  2. Extract the specified off-diagonal values of a square matrix.

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) \]

Matrix multiplication

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


example:

x = ['a','z','y','z']
remove_all(x,'z')
## answer: returns
['a','y']

primes

  1. Write a function 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).
  2. Write a function 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