Matrices: Matrices in Python
Basic properties and creation of matrices
Matrices are in Python 2-dimensional arrays. As with vectors, use the ndarray
data structure for matrices. With arrays, you can think of a structure of row vectors of equal length, which are placed under each other.
>>> import numpy as np
>>> A = np.array([[1,2,3],[4,5,6]]) # explicit entering of matrix elements
>>> print(A)
[[1 2 3]
[4 5 6]]
>>> A = np.array([range(1,4),range(4,7)] # iterations used for the rows in the matrix
>>> print(A)
[[1 2 3]
[4 5 6]]
>>>>> type(A)
<class 'numpy.ndarray'>
>>> A.dtype
dtype('int32')
>>> A.shape # dimension
(2, 3)
>>> np.size(A) # number of elements
6
>>> len(A) # 2-dimensional array
2
\(\phantom{x}\)
We illustrate other much used methods to create arrays by examples too.
Construction of a matrix via repetition loops
>>> A = np.array([[i**j for j in range(1,4)] \
for i in range(1,3)])
>>> print(A)
[[ 1 1 1 1]
[ 2 4 8 16]
[ 3 9 27 81]]
Construction of a matrix via an indexing function
>>> def f(i,j): return i**j
>>> A = np.array([f(i,j) for i in range(1,4) \
for j in range(1,5)]).reshape(3,4)
>>> print(A)
[[ 1 1 1 1]
[ 2 4 8 16]
[ 3 9 27 81]]
Construction of a special matrix There exist also special functions to create matrices and vectors with ones and zeros, namely zeros
, zeros_like
, eye
, ones
en diag
:
>>> print(A)
[[ 1 1 1 1]
[ 2 4 8 16]
[ 3 9 27 81]]
>>> Z = np.zeros_like(A); print(Z)
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
>>> Z = np.zeros((2,4)); print(Z) # zero matrix
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
>>> I = np.eye(3); print(I) # identity matrix
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
>>> J = np.ones((3,3)); print(J) # matrix with ones
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
>>> D = np.diag(np.array([1,2,3])); print(D) # diagonal matrix
[[1 0 0]
[0 2 0]
[0 0 3]]
Construction of a matrix by stacking Arrays can also be stacked horizontally and vertically:
>>> np.hstack((A,I)) # horizontal stacking of matrices
array([[ 1., 1., 1., 1., 1., 0., 0.],
[ 2., 4., 8., 16., 0., 1., 0.],
[ 3., 9., 27., 81., 0., 0., 1.]])
>>> np.vstack((Z,A,Z)) # vertical stacking of matrices array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 1., 1., 1., 1.], [ 2., 4., 8., 16.], [ 3., 9., 27., 81.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
Constructie of a random matrix You can also randomly create matrices:
>>> A = np.random.rand(3,2); print(A) # random 3x2 matrix with numbers between 0 and 1
[[0.87206119, 0.2388594],
[0.81963097, 0.4175895],
[0.67910649, 0.99048622]]
>>> random 2x3 matrix with integers in the segment [5, 8]:
>>> A = np.random.randint(5, 9, size=(2, 3); print(A)
[[5 6 5]
[7 8 8]]
Construction of a regular grid We discuss two special Python functions to create vectors and arrays, namely linspace
and meshgrid
. These functions are used a lot in plotting graphs and surfaces. If you want to create an array of \(N\) equidistant values from the interval \((a,b)\), do it with the function call linspace(a,b,N)
:
>>> x = np.linspace(0,2,5); print(x)
[ 0. 0.5 1. 1.5 2. ]
>>> y = np.linspace(0,1,3); print(y)
[ 0. 0.5 1. ]
These arrays can be used to define a two-dimensional grid. With the option sparse=True
, memory load will be limited and only the most essential information of the grid are stored; with the option sparse=False
, the \(x\) and \(y\) coordinates of all grid points are stored. It should be clear which method is preferred in which circumstances.
>>> X, Y = np.meshgrid(x,y, sparse=True)
>>> print(X)
[[ 0. 0.5 1. 1.5 2. ]]
>>> print(Y)
[[ 0. ]
[ 0.5]
[ 1. ]]
>>> X, Y = np.meshgrid(x,y, sparse=False)
>>> print(X)
[[ 0. 0.5 1. 1.5 2. ]
[ 0. 0.5 1. 1.5 2. ]
[ 0. 0.5 1. 1.5 2. ]]
>>> print(Y)
[[ 0. 0. 0. 0. 0. ]
[ 0.5 0.5 0.5 0.5 0.5]
[ 1. 1. 1. 1. 1. ]]