Singular value decomposition (SVD), explained with pictures
A matrix is a plan for changing the plane. It can stretch, shear and rotate it. However complicated a matrix looks, what it does can be split into "turn, stretch, turn again." Split the change from a circle to an ellipse into three steps and watch.
Short answer. Whatever the matrix, what it does can be split into three steps: turn, stretch along the axes, turn again. This is the singular value decomposition A = U Σ Vᵀ. The stretch amounts in the middle, in Σ, are the singular values. Put a circle in and an ellipse comes out. The long radius and the short radius of that ellipse are the singular values.
Left column = where î goes, right column = where ĵ goes
Drag the circles at the arrow tips (î, ĵ) or change the numbers. The dashed circle has radius 1. The colored dots show where the points on that circle go after A. Dots of the same color belong together. The dashed arrows v1 and v2 go to the solid arrows σ1·u1 and σ2·u2, which are the long radius and the short radius of the ellipse.
Definitions
The text below uses 2×2 matrices, but the definitions are the same for m×n matrices. A is a real m×n matrix, and p = min(m, n).
- singular value
- The n×n matrix AᵀA is symmetric, and all its eigenvalues are 0 or more. Write them from largest to smallest as λ₁ ≥ λ₂ ≥ … ≥ λₙ ≥ 0. The numbers σᵢ below are called the singular values of A. The number of non-zero singular values equals the rank of A.
- singular value decomposition (SVD)
- Writing A as a product of three matrices as below. U is an m×m orthogonal matrix, V is an n×n orthogonal matrix, and Σ is an m×n matrix with the singular values σᵢ in its (i, i) cells (i = 1, …, p), largest first, and 0 in every other cell. Every real matrix has a singular value decomposition.
- left singular vector, right singular vector
- In a singular value decomposition A = UΣVᵀ, the columns v₁, …, vₙ of V are called the right singular vectors of A, and the columns u₁, …, uₘ of U are called the left singular vectors of A. Writing AV = UΣ one column at a time gives the equation below.
- low-rank approximation
- If A has rank r, A can be written as a sum of r rank-1 matrices, as in the first formula below. Keeping only the k terms with the largest singular values (k < r) gives Aₖ, called the rank-k approximation of A. If you measure distance by squaring the difference in every cell, adding them all up, and taking the square root, Aₖ is the closest matrix to A among all matrices of rank k or less.
1. A circle becomes an ellipse
A matrix A moves every point on the plane (as on the determinant page). Move the points of a circle with radius 1 with A, and you always get an ellipse. If the matrix squashes the plane, that ellipse is pressed into a line segment or even a single point.
An ellipse has a longest direction and a shortest direction, and they are at a right angle. Trace back which points on the circle land there, and you find two directions v1 and v2. These two are also at a right angle. In short:
- On the input side there are two directions v1 and v2 at a right angle.
- A stretches v1 by σ1 and sends it in the direction u1. It stretches v2 by σ2 and sends it in the direction u2.
- On the output side, u1 and u2 are also at a right angle.
Keeping a right angle is not common. î and ĵ are at a right angle too, but after A they usually are not (look at the green and blue arrows in the lab). If σ1 and σ2 are different, there is only one pair that keeps its right angle (an arrow and its reverse count as the same direction). The amounts σ1 and σ2 by which these two directions are stretched are the singular values. The larger one is called σ1.
2. The formula: A = U Σ Vᵀ
Write section 1 as a product of three matrices, and you have the singular value decomposition.
| Piece | What it does | In a 2×2 matrix |
|---|---|---|
| Vᵀ | Turns the input. Sends v1 and v2 onto the across axis and the up axis | Its rows are v1 and v2 |
| Σ (sigma) | Stretches along the axes. The singular values sit on its diagonal | [σ1 0 / 0 σ2] |
| U | Turns again. Sends the across axis and the up axis to u1 and u2 | Its columns are u1 and u2 |
When you multiply a point x by A, things happen from right to left: A x = U (Σ (Vᵀ x)). First Vᵀ, then Σ, and last U. That is why it is "turn, stretch, turn again."
One direction at a time, it says A v1 = σ1 u1 and A v2 = σ2 u2. You can also write A as a sum of pieces:
Each piece (u1 v1ᵀ) is the simplest kind of matrix: it presses the whole plane onto one line. σ is the weight of that piece. The "drop the small one" idea in section 7 comes from here.
The ᵀ in Vᵀ means transpose: swap the rows and the columns. The columns of V are v1 and v2, so the rows of Vᵀ are v1 and v2. U and V only turn (and sometimes flip), so they never change lengths or right angles.
3. Watch the three steps
Slowly move the "Step" slider from 0 to 3, or press "Play the three steps."
- Turn with Vᵀ (0 to 1). A turned circle is still a circle, so the shape stays the same and only the colored dots move around. At the end of this step, v1 lies on the across axis and v2 lies on the up axis.
- Stretch with Σ (1 to 2). Across is multiplied by σ1, up and down by σ2. The circle becomes an ellipse lined up with the axes.
- Turn with U (2 to 3). The ellipse is turned into its final place. The across axis goes to u1 and the up axis goes to u2.
When a flip is mixed in. If the determinant is negative, A flips the plane over. Turning alone can never flip, so one of U and V carries a flip. In the numbers, the lab puts the flip in Vᵀ. In the picture, it shows the flip during step ②: up and down is multiplied by −σ2. The circle becomes a flat line for a moment and comes out as its mirror image.
Try this. ① The lab starts with "Shear," [1 1 / 0 1]. σ1 = 1.62 and σ2 = 0.62, and their product is 1, the same as the determinant. Press "Play the three steps." Step ① turns 58° clockwise, step ② stretches 1.62 times across and 0.62 times up and down, and step ③ turns 32° counterclockwise. ② "2× across, 0.5× up" is already a stretch along the axes, so steps ① and ③ have nothing to turn (0°). ③ In "Flip," [0 2 / 1 0], slowly move the slider from 1 to 2. The circle grows across and gets pressed up and down into a line, then comes out again as its mirror image. ④ Drag the tip of î onto the same line as ĵ. σ2 becomes 0 and the rank drops to 1.
4. How the formula is built: find the direction that stretches the most
In section 1, v1 was the direction A stretches the most, and σ1 was how much it stretches there. If you find that direction with a formula instead of by turning and measuring, you can see why the singular values are the square roots of the eigenvalues of AᵀA, and where A v = σ u and A = U Σ Vᵀ come from. The matrix is the shear [1 1 / 0 1], the one the lab above starts with.
Press "Next" to go one step at a time. A piece is added to the formula on top (the orange background marks the new piece), the picture changes by that much, and the reason appears below. In the left picture, the dashed circle has radius 1, the gray dashed arrow is v on the circle, and the black solid arrow is A v. In the right graph, across is the angle θ of v and up is the length of A v (its square in steps 2 and 3). The faint lines are the graph before and after squaring. Orange is the peak side (v1) and purple is the valley side (v2).
Written only as equations, the steps above look like this.
- Measure while turning:
|A v|. Write the arrow of length 1 on the circle at angle θ asv = (cos θ, sin θ). At θ = 0°, v = (1, 0) and A v = (1, 0), with length 1. At θ = 90°, v = (0, 1) and A v = (1, 1), with length 1.41. Turn v from 0° to 180°, and the length goes up and down between 0.62 and 1.62 while the tip of A v draws an ellipse. Past 180°, v becomes −v and the same lengths repeat, so half a turn is enough. The peak, 1.62, is σ1, and the valley, 0.62, is σ2. A big matrix has too many directions to try one by one, so we need a formula. - Square it:
|A v|² = vᵀ (AᵀA) v. The squared length of an arrow w is its dot product with itself,w·w = wᵀ w(see dot product). Put A v in for w. A transpose flips the order of a product,(A v)ᵀ = vᵀ Aᵀ, so|A v|² = vᵀ Aᵀ A v. For the shear,AᵀA = [1 1 / 1 2]. At θ = 0°, vᵀ(AᵀA)v is the top-left entry, 1. At 90° it is the bottom-right entry, 2 (= 1.41²). Lengths are never negative, so squaring does not move the peak or the valley. From here on you only need one matrix, AᵀA, instead of A. - The peak is an eigenvector:
AᵀA v = λ v. At the peak (58°) the graph is flat: turn v a little and the value does not change. When v turns a little, it moves at a right angle to itself, and how fast the value changes is set by the part of AᵀA v in that right-angle direction (calculus shows this; we do not prove it here). For the graph to be flat, that part must be 0, so AᵀA v must point the same way as v. In the picture, that is where the orange arrow AᵀA v lies on top of v. An arrow that keeps its direction is an eigenvector, and the value there isvᵀ (λ v) = λ(v has length 1), which is the eigenvalue. At the peak, λ1 = 2.62. The valley (148°) is flat too, so for the same reason it is an eigenvector, with λ2 = 0.38. - Take the square root:
σ = √λ. Step 2 squared the length, so undo it.σ1 = √2.62 = 1.62andσ2 = √0.38 = 0.62, the same values measured in step 1. This is "singular values = square roots of the eigenvalues of AᵀA." The peak and the valley are always 90° apart. Write vᵀ(AᵀA)v in terms of θ and tidy it up with cos 2θ and sin 2θ, and you get a single wave that goes up and down once every 180°. So v1 and v2 are at a right angle. For big matrices, the same job is done by the fact that the eigenvectors of a symmetric matrix are at right angles to each other (we do not prove it here). - Make u:
A v = σ u. A v1 has length σ1, so dividing it by σ1 leaves a direction of length 1. Call it u1.u1 = A v1 ÷ σ1 = (1.38, 0.85) ÷ 1.62 = (0.85, 0.53)andu2 = A v2 ÷ σ2 = (-0.32, 0.53) ÷ 0.62 = (-0.53, 0.85)(dividing the rounded numbers gives slightly different last digits). Written the other way,A v = σ u. u1 and u2 are at a right angle too, because(A v1)·(A v2) = v1ᵀ (AᵀA) v2 = λ2 (v1·v2) = 0. If σ is 0 you cannot divide, so that u is chosen as an arrow of length 1 at a right angle to the other u. - Put it together:
A = U Σ Vᵀ. Stand the two equationsA v1 = σ1 u1andA v2 = σ2 u2side by side as columns:A [v1 v2] = [u1 u2] [σ1 0 / 0 σ2], which isA V = U Σ. The columns v1 and v2 of V are arrows of length 1 at a right angle, soV Vᵀ = I. Multiply both sides by Vᵀ on the right, and you getA = U Σ Vᵀ. In numbers,U = [0.85 -0.53 / 0.53 0.85],Σ = [1.62 0 / 0 0.62]andVᵀ = [0.53 0.85 / -0.85 0.53], the same as the readout in the lab above. Multiply the three and î goes to (1, 0) and ĵ goes to (1, 1), just like A. The order used by hand in section 6 (AᵀA, then eigenvalues, then square roots, then u = A v ÷ σ) is exactly this path.
5. Properties to know
| Property | What it means |
|---|---|
Singular values are always 0 or more, written largest first (σ1 ≥ σ2 ≥ 0) | They are stretch amounts, so they are never negative. Pointing the other way is the job of U or V. For example, the singular values of [−3 0 / 0 1] are also 3 and 1 |
| An arrow of length 1 ends up with a length between σ2 and σ1 | σ1 is the most A stretches anything, and σ2 is the least. Whatever direction you put in, the result is in between |
σ1 × σ2 = |determinant| (2×2) | A circle with area π becomes an ellipse with area π × σ1 × σ2. The determinant tells you what area gets multiplied by, so the two match. The lab's readout always shows them equal |
| The number of singular values that are not 0 = the rank | Each direction that gets squashed flat has a singular value of 0. Press "Squash": σ2 = 0 and the rank is 1 (see rank) |
| Singular values = square roots of the eigenvalues of AᵀA | AᵀA is the matrix that gives the squared stretched length |A x|². The v's are eigenvectors of AᵀA, and the u's are eigenvectors of AAᵀ. Section 4 shows where this comes from, one step at a time |
6. Work one out by hand
An easy one: [3 0 / 0 1]
This matrix multiplies across by 3 and up and down by 1. It is already a stretch along the axes, so there is nothing to turn. So U = [1 0 / 0 1], Σ = [3 0 / 0 1], Vᵀ = [1 0 / 0 1], and the singular values are 3 and 1. The circle becomes an ellipse with radius 3 across and radius 1 up and down.
One step further: A = [0 2 / 1 0]
This matrix sends î to (0, 1) and ĵ to (2, 0). Follow along step by step.
- Find Aᵀ. Swap the rows and the columns.
Aᵀ = [0 1 / 2 0] - Multiply AᵀA.
AᵀA = [0×0+1×1 0×2+1×0 / 2×0+0×1 2×2+0×0] = [1 0 / 0 4] - Find the eigenvalues of AᵀA. It is a diagonal matrix, so the diagonal entries are the eigenvalues. Largest first: 4, 1.
- Their square roots are the singular values.
σ1 = √4 = 2,σ2 = √1 = 1 - The eigenvectors are the v's. For eigenvalue 4,
v1 = (0, 1). For eigenvalue 1,v2 = (1, 0) - For each u, put v into A and divide by σ.
u1 = A v1 ÷ σ1 = (2, 0) ÷ 2 = (1, 0),u2 = A v2 ÷ σ2 = (0, 1) ÷ 1 = (0, 1) - Put it together.
U = [1 0 / 0 1](columns u1, u2),Σ = [2 0 / 0 1],Vᵀ = [0 1 / 1 0](rows v1, v2) - Check.
U × Σ × Vᵀ = [2 0 / 0 1] × [0 1 / 1 0] = [0 2 / 1 0], which is A.σ1 × σ2 = 2and|determinant| = |0×0 − 2×1| = 2, so they match.
Written as equations, the same solution looks like this.
In equations, (0, 1) is written standing up, as a column. It is the same arrow.
Vᵀ = [0 1 / 1 0] is a flip that swaps across and up. The determinant is −2, which is negative, so there is one flip inside. Press "Flip" in the lab and you get the same U, Σ and Vᵀ.
If AᵀA is not diagonal, find its eigenvalues the way the eigenvectors page shows. For a 2×2 matrix, solve λ² − (sum of the diagonal) × λ + (determinant) = 0.
7. Drop the small ones: low-rank approximation
In the formula from section 2, A = σ1 u1 v1ᵀ + σ2 u2 v2ᵀ, if σ2 is small, the second piece adds only a little to A. Drop that piece, and what is left, A₁ = σ1 u1 v1ᵀ, is a matrix of rank 1: it presses the plane onto one line.
This A₁ is the closest matrix to A among all matrices of rank 1. The size of what you dropped, A − A₁ = σ2 u2 v2ᵀ, is exactly σ2 (size here means: square the four differences, add them, take the square root). No matrix of rank 1 is closer to A.
The same holds for big matrices. Keep only the k largest singular values, and you get the closest matrix to the original among all matrices of rank k (the Eckart-Young theorem). You also store fewer numbers. A 1000×1000 matrix holds 1 million numbers. Keep k = 50, and you need 50 u's (50,000 numbers), 50 v's (50,000) and 50 σ's: 100,050 numbers in all, about 10% of the original.
Try this. In "Shear," turn on "Drop the small one." The ellipse becomes a line segment. The matrix left is [0.72 1.17 / 0.45 0.72], A minus that is [0.28 -0.17 / -0.45 0.28], and the size of the difference is σ2 = 0.62. Turn it on in "2× across, 0.5× up" and you get [2 0 / 0 0]: the up-and-down part, which stretches less, is dropped. In "Squash," σ2 is already 0, so nothing changes.
8. How is this different from eigenvalues?
An eigenvector is "an arrow whose direction stays the same after A," and its eigenvalue is how much that arrow is stretched. That sounds close to singular values, but it is different.
| Eigendecomposition | Singular value decomposition | |
|---|---|---|
| Works for | Some square matrices | Every matrix (rectangular ones too) |
| Directions | The direction going in is the direction coming out. Eigenvectors may not be at right angles to each other | The direction going in (v) and coming out (u) can differ. The v's are always at right angles to each other, and so are the u's |
| Stretch amounts | Can be negative or complex | Always real numbers of 0 or more |
| Formula | A = P D P⁻¹ (columns of P are eigenvectors, the diagonal of D holds the eigenvalues) | A = U Σ Vᵀ |
The link between them: singular values = square roots of the eigenvalues of AᵀA (see section 4). If A is symmetric (A = Aᵀ), the singular values are the absolute values of the eigenvalues.
For example, the shear [1 1 / 0 1] has only one eigenvalue, 1, and only one eigenvector direction, (1, 0), so it has no eigendecomposition. Its singular values still come out fine: 1.62 and 0.62. A rotation matrix that is not 0° or 180° turns every direction, so it has no real eigenvectors. Its singular values are 1 and 1.
Where is it used?
- The idea behind image compression. A grayscale image is a table of brightness numbers, which is a matrix. Keep only a few of the largest singular values, and the big shapes stay. The more you keep, the closer it gets to the original. Real JPEG files use a different method, but this is the classic example of "keep only what matters."
- Recommendation systems. Approximate a people × products rating table with a low rank, and a few "taste axes" describe both people and products. This became widely known through the Netflix Prize (2006 to 2009). The table has many empty cells, so instead of using SVD as is, people train two low-rank matrices with the same idea.
- Principal component analysis (PCA). Subtract the average from the data table and run SVD. Then v1, v2, ... are the directions where the data spreads out the most (the principal components), and the square of each singular value is proportional to the spread (variance) in that direction. Real libraries often compute PCA this way.
- Removing noise. Noise often spreads over the small singular values. Drop those, and much of the noise goes away while the main pattern stays.
- Least squares and the pseudoinverse. Used to find the "least wrong answer" when a system of equations has no exact answer (least squares). Replace each singular value that is not 0 with 1/σ to build
A⁺ = V Σ⁺ Uᵀ. This is the pseudoinverse, andx = A⁺ bis the least-squares answer (the shortest one, if there are many). It works even for matrices with no inverse. NumPy'spinvandlstsqare computed with SVD too. - LoRA and low rank in AI. When fine-tuning a large language model, LoRA does not change the whole weight matrix. It writes the change as the product of two matrices with a small rank (for example 8). For a 4096×4096 matrix, the numbers to train drop from about 16.78 million to 65,536 (about 0.4%). The idea is the low-rank one: a few important directions are enough. LoRA itself does not compute an SVD.
Common questions
What is the singular value decomposition (SVD)?
It splits what any matrix does into three steps: turn, stretch along the axes, turn again. It is written A = U Σ Vᵀ. U and V turn (and sometimes flip). The stretch amounts on the diagonal of Σ are the singular values.
How are singular values different from eigenvalues?
Eigenvalues exist only for square matrices, and they can be negative or complex. The eigendecomposition works only for some of those matrices. Every matrix has singular values, and they are always real numbers of 0 or more. The singular values are the square roots of the eigenvalues of AᵀA.
What does a singular value of 0 mean?
It means that direction has been squashed flat and is gone. The number of singular values that are not 0 is the rank. If a square matrix has a singular value of 0, its determinant is also 0, so it has no inverse.
Are U and V unique?
No. You can reverse a matching pair u and v together, and the answer is still right. If two singular values are equal, there are even more directions to choose from. The singular values themselves are always unique.
Does it work for matrices that are not square?
Yes. For an m×n matrix, U is m×m, Σ is m×n, and V is n×n. There are as many singular values as the smaller of m and n.
Words
| Word used here | Word used in textbooks |
|---|---|
| splitting into turn, stretch, turn again | singular value decomposition (SVD) |
| stretch amounts σ1, σ2 | singular values |
| the two input directions v1, v2 | right singular vectors |
| the two output directions u1, u2 | left singular vectors |
| a matrix that only turns (sometimes flips), like U and V | orthogonal matrix |
| a matrix with numbers only on its diagonal, like Σ | diagonal matrix |
| the matrix with rows and columns swapped | transpose. Written Aᵀ |
| the number of directions that are not squashed | rank |
| dropping the small ones | low-rank approximation, truncated SVD |
| an arrow whose direction stays the same, and how much it is stretched | eigenvector and eigenvalue |
| an "undo" matrix you can use even when there is no inverse | pseudoinverse (Moore-Penrose inverse). Written A⁺ |
| finding the directions where data spreads the most | principal component analysis (PCA) |
← Projection and least squaresTensors and shapes: axes, shape and broadcasting →