5.4.6 · D4Scientific Computing (Python)

Exercises — NumPy linear algebra — np.linalg.solve, eig, svd, norm, det

1,958 words9 min readBack to topic

This page is a self-testing ladder. Each problem states what to compute; the answer hides inside a collapsible [!recall]- callout so you can try first, then reveal. Difficulty climbs L1 Recognition → L2 Application → L3 Analysis → L4 Synthesis → L5 Mastery.

Everything here builds on the parent topic. If a word feels unfamiliar, the parent note defines it — but every symbol below is re-earned as we go.


Level 1 — Recognition

(Can you read the notation and pick the right tool?)

Exercise 1.1

You are given a square matrix A and a vector b. You want the x that satisfies . Which one NumPy call do you use, and which call must you avoid?

Recall Solution 1.1

Use x = np.linalg.solve(A, b). Avoid np.linalg.inv(A) @ b.

  • is the machine, is where an arrow landed, is where it started. solve runs the machine backward directly.
  • Inverting first computes a whole matrix you don't need, costs more, and amplifies rounding error.

Exercise 1.2

For the vector , state the numeric value of np.linalg.norm(v) (the default). What does that number mean geometrically?

Recall Solution 1.2

Default norm is the Euclidean () length: . Geometrically it is the straight-line distance from the origin to the tip of the arrow — the hypotenuse of a right triangle with legs and .

Exercise 1.3

np.linalg.eig(A) returns w, V. If you want the second eigenvector, do you write V[1, :] or V[:, 1]?

Recall Solution 1.3

V[:, 1] — eigenvectors are the columns of V. V[1, :] would grab a row, which is not an eigenvector.


Level 2 — Application

(Turn a small hand computation into the right call.)

Exercise 2.1

Solve by hand, then confirm the NumPy result:

Recall Solution 2.1

WHAT: subtract row 2 from row 1 to kill a variable. WHY: we want a triangular (one-unknown-at-a-time) system — this is Gaussian elimination. Back-substitute into row 2: So .

A = np.array([[3.,2.],[1.,2.]]); b = np.array([7.,5.])
np.linalg.solve(A, b)   # array([1., 2.])

Exercise 2.2

Compute by hand. What does its sign and size tell you?

Recall Solution 2.2

WHAT/WHY: for a matrix, is the signed area of the parallelogram spanned by the two columns and — it measures how much the machine scales area. (See figure below.) Positive and non-zero → the machine scales area by , keeps orientation (no flip), and is invertible (a unique solve exists).

Figure — NumPy linear algebra — np.linalg.solve, eig, svd, norm, det

Exercise 2.3

For , compute all three norms: , , .

Recall Solution 2.3
  • (total taxi-cab distance).
  • (the largest single component).
v=np.array([1.,-2.,2.])
np.linalg.norm(v), np.linalg.norm(v,1), np.linalg.norm(v,np.inf)  # 3.0, 5.0, 2.0

Level 3 — Analysis

(Reason about why a result comes out the way it does.)

Exercise 3.1

Find the eigenvalues of by hand via the characteristic equation, then name the eigenvectors and explain their meaning.

Recall Solution 3.1

WHAT: an eigenvector satisfies — the machine only scales it, never turns it. Rearranged: . WHY set the determinant to zero: for a nonzero to exist, must squash space (be singular), i.e. . So

  • For : .
  • For : .

Meaning: along the diagonal the machine stretches by ; along the anti-diagonal it leaves length unchanged (). These two directions never rotate.

Figure — NumPy linear algebra — np.linalg.solve, eig, svd, norm, det

Exercise 3.2

A matrix has np.linalg.det(A) equal to 2.3e-17. A classmate concludes "the determinant is zero, so is singular." Steel-man and then correct this.

Recall Solution 3.2

The reasoning: in exact math, singular, so a value this tiny looks like "zero with rounding." The correction: floating point almost never returns exactly , and the size of det is not scale-free — a genuinely invertible matrix scaled by everywhere has a determinant near per row and looks "small" without being singular. The reliable test is the condition number np.linalg.cond(A): large (say ) means untrustworthy solves. See Condition number and numerical stability.

Exercise 3.3

For , its eigenvalues are and . Its singular values are and . Explain why one is negative but both singular values are positive.

Recall Solution 3.3

Eigenvalues can be negative: means the -axis direction is scaled by , i.e. stretched by and flipped. Singular values only report the stretch amount, never the flip, so they are and . Formally, are eigenvalues of : here , whose eigenvalues give . Squaring erased the sign. See Eigenvalues and diagonalization.


Level 4 — Synthesis

(Combine two or more tools.)

Exercise 4.1

You must fit a line through three points — an over-determined system (3 equations, 2 unknowns). Set it up as and explain which NumPy tool solves it, and why solve cannot.

Recall Solution 4.1

Each point gives . Stack them: np.linalg.solve needs a square — here is , so it errors. We want the least-squares best fit: minimize . Use np.linalg.lstsq(A, y, rcond=None) (which uses SVD under the hood). See Least squares regression. Normal equations by hand: . , . Solve : , so Best line: .

Exercise 4.2

Verify the identity for (eigenvalues from Ex. 3.1), and also that the trace (sum of diagonal) equals .

Recall Solution 4.2

Eigenvalues were .

  • Product: . Determinant: . ✓
  • Sum: . Trace: . ✓ WHY: the determinant is the total volume scaling, which is the product of the per-direction stretches (the eigenvalues); the trace equals their sum. These are cheap sanity checks on any eigen-computation.

Level 5 — Mastery

(Deep, multi-step, connects to real applications.)

Exercise 5.1

Compute the SVD of by hand: find the singular values, and give the rank. Then explain how truncating the smaller singular value would compress this matrix.

Recall Solution 5.1

WHAT: SVD writes ; singular values are the diagonal of , obtained from . Eigenvalues (sorted descending). Rank = number of nonzero singular values . Compression: keeping only the top singular value gives the best rank-1 approximation ; the discarded energy is . This is exactly the idea behind low-rank image compression and Principal Component Analysis (PCA).

Exercise 5.2

The matrix is almost singular. (a) Compute . (b) Argue why solving here is dangerous, and which single NumPy call quantifies the danger.

Recall Solution 5.2

(a) . (b) The columns and are nearly parallel — the parallelogram they span is a razor-thin sliver (area ). A tiny change in swings wildly, because the machine barely spreads inputs across directions. np.linalg.det alone is misleading (it's small but nonzero); the honest measure is np.linalg.cond(A), the condition number, which is large here. See Condition number and numerical stability and Vector norms and metric spaces.

Figure — NumPy linear algebra — np.linalg.solve, eig, svd, norm, det

Exercise 5.3

A rotation matrix turns arrows without stretching. Predict its determinant, its singular values, and the nature of its eigenvalues (real or complex?), then explain each geometrically. Use as a concrete check.

Recall Solution 5.3
  • Determinant: — area is preserved (a rotation squishes nothing).
  • Singular values: both ; a pure rotation stretches by in every direction ().
  • Eigenvalues: generally complex (). WHY: a real eigenvector would be a direction the machine doesn't turn — but a genuine rotation turns every real direction, so no real eigenvector can exist. The math signals this by producing complex eigenvalues.
  • Check : , , singular values , eigenvalues . This is why we reach for svd (real, well-behaved) over eig on such matrices.

Recall One-line self-quiz

Which tool for an over-determined fit? ::: np.linalg.lstsq (least squares via SVD), never solve. Singular values of a rotation matrix? ::: all equal to . How to detect near-singularity reliably? ::: np.linalg.cond(A) is large — not a small det.