Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

0.4 Linear Algebra Tools in NumPy

NumPy provides concise operations for vector and matrix calculations. This section introduces the tools; later chapters develop their mathematical meaning.

Elementwise multiplication versus matrix multiplication

Consider two matrices with the same shape:

The * operator multiplies corresponding entries:

array([[2., 0.], [3., 8.]])

The @ operator performs matrix multiplication:

array([[ 4., 4.], [10., 8.]])

These results differ because matrix multiplication forms row-by-column dot products. For A@BA @ B to be defined, the number of columns of AA must equal the number of rows of BB.

((2, 2), (2, 2), (2, 2))

Matrix-vector products

A matrix-vector product combines the columns of a matrix using the entries of the vector:

array([ 3., 11.])

Check the calculation manually:

[1234][5−1]=[1(5)+2(−1)3(5)+4(−1)]=[311].\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 5 \\ -1 \end{bmatrix} = \begin{bmatrix} 1(5)+2(-1) \\ 3(5)+4(-1) \end{bmatrix} = \begin{bmatrix} 3 \\ 11 \end{bmatrix}.

Dot products and norms

For one-dimensional arrays, @ computes the dot product:

np.float64(-1.0)

The Euclidean norm measures vector length:

np.float64(2.449489742783178)

These operations return in the geometry chapter with definitions and applications.

Solving a linear system

The system

2x+y=7,x−y=2\begin{aligned} 2x + y &= 7, \\ x - y &= 2 \end{aligned}

can be written as Ax=bAx=b. Define the coefficient matrix and right-hand side:

Use np.linalg.solve(A, b) for a square system with a unique solution:

array([3., 1.])

Never stop at the returned array. Check the residual Ax−bAx-b:

(array([0., 0.]), np.float64(0.0))

The residual may contain tiny values such as 1e-16 because computers approximate most real-number arithmetic.

Comparing floating-point results

Avoid exact equality for computed floating-point arrays:

False

Use a tolerance-based comparison:

(np.True_, True)

Inverses

For an invertible matrix, NumPy can compute A−1A^{-1}:

array([[ 0.33333333, 0.33333333], [ 0.33333333, -0.66666667]])

The product is approximately the identity matrix:

array([[1.00000000e+00, 0.00000000e+00], [5.55111512e-17, 1.00000000e+00]])

The formula x=A−1bx=A^{-1}b is mathematically valid:

array([3., 1.])

However, when the goal is to solve Ax=bAx=b, prefer:

x = np.linalg.solve(A, b)

Computing an inverse performs unnecessary work and can introduce more numerical error. Use inv when the inverse itself is the object being studied, not as the default system-solving method.

Singular and nearly singular systems

A singular matrix does not have an inverse. solve reports this rather than inventing a unique solution:

Running np.linalg.solve(singular, np.array([3.0, 6.0])) raises LinAlgError: Singular matrix.

Some invertible matrices are still numerically difficult. The condition number is one warning sign:

(np.float64(1.7675918792439989), np.float64(4.804857307547117e+16))

We will interpret singularity and conditioning after developing elimination, invertibility, and vector spaces.

A reliable computational workflow

When using a linear algebra routine:

  1. state the mathematical problem;

  2. construct arrays with intentional shapes;

  3. inspect .shape;

  4. call the appropriate routine;

  5. verify the result with a residual or mathematical property; and

  6. interpret the result in the problem’s context.

Practice

Exercise 1: matrix-vector product

Compute MvMv for

M=[1−20314],v=[2−13].M=\begin{bmatrix}1&-2&0\\3&1&4\end{bmatrix}, \qquad v=\begin{bmatrix}2\\-1\\3\end{bmatrix}.

Predict the output shape before running the calculation.

Solution
(array([ 4., 17.]), (2,))

The result is [4., 17.] with shape (2,).

Exercise 2: solve and verify

Solve the system

3x−2y=4,5x+y=11.\begin{aligned} 3x-2y&=4,\\ 5x+y&=11. \end{aligned}

Verify the answer with np.allclose.

Solution
(array([2., 1.]), True)

The solution is x=2x=2 and y=1y=1.

Exercise 3: diagnose the operation

Explain why A * x is not a replacement for A @ x when A is a matrix and x is a vector.

Solution

A * x uses broadcasting to multiply each row’s entries elementwise by x; its result has the same matrix shape as A. A @ x forms one dot product per row and returns a vector. The operations answer different questions.