Matrices in Python
Info |
---|
Like many other programming languages, the indices in Python arrays starts at 0. This is commonly known, but may catch you by surprise if you're used to the indices in e.g. Matlab. |
In Python, matrices are not it's own thing, but rather a list of list/nested lists. Let's look at an example:
Our matrix of choice will be
To represent this matrix in python, we will consider the matrix as two independent lists,
and
, put together in one list. Written in code, it looks like this:
Code Block | ||
---|---|---|
| ||
mat1 = [[1, 2, 3], [4, 5, 6]] # To make the matrices easier to read while coding, we can place the different lists vertical to eachother instead of horizontally. mat2 = [[1, 2, 3], [4, 5, 6]] # Printing both of these matrices will result in the same output: [[1, 2, 3], [4, 5, 6]] # To alter the output to a more readable format, for-loops are very helpful: [[1, 2, 3], [4, 5, 6]] |
Now, let's see how to obtain different values from our matrix:
Code Block | ||
---|---|---|
| ||
A = [[1, 2, 3], [4, 5, 6]] print("A =", A) # Whole matrix print("A[1] =", A[1]) # 2nd row print("A[1][2] =", A[1][2]) # 3rd element of 2nd row print("A[0][-1] =", A[0][-1]) # Last element of 1st Row column = []; # Empty list for row in A: column.append(row[2]) # Adding the element in the third column of every row. print("3rd column =", column) |
The script above will result in the following output:
Code Block | ||
---|---|---|
| ||
A = [[1, 2, 3], [4, 5, 6]] A[1] = [4, 5, 6] A[1][2] = 6 A[0][-1] = 3 3rd column = [3, 6] |
Matrices in Python using NumPy
First of all, we have to import the NumPy libary. For the sake of this tutorial, we will only do it once, but you can assume it is done everywhere NumPy is being used.
Code Block | ||
---|---|---|
| ||
import numpy as np |
Info |
---|
A lot of great, more detailed information about the different functions in NumPy can be found by searching in their user manual. This article will only describe a limited number of functions and their functionalities. |
To make an array (or matrix) using NumPy, we will use the function numpy.array, and simply use the same syntax as before, but now as a function parameter.
Note | ||||
---|---|---|---|---|
There is another NumPy function for making matrices, numpy.matrix, but this is no longer recommended to use. The class may be removed in the future.
|
Code Block | ||
---|---|---|
| ||
A = np.array([[1, 2, 3], [4, 5, 6]]) # Array of ints B = np.array([[1.1, 2, 3], [4, 5, 6]]) # Array of floats C = np.array([[1, 2, 3], [4, 5, 6]], dtype=complex) # Array of complex numbers |
When printing the matrices above using print, the output will look like this:
Code Block | ||
---|---|---|
| ||
[[1 2 3] # A [4 5 6]] [[1.1 2. 3. ] # B [4. 5. 6. ]] [[1.+0.j 2.+0.j 3.+0.j] # C [4.+0.j 5.+0.j 6.+0.j]] |
Notice how the matrices are printed in an easily readable format without the use of for-loops. This is another benefit of using NumPy arrays.
There are several other functions in NumPy that can create specific matrices. Here are some of them:
numpy.zeros returns a new array with given shape and type, filled with zeros.
Code Block language py title zeros collapse true A = np.zeros((2, 3)) # When printed: [[0. 0. 0.] [0. 0. 0.]]
numpy.ones returns a new array with given shape and type, filled with ones.
Code Block language py title ones collapse true A = np.ones((2, 3)) # When printed: [[1. 1. 1.] [1. 1. 1.]]
numpy.empty returns a new array with given shape and type, without initalizing entries.
Code Block language py title empty collapse true A = np.empty((2, 3)) # When printed: [[ 1.33360294e+241 1.00200641e-310 3.42196482e-210] # Random numbers [ 2.42869050e-313 1.59166660e-308 -1.95565444e-310]]
numpy.full returns a new array with given shape and type, filled with fill_value.
Code Block language py title full collapse true A = np.full((2, 3), 2019) B = np.full((2, 3), np.inf) # When printed: [[2019 2019 2019] # A [2019 2019 2019]] [[inf inf inf] # B [inf inf inf]]
Linear algebra using NumPy
NumPy has several useful functions for built-in linear algebra. For a full list, check out the NumPy documentation on linear algebra.
Solving a linear matrix equation
The equations we would like to solve are
and
. Lets first write it as matrices:
Code Block | ||||
---|---|---|---|---|
| ||||
np.linalg.solve(a,b) #where a and b is matrixs #returns a array with solutions to the system |
Code Block | ||||
---|---|---|---|---|
| ||||
np.linalg.eig(a) # where a is a square matrix #returns two arrays [v,w] #v containing the eigenvalues of a #w containing the eigenvectors of a |
Code Block | ||||
---|---|---|---|---|
| ||||
np.linalg.det(a) # where a is a square matrix #returns the determinant of the matrix a |
Code Block | ||||
---|---|---|---|---|
| ||||
np.linalg.inv(a) # where a is the matrix to be inverted #Returns the inverse matrix of a |
Example
Code Block | ||||
---|---|---|---|---|
| ||||
import numpy as np # Solving following system of linear equation # 5a + 2b = 35 # 1a + 4b = 49 a = np.array([[5, 2],[1,4]]) # Lefthand-side of the equation b = np.array([35, 94]) #Righthand-side print(np.linalg.solve(a,b)) #Printing the solution |
Relaterte artikler
Content by Label | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Page properties | ||
---|---|---|
| ||
|
BibTeX Display Table |
---|