Linear algebra is one of the essential building blocks of computational mathematics. The objects of linear algebra are vectors and matrices. The package NumPy includes all the necessary tools to manipulate those objects.
The first task is to build matrices and vectors, or to alter existing ones by slicing. The other main task is the dot
operation, which embodies most of the linear algebra operations (scalar product, matrix-vector product, and matrix-matrix product). Finally, various methods are available to solve linear problems.
For the impatient, here is how to use arrays in a nutshell. Be aware though that the behavior of arrays may be surprising at first, so we encourage you to read on after this introductory section.
Creating vectors is as simple as using the function array
to convert a list to an array:
v = array([1.,2.,3.])
The object v
is now a vector that behaves much like a vector in linear algebra. We have already emphasized the differences with the list object in Python (refer to section Arrays in Chapter 3, Containers Type). Here are some illustrations of the basic linear algebra operations on vectors:
# two vectors with three components v1 = array([1., 2., 3.]) v2 = array([2, 0, 1.]) # scalar multiplications/divisions 2*v1 # array([2., 4., 6.]) v1/2 # array([0.5, 1., 1.5]) # linear combinations 3*v1 # array([ 3., 6., 9.]) 3*v1 + 2*v2 # array([ 7., 6., 11.]) # norm from scipy.linalg import norm norm(v1) # 3.7416573867739413 # scalar product dot(v1, v2) # 5. v1 @ v2 # 5 ; alternative formulation
Note that all basic arithmetic operations are performed elementwise:
# elementwise operations: v1 * v2 # array([2., 0., 3.]) v2 / v1 # array([2.,0.,.333333]) v1 - v2 # array([-1., 2., 2.]) v1 + v2 # array([ 3., 2., 4.])
Some functions act elementwise on arrays as well:
cos(v1) # cosine, elementwise: array([ 0.5403, -0.4161, -0.9899])
This subject will be covered in the section Functions Acting on Arrays.
A matrix is created in a similar way to a vector, but from a list of lists instead:
M = array([[1.,2],[0.,1]])
To create a row matrix containing the same data as the vector v = array([1., 2., 1.])
, we do this:
R = array([[1.,2.,1.]]) # notice the double brackets: # this is a matrix shape(R) # (1,3): this is a row matrix
The corresponding column matrix is obtained by the method reshape
:
C = array([1., 2., 1.]).reshape(3, 1) shape(C) # (3,1): this is a column matrix
Indexing and slicing are similar to that of a list. The main difference is that there may be several indexes or slices when the array is a matrix. The subject will be covered in depth in section Array indexing; here, we just give some illustrating examples of indexing and slicing:
v = array([1., 2., 3]) M = array([[1., 2],[3., 4]]) v[0] # works as for lists v[1:] # array([2., 3.]) M[0, 0] # 1. M[1:] # returns the matrix array([[3., 4]]) M[1] # returns the vector array([3., 4.]) # access v[0] # 1. v[0] = 10 # slices v[:2] # array([10., 2.]) v[:2] = [0, 1] # now v == array([0., 1., 3.]) v[:2] = [1, 2, 3] # error!
The essential operator that performs most of the usual operations of linear algebra is the Python function dot
. It is used for matrix-vector multiplications:
dot(M, v) # matrix vector multiplication; returns a vector M @ v # alternative formulation
It may be used to compute a scalar product between two vectors:
dot(v, w) # scalar product; the result is a scalar v @ w # alternative formulation
Lastly, it is used to compute matrix-matrix products:
dot(M, N) # results in a matrix M @ N # alternative formulation
If A is a matrix and b is a vector, you can solve the linear equation:
Using the solve
method, which has this syntax:
from scipy.linalg import solve x = solve(A, b)
For example, we want to solve:
Here is the solution for the preceding equation:
from scipy.linalg import solve A = array([[1., 2.], [3., 4.]]) b = array([1., 4.]) x = solve(A, b) allclose(dot(A, x), b) # True allclose(A @ x, b) # alternative formulation
The command allclose
is used here to compare two vectors. If they are close enough to each other, this command returns True
. Optionally a tolerance value can be set. For more methods related to linear equations systems, refer to section Linear algebra methods in SciPy.
3.137.212.71