This section summarizes the NumPy array conventions, linear-algebra operations, and indexing idioms used throughout the computational examples in these notes.
Indexing: 0-indexed. A[i, j] for entries, A[i, :] for rows, and A[:, j] for columns.
TipRemark
(Shapes and one-dimensional arrays)A.shape returns (m, n). A 1D array has shape (n,), which is not equivalent to a column vector (n, 1). These one-dimensional arrays can behave differently from column arrays under broadcasting. Use x[:, None] or x.reshape(-1, 1) to convert to 2D.
WarningExercise
Create \(4 \times 4\) identity matrix. Verify shape and dtype.
Extract row 2 and column 3 from a random \(3 \times 3\)\(\bA\).
Compare A @ x for x.shape == (3,) and x.shape == (3,1). Record the output shape in each case, then explain why the two representations behave differently under broadcasting.
36.2 Linear Algebra Operations
The core routines in numpy.linalg provide access to standard BLAS and LAPACK implementations:
TipRemark
(Vectorization and performance) Explicit for loops in Python are slow. Push heavy lifting into NumPy/BLAS routines by vectorizing operations. For example, use A @ B instead of triple-nested loops.
TipRemark
(Linear solve versus inversion)np.linalg.solve(A, b) is faster and more stable than np.linalg.inv(A) @ b. Explicit inversion is almost never desirable in scientific computation.
TipRemark
(Computed equality) For computed floating-point quantities, compare the error with a tolerance appropriate to their scale. Functions such as np.allclose and np.isclose perform tolerance tests. Their default tolerances do not establish mathematical equality or certify accuracy for every problem. Exact equality is appropriate for integers, booleans, and deliberately exact structural checks.
TipRemark
(Sparse matrices) Large discretized PDE and graph problems should usually use scipy.sparse, not dense NumPy arrays. Use scipy.sparse.linalg for sparse solves, eigenvalue computations, CG, and GMRES.
WarningExercise
Solve a \(2 \times 2\) system and verify the residual \(\|\bA\bx - \bb\|\).
Profile solve vs inv @ b for \(n=1000\).
Compare np.linalg.eig and np.linalg.eigh (optimized for symmetric matrices).
WarningExercise
For a \(3\times3\) matrix, form an approximation x with shape (3,1) and a right-hand side b with shape (3,). Predict the shape of b - A @ x before evaluating it. Explain why that array is not the residual vector you intended. Make the shapes consistent and recompute the residual.
36.3 Key Idioms
Element-wise:* and ** are entry-wise. Matrix powers use np.linalg.matrix\_power.
Broadcasting: Automatic dimension expansion. For A.shape == (m,n) and v.shape == (n,), A * v scales columns by the entries of v. To scale rows by a vector of length m, use A * v[:,None].
Aliases, Views, Copies:B = A makes a second name for the same array. Slices are usually views. Use B = A.copy() for independent data.
Sparse construction: The scipy.sparse module provides diags for banded matrices and csr\_matrix for compressed sparse row storage.
WarningExercise
Let \(\bA = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}\). Compare A * A and A @ A.
Demonstrate the ``view pitfall’’ by modifying a slice of a matrix.
Use broadcasting to perform diagonal scaling \(\bA\bD\) without constructing \(\bD = \text{diag}(d)\).
NoteFurther reading
Heath gives a broad introduction to scientific computing at roughly the level of these notes (Heath 2018). For the effects of floating-point arithmetic, Higham is the standard detailed reference (Higham 2002). Meyer provides an accessible bridge from undergraduate linear algebra to matrix computation (Meyer 2000).
Heath, Michael T. 2018. Scientific Computing: An Introductory Survey. 2nd ed. Society for Industrial and Applied Mathematics. https://doi.org/10.1137/1.9781611975581.
Higham, Nicholas J. 2002. Accuracy and Stability of Numerical Algorithms. 2nd ed. Society for Industrial and Applied Mathematics. https://doi.org/10.1137/1.9780898718027.