Scientia sine finibus

Linear Algebra / Unit 6, Orthogonality / Topic 6.3

Units and Topics

Unit 6. Orthogonality

6.3 Orthogonal Matrices and QR Factorization

This topic studies square matrices with orthonormal columns, which are called orthogonal matrices. They are the maps that keep all lengths and angles, such as rotations and reflections. The topic then writes any matrix with independent columns as a product A = QR, which records the Gram-Schmidt process.

You need Topics 3.3, 3.4, 5.1, and 6.2 for this topic.

1. Orthogonal Matrices

A square matrix Q is called orthogonal when its columns are orthonormal. From Topic 6.2, this means QT Q = I. For a square matrix, that equation says that QT is the inverse of Q.

QT Q = I so Q⁻¹ = QT

The inverse of an orthogonal matrix costs nothing to calculate: exchange the rows and the columns. Because QQT = I is then also true, the rows of an orthogonal matrix are orthonormal as well.

The name is a historical accident. An orthogonal matrix has orthonormal columns, not only orthogonal columns. A matrix with orthogonal columns of other lengths is not called orthogonal.

Examples

MatrixMapDeterminant
cos θ−sin θsin θcos θRotation by θ1
100−1Reflection across the x-axis−1
0110Reflection across the line y = x. It is also a permutation matrix−1
0.6−0.80.80.6Rotation by about 53°1

Each permutation matrix of Topic 3.3 is orthogonal, which is the reason that its inverse is its transpose.

2. Lengths and Angles Do Not Change

The rule of Topic 3.3 moves a matrix across a dot product as its transpose. For an orthogonal matrix,

(Qx) · (Qy) = x · (QT Qy)move Q to the other side as its transpose(Qx) · (Qy) = x · ybecause QT Q = I

An orthogonal matrix keeps all dot products. Lengths and angles are defined by dot products, so they are kept as well.

  • |Qx| = |x| for each vector x.
  • The angle between Qx and Qy is the angle between x and y.
  • A shape and its image under Q have the same size and the same form.

Try it: maps that keep lengths and angles

Choose a map and move the slider to set the angle. Drag the tips of u and v. The dashed arrows are the images of the two vectors. The readout compares the lengths and the dot product before and after the map.

3. More Properties

  • The determinant of an orthogonal matrix is 1 or −1, because det(QT Q) = (det Q)² = 1. In two and three dimensions, the orthogonal matrices with the determinant 1 are the rotations. Those with the determinant −1 include a reflection.
  • The product of two orthogonal matrices is orthogonal: (Q₁Q₂)T(Q₁Q₂) = Q₂T Q₁T Q₁Q₂ = I. One rotation after another is a rotation.
  • Each eigenvalue of an orthogonal matrix has the size 1. If Qv = λv, the lengths of the two sides are equal, so the size of λ is 1. Real eigenvalues can only be 1 or −1, and the others are complex numbers of size 1.

From Topic 4.6, a change of basis between two orthonormal bases has an orthogonal matrix. Rotating the coordinate axes is the most common example in physics. The complex version of an orthogonal matrix is called a unitary matrix. In quantum mechanics, the change of a state in time is a unitary map, which keeps the total probability equal to 1. Topic 8.4 studies unitary matrices.

4. A Matrix with Orthonormal Columns That Is Not Square

A matrix Q with more rows than columns can also have orthonormal columns. Then QT Q = I is still true, and Q still keeps lengths. But Q has no inverse, and QQT is not the identity matrix. From Topic 6.2, QQT is the projection matrix onto the column space of Q.

5. The QR Factorization

The Gram-Schmidt process turns the independent columns a₁, …, aₙ of a matrix A into orthonormal vectors q₁, …, qₙ. Each original column is a combination of the new vectors up to its own position: a₁ is a multiple of q₁, a₂ is a combination of q₁ and q₂, and so on. Written as one matrix equation, this is

A = QR

The columns of Q are the orthonormal vectors, and R is upper triangular. Because QT Q = I, the matrix R can be found from R = QT A. Its entries are dot products: the entry in row i and column j is qi · aj.

An example

A = 3142 a₁ = 34 a₂ = 12

The first column has the length 5, so q₁ = (0.6, 0.8). The dot product q₁ · a₂ is 0.6 + 1.6 = 2.2. Subtract the projection.

a₂ − 2.2q₁ = 12 − 1.321.762.2 times q₁ is (1.32, 1.76)a₂ − 2.2q₁ = −0.320.24subtract the matching components

This vector has the length 0.4, so q₂ = (−0.8, 0.6). The lengths 5 and 0.4 and the dot product 2.2 are the entries of R.

Q = 0.6−0.80.80.6 R = 52.200.4 QR = 3142

Check the determinant:

det A = 6 − 4 = 2(det Q)(det R) = (1)(5)(0.4) = 2

6. Uses of the QR Factorization

Solving a system

If A = QR, the system Ax = b becomes QRx = b. Multiply both sides by QT.

Rx = QTb

The matrix R is upper triangular, so the system is solved by back substitution. No inverse is needed, because the inverse of Q is its transpose. Topic 6.4 uses the same equation for least squares.

Accuracy

Multiplying by an orthogonal matrix does not change the lengths of vectors, so it does not make rounding errors larger. For this reason, computer programs prefer methods that are built from orthogonal matrices.

Finding eigenvalues

Topic 5.2 stated that computer programs do not find eigenvalues from the characteristic polynomial. The standard method is called the QR algorithm. Factor the matrix as A = QR, and then multiply the factors in the reverse order to get a new matrix A₁ = RQ. The new matrix is similar to A, because RQ = QT(QR)Q = QT AQ, so it has the same eigenvalues. Repeat the two steps with A₁. For most matrices, the result approaches a triangular matrix, and the eigenvalues appear on its diagonal. The Python practice of this topic carries out the method.

Common Mistakes

  • Calling a matrix orthogonal because its columns are perpendicular. The columns must also have the length 1.
  • Using Q⁻¹ = QT for a matrix that is not orthogonal.
  • Assuming that QQT = I for a matrix with orthonormal columns that is not square.
  • Thinking that an orthogonal matrix must have the determinant 1. Reflections have the determinant −1.
  • Thinking that a determinant of 1 or −1 makes a matrix orthogonal. The shear has the determinant 1, and it is not orthogonal.
  • Calculating R as AQT. The correct product is R = QT A.

Summary

FactStatement
Orthogonal matrixA square matrix with orthonormal columns, QT Q = I
InverseQ⁻¹ = QT
What it keepsDot products, lengths, and angles
Determinant1 or −1
QR factorizationA = QR, with orthonormal columns in Q and an upper triangular R = QT A
Solving Ax = bRx = QTb, by back substitution

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Is 0.80.6−0.60.8 orthogonal? Find its inverse.

0.64 + 0.36 = 1each column has the length 10.48 − 0.48 = 0the dot product of the two columns

So the matrix is orthogonal. Its inverse is its transpose, 0.8−0.60.60.8.

2. Is 111−1 orthogonal?

No. The columns are perpendicular, but each has the length √2.

Dividing the matrix by √2 gives an orthogonal matrix.

3. Q is an orthogonal matrix and x = (3, 4). What is the length of Qx?

An orthogonal matrix keeps lengths, so |Qx| = |x| = 5.

4. Find the QR factorization of A = 1110.

q₁ = (1/√2)(1, 1)the first column divided by its lengthq₁ · a₂ = 1/√2the dot producta₂ − 1/√2q₁ = (1, 0) − (1/2, 1/2) = (1/2, −1/2)subtract the projectionq₂ = (1/√2)(1, −1)divide by the length 1/√2

Q = (1/√2) 111−1 and R = √21/√201/√2.

5. Use Q and R from Part 5 to solve Ax = b for b = (5, 8).

QTb = (0.6(5) + 0.8(8), −0.8(5) + 0.6(8)) = (9.4, 0.8)0.4y = 0.8back substitution, row 2y = 25x + 2.2(2) = 9.4back substitution, row 1x = 13 + 2 = 5the check in the first equation4 + 4 = 8the check in the second equation

6. Explain why a matrix that keeps all lengths must keep all angles.

The dot product can be written with lengths only: x · y = (|x + y|² − |x|² − |y|²) / 2.

A map that keeps all lengths therefore keeps all dot products, and the angle is found from dot products.

Python Practice

Step 1. Test whether a matrix is orthogonal

A matrix Q is orthogonal when QTQ is the identity matrix. The expression Q.T is the transpose, and the @ sign multiplies matrices. The code below tests the matrix of Practice Problem 1.

Python

The result is the identity matrix, apart from very small rounding errors, so the matrix is orthogonal.

The matrix in the next box has perpendicular columns, but it is not orthogonal. Divide it by the number that makes it orthogonal. The instruction np.sqrt is the square root, and the / sign divides each entry of a matrix by a number.

Python

Step 2. Check that lengths do not change

The instruction np.linalg.norm calculates the length of a vector. The code below compares the length of a vector before and after a rotation by 70 degrees.

Python

The vector changes, but both lengths are 5. Change the angle to any other number and run the code again. The length stays 5.

Step 3. Find a QR factorization

The instruction np.linalg.qr gives two results, the matrices Q and R. Two names with a comma between them receive the two results.

Python

Compare the results with Part 5. Python gives the first column of Q as (−0.6, −0.8) and the first row of R as −5, −2.2. A column of Q can be multiplied by −1 if the matching row of R is also multiplied by −1, and the product QR stays the same. Both answers are correct factorizations. The last result is the matrix A.

Step 4. Solve a system with Q and R

From Part 6, the system Ax = b becomes Rx = QTb. The code below solves the system of Practice Problem 5.

Python

The solution is (1, 2). Now solve the system with the same matrix and the right side (2, 2).

Python

Step 5. Find eigenvalues with the QR algorithm

The code below carries out the method of Part 6. From Topic 5.4, the line that starts with for repeats the indented lines, here 20 times. Each repeat factors the matrix and multiplies the factors in the reverse order. The matrix is typed with decimal points, so that Python stores its entries as decimal numbers.

Python

The result is the diagonal matrix with the entries 3 and 1. These are the eigenvalues of the starting matrix, as Topic 5.1 found.

Now use the method on the matrix with the rows 4, 1 and 2, 3, whose eigenvalues are 5 and 2.

Python

This result is upper triangular and not diagonal, because the starting matrix is not symmetric. The eigenvalues 5 and 2 are on its diagonal.

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Which of these matrices is an orthogonal matrix?

2. Q is an orthogonal matrix. Which matrix is its inverse?

3. Q is an orthogonal matrix, and x = (5, 12). Find the length of Qx.

4. Let A = 3241. In the factorization A = QR, find the two entries in the first row of R. Take the first entry as positive.

5. Solve Rx = QTb by back substitution, with R = 5201 and QTb = (9, 2).

Next: Topic 6.4, Least Squares