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. Optionally, and as mentioned at the top of this page, see SciPys documentation on linear algebra.
Dot product/Matrix multiplication
To compute the dot product of two arrays, A and B, we can use the function numpy.dot. However, as recommended by NumPy
Solving a linear matrix equation
BibTeX Referencing |
---|
reference | @misc{scipy, title={numpy.dot¶}, url={https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html#numpy.dot}, journal={numpy.dot - NumPy v1.16 Manual}, author={SciPy}} |
---|
|
, if both arrays are 2-D arrays it is preffered to use numpy.matmul or "A @ B". As an example, we will now compute the dot product of two matrices, A and B, in all three ways. Let
Code Block |
---|
|
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Using "np.dot"
print(np.dot(A, B))
# Using "np.matmul"
print(np.matmul(A, B))
# Using "@"
print(A @ B)
# All three version will produce the same output:
[[19 22]
[43 50]] |
Info |
---|
The @-operator and numpy.matmul uses the the same computing approach, where as numpy.dot is different. For more detailed information about the differences, read the function documentations provided by NumPy. |
Solving a system of linear equations
Let the set of The equations we would like to solve are be
and
.
Lets Let's first write it as matrices:
The function we will use to solve the equation is numpy.linalg.solve. It takes two parameters, a and b, where both are arrays (remember that our matrix is an array type). numpy.linalg.solve will then computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b BibTeX Referencing |
---|
reference | @misc{scipy, title={numpy.linalg.solve¶}, url={https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html#numpy.linalg.solve}, journal={numpy.linalg.solve - NumPy v1.16 Manual}, author={SciPy}} |
---|
|
.
Code Block |
---|
|
a = np.array([[5, 2],[1,4]]) # Left-hand side
b = np.array([33, 21]) # Right-hand side
print(np.linalg.solve(a,b)) #where a and# bPrinting isthe matrixssolution
#returns a array with solutions to the system# Output:
[5. 4.] # In our example this evaluates to x = 5, y = 4, which is the correct answer!
# You may also save the values directly into variables:
x, y = np.linalg.solve(a,b) |
Eigenvalues and eigenvectors
numpy.linalg.ein is a function that computes the eigenvalues and eigenvectors of a square array, returning two arrays consisting of the eigenvalues and eigenvectors respectivly. Lets look at an example. We want to find the eigenvalues and eigenvectors of matrix A, where
.
Code Block |
---|
language | py |
---|
title | Eigenvalues and eigenvectors |
---|
|
A = np.array([[1, 3], [4, -3]])
w, v = np.linalg.eig(aA) # where a is a square matrix
#returns two arrays [v,w]
#v containing the eigenvalues of a
#w containing the eigenvectors of a # "w" are the eigenvalues, "v" the eigenvectors
print(w)
print(v)
# Output:
[ 3. -5.] # w
[[ 0.83205029 -0.4472136 ] # v
[ 0.5547002 0.89442719]] |
The eigenvectors are arranged so that column v[:,i] is the eigenvector corresponding to the eigenvalue w[i], i.e.
is the eigenvector to the eigenvalue 3, and
is the eigenvector to the eigenvalue -5.
Note that the function returns the eigenvectors with a unit length. An easier way to present the eigenvectors above is as
and
, respectively.
Determinant
To compute the determinant of a square array, we will use the function numpy.linalg.det. It takes an array (or several arrays combined) as input, and returns the determinant. As an example, we will compute the determinant of matrix A, where
.
Code Block |
---|
|
A = np.array([[1, 2], [3, 4]])
print(np.linalg.det(A))
# Output:
-2.0000000000000004 |
Code Block |
---|
language | py |
---|
title | Determinant |
---|
|
np.linalg.det(a) # where a is a square matrix
#returns the determinant of the matrix a |
Code Block |
---|
language | py |
---|
title | Example: Finding the inverse |
---|
|
np.linalg.inv(a) # where a is the matrix to be inverted
#Returns the inverse matrix of a |
Example
determinant of multiple arrays at once | collapse | true |
---|
|
A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 2], [2, 1]])
C = np.array([[4, 4], [2, 3]])
# To compute the determinant of multiple arrays at once, we have to put our arrays into a single array as input.
# This will result in an array of length 3 as output, with the determinants respectivly.
print(np.linalg.det([A, B, C]))
# Output:
[-2. -3. 4.] # Evaluates to: det(A) = -2, det(B) = -3 and det(C) = 4. |
Inverting a matrix
To invert a matrix, we can use the function numpy.linalg.inv. It takes a square array as input, and returns the invers of the given array. As an example, we will compute the invers of matrix A, where
Code Block |
---|
|
A = np.array([[1, 2], [3, 4]])
print(np.linalg.inv(A))
# Output:
[[-2. 1. ]
[ 1.5 -0.5]] |
Code Block |
---|
language | py |
---|
title | Example: Check the answer |
---|
collapse | true |
---|
|
# To check if the invers we got in the example above is correct, we simply compute the dot product of A and its invers.
A |
Code Block |
---|
language | py |
---|
title | Solving set of equations |
---|
|
import numpy as np
# Solving following system of linear equation
# 5a + 2b = 35
# 1a + 4b = 49
a = np.array([[51, 2], [13, 4]]) # Lefthand-side of the equation
b
B = np.array([35, 94]) #Righthand-side
print(np.linalg.solve(a,b)) #Printing the solution[-2., 1. ], [1.5, -0.5]]) # This is the presumed invers of A.
print(A @ B) # Matrix product/ dot product
# Output:
[[1. 0.] # This is the identity matrix, which implies that B really is the invers of A.
[0. 1.]] # Remember how the invers works, where "B @ A" would have given us the same answer. |