Skip to main content

Section 4.9 Python recap

Matrices. In the code below we show how to define a matrix in numpy. In the example we use a \(2\times2\) matrix but the same syntax can be used for any \(n\times m\) matrix.

Operations with matrices. Matrices can be added, subtracted, multiplied and inverted. Moreover, matrices can be transposed, namely their rows become columns and viceversa. The code below shows examples of such operations. There are two important things to remark:
  1. The multiplication operator for matrix multiplication is @.
  2. Using the inversion to solve a system is not optimal, better using the LU decomposition or other methods.

Vectors and covectors. Arrays are by default row vectors. Technically, these are called covectors. A row vector v can be made into a column vector by transposition. Note that matrices act (by multiplication) on column vectors only from the right and on row vectors, i.e. covectors, only from the left, as the code below illustrates:
Implicit loops. Sometimes loops can be executed implicitly using the fact that arrays can be used in place of indices for matrices, as the example below shows:
Lines 4 and 6 in the code above correspond to the following loop:
	for k=1:x.size:
	   y[k]=x[k]+k**2