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
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 equations we would like to solve be
and
. 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)) # Printing the solution
# 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 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 |
---|
|
A = np.array([[1, 3], [4, -3]])
w, v = np.linalg.eig(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] are the eigenvectors corresponding to the eigenvalue w[i]
# e.i. 0.83 and 0.55 are the eigenvectors to the eigenvalue 3, and -0.45 and 0.89 are the eigenvectors to the eigenvalue -5. |
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 | Example: Finding the 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 = np.array([[1, 2], [3, 4]])
B = np.array([[-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. |