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.

Open In Colab Binder

The first 67 exercises are compiled from the original manuscript of Chapter 9 exercises, in the same order as in the manuscript, and are divided by topic into nine groups. Every exercise comes with a hint and a complete solution that can be expanded; computational exercises are followed by a NumPy verification, while pure proof exercises have no accompanying program. The last exercise, on the Jacobi method, is an extension exercise.

This part uses 0-based indexing. Unless stated otherwise, the inner product on Rn\mathbb{R}^n is the standard inner product ⟨x,y⟩=x⊤y⟨\mathbf{x}, \mathbf{y}⟩ = \mathbf{x}^\top\mathbf{y}. Unifying the terminology of the manuscript and changing its subscripts to 0-based indexing are not noted exercise by exercise; every change that alters the meaning of an exercise (adding or removing conditions, correcting data or notation, supplying definitions), as well as errors and ambiguities in the original manuscript, is recorded in the “Note on the original manuscript” of the corresponding exercise.

Definition and Examples of Inner Products

============================================================
  Exercise 9-1 | Axioms of a Real Inner Product
============================================================

▶ Step 1: Matrix form agrees with the expansion; (i) symmetry, (ii) linearity in the second argument
----------------------------------------

[x^T G_k y vs. the expansion]
  Computed value: -0.22580195849905899
  Expected value: -0.22580195849905882
  check passed

[(i) symmetry]
  Computed value: -0.22580195849905899
  Expected value: -0.22580195849905893
  check passed

[(ii) linearity in the second argument]
  Computed value: -2.2066737806193535
  Expected value: -2.2066737806193535
  check passed

▶ Step 2: Range for (iii): k > 9
----------------------------------------

[<(1,3),(1,3)> for k = 9]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[<(1,3),(1,3)> for k = 8 (negative)]
  Computed value: -1.0
  Expected value: -1.0
  check passed

[k = 9.5: smallest eigenvalue of G_k > 0]
  Computed value: True
  Expected value: True
  check passed

[k = 10: smallest eigenvalue of G_k > 0]
  Computed value: True
  Expected value: True
  check passed

[k = 20: smallest eigenvalue of G_k > 0]
  Computed value: True
  Expected value: True
  check passed

[k = 9: G_k is not positive definite]
  Computed value: False
  Expected value: False
  check passed

[k = 5: G_k is not positive definite]
  Computed value: False
  Expected value: False
  check passed

[k = -1: G_k is not positive definite]
  Computed value: False
  Expected value: False
  check passed

▶ Step 3: k = 10: length and perpendicular direction
----------------------------------------

[||u||]
  Computed value: 3.1622776601683795
  Expected value: 3.1622776601683795
  check passed

[<u, (3,10)>_10]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[||(3,10)||]
  Computed value: 3.1622776601683795
  Expected value: 3.1622776601683795
  check passed

[<u, (0,1)>_10 (the perpendicular direction under the standard inner product)]
  Computed value: -3.0
  Expected value: -3.0
  check passed
============================================================
  Exercise 9-2 | An Integral Inner Product on Polynomials
============================================================

▶ Step 1: Gram matrix of the basis {1, x, x^2}: H[i, j] = ∫_0^1 x^(i+j) dx = 1/(i+j+1)
----------------------------------------
[[1.     0.5    0.3333]
 [0.5    0.3333 0.25  ]
 [0.3333 0.25   0.2   ]]

[H is positive definite (smallest eigenvalue > 0)]
  Computed value: True
  Expected value: True
  check passed

[coefficient form = numerical integration]
  Computed value: 0.011455724284581886
  Expected value: 0.011455724273442361
  check passed

▶ Step 2: <x, 3x-2> and lengths
----------------------------------------

[<x, 3x-2>]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[(||1||, ||x||, ||x^2||)]
  Computed value: [np.float64(1.0), np.float64(0.5773502691896257), np.float64(0.4472135954999579)]
  Expected value: [1, np.float64(0.5773502691896258), np.float64(0.4472135954999579)]
  check passed

▶ Step 3: Angle between 1 and x
----------------------------------------

[cos θ]
  Computed value: 0.8660254037844387
  Expected value: 0.8660254037844386
  check passed

[θ]
  Computed value: 0.5235987755982987
  Expected value: 0.5235987755982988
  check passed

▶ Step 4: Subspace orthogonal to x
----------------------------------------

[coefficients of <x, p> ×12]
  Computed value: [6. 4. 3.]
  Expected value: [6, 4, 3]
  check passed

[<x, 3x-2>, <x, 2x^2-1>]
  Computed value: [np.float64(0.0), np.float64(0.0)]
  Expected value: [0, 0]
  check passed

[the two basis polynomials are linearly independent]
  Computed value: 2
  Expected value: 2
  check passed

[dimension of the orthogonal complement = 3 - rank]
  Computed value: 2
  Expected value: 2
  check passed

▶ Step 5: Cauchy-Schwarz inequality
----------------------------------------

[|<f,g>| <= ||f|| ||g||]
  Computed value: True
  Expected value: True
  check passed

[|<f,g>| <= ||f|| ||g||]
  Computed value: True
  Expected value: True
  check passed

[|<f,g>| <= ||f|| ||g||]
  Computed value: True
  Expected value: True
  check passed

[|<f,g>| <= ||f|| ||g||]
  Computed value: True
  Expected value: True
  check passed

[|<f,g>| <= ||f|| ||g||]
  Computed value: True
  Expected value: True
  check passed

[equality for f and 2f]
  Computed value: 0.05069029428408314
  Expected value: 0.05069029428408314
  check passed
============================================================
  Exercise 9-3 | Frobenius Inner Product
============================================================

▶ Step 1: tr(A^T B) = Σ a_ij b_ij, and the inner product axioms
----------------------------------------

[tr(X^T Y) = Σ x_ij y_ij]
  Computed value: -1.0856961812858006
  Expected value: -1.0856961812858006
  check passed

[symmetry]
  Computed value: -1.0856961812858006
  Expected value: -1.0856961812858006
  check passed

[linearity in the second argument]
  Computed value: 18.171022543353594
  Expected value: 18.17102254335359
  check passed

[<X, X> = Σ x_ij^2 > 0]
  Computed value: True
  Expected value: True
  check passed

▶ Step 2: Inner product and norms of the given matrices
----------------------------------------

[A^T B]
  Computed value: [[2 0]
 [7 3]]
  Expected value: [[2, 0], [7, 3]]
  check passed

[<A, B>]
  Computed value: 5
  Expected value: 5
  check passed

[(||A||, ||B||)]
  Computed value: [np.float64(3.7416573867739413), np.float64(2.449489742783178)]
  Expected value: [np.float64(3.7416573867739413), np.float64(2.449489742783178)]
  check passed

[agrees with np.linalg.norm(·, 'fro')]
  Computed value: 3.7416573867739413
  Expected value: 3.7416573867739413
  check passed

▶ Step 3: Column-wise vec(A) = (a, b, c, d): Frobenius inner product = standard inner product on R^4
----------------------------------------

[vec(A)]
  Computed value: [1 0 2 3]
  Expected value: [1, 0, 2, 3]
  check passed

[<A, B> = vec(A)^T vec(B)]
  Computed value: 5
  Expected value: 5
  check passed

▶ Step 4: Symmetric and skew-symmetric matrices are orthogonal
----------------------------------------

[n = 2: <S, K>]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[n = 2: S + K = M (orthogonal decomposition)]
  Computed value: [[-0.2813 -0.668 ]
 [-1.0552 -0.3908]]
  Expected value: [[-0.2813 -0.668 ]
 [-1.0552 -0.3908]]
  check passed

[n = 4: <S, K>]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[n = 4: S + K = M (orthogonal decomposition)]
  Computed value: [[ 0.4819 -0.2386  0.9578 -0.1998]
 [ 0.0243  1.5458  0.5451 -0.5052]
 [-0.1828  0.5405  1.9351 -0.2696]
 [-0.2436  1.0023 -0.8865 -0.2917]]
  Expected value: [[ 0.4819 -0.2386  0.9578 -0.1998]
 [ 0.0243  1.5458  0.5451 -0.5052]
 [-0.1828  0.5405  1.9351 -0.2696]
 [-0.2436  1.0023 -0.8865 -0.2917]]
  check passed
============================================================
  Exercise 9-4 | The Matrix of a Bilinear Form
============================================================

▶ Step 1: Recover the matrix A from a_ij = <e_i, e_j>
----------------------------------------

[A]
  Computed value: [[ 1. -1.]
 [-1.  4.]]
  Expected value: [[1, -1], [-1, 4]]
  check passed

[A is symmetric]
  Computed value: [[ 1. -1.]
 [-1.  4.]]
  Expected value: [[ 1. -1.]
 [-1.  4.]]
  check passed

[x^T A y = original expression]
  Computed value: -0.8247505957919986
  Expected value: -0.8247505957919985
  check passed

▶ Step 2: The matrix is not unique given only the quadratic form; A is positive definite
----------------------------------------

[x^T A2 x = x^T A x]
  Computed value: 0.31917786488105554
  Expected value: 0.31917786488105554
  check passed

[x^T A2 y ≠ x^T A y (different bilinear forms)]
  Computed value: False
  Expected value: False
  check passed

[all eigenvalues of A are positive]
  Computed value: True
  Expected value: True
  check passed

▶ Step 3: Order n: a_ij = e_i^T A e_j; spot-check ⇒ via the contrapositive and ⇐ with random vectors
----------------------------------------

[[e_i^T M e_j] recovers M]
  Computed value: [[np.float64(-1.6413972945846467), np.float64(-0.005203264171931977), np.float64(-0.6234637409883934), np.float64(0.14863152325202633)], [np.float64(-1.608187784186389), np.float64(0.2417718768768513), np.float64(0.23538091873745476), np.float64(1.5756260314314627)], [np.float64(0.3166450164719021), np.float64(0.5105466616976417), np.float64(-1.4931166849642326), np.float64(2.2527291247240275)], [np.float64(-1.9156455579583005), np.float64(1.1018018558224842), np.float64(-0.3299040738738497), np.float64(-0.8806465179277969)]]
  Expected value: [[-1.6414 -0.0052 -0.6235  0.1486]
 [-1.6082  0.2418  0.2354  1.5756]
 [ 0.3166  0.5105 -1.4931  2.2527]
 [-1.9156  1.1018 -0.3299 -0.8806]]
  check passed

[<e_0, e_1> ≠ <e_1, e_0> when M is not symmetric]
  Computed value: False
  Expected value: False
  check passed

[u^T S v = v^T S u when S is symmetric]
  Computed value: 1.6725345994053922
  Expected value: 1.6725345994053922
  check passed

▶ Step 4: A = [[2,3],[3,4]] does not give an inner product
----------------------------------------

[<(1,-1),(1,-1)>]
  Computed value: 0
  Expected value: 0
  check passed

[<(3,-2),(3,-2)>]
  Computed value: -2
  Expected value: -2
  check passed

[det]
  Computed value: -1.0000000000000004
  Expected value: -1
  check passed

[eigenvalues 3 ± √10]
  Computed value: [-0.1623  6.1623]
  Expected value: [np.float64(-0.16227766016837952), np.float64(6.16227766016838)]
  check passed
============================================================
  Exercise 9-5 | Change of Basis and Inner Products
============================================================

▶ Step 1: 2-dimensional case: M = B^T B
----------------------------------------

[M]
  Computed value: [[ 5. -3.]
 [-3.  2.]]
  Expected value: [[5, -3], [-3, 2]]
  check passed

[M is symmetric]
  Computed value: [[ 5. -3.]
 [-3.  2.]]
  Expected value: [[ 5. -3.]
 [-3.  2.]]
  check passed

[diagonal = (||v_0||^2, ||v_1||^2)]
  Computed value: [5. 2.]
  Expected value: [np.float64(5.0), np.float64(2.0)]
  check passed

[cos θ(v_0, v_1)]
  Computed value: -0.9486832980505138
  Expected value: -0.9486832980505138
  check passed

▶ Step 2: Inner product computed in the new coordinates = standard inner product
----------------------------------------

[[x]_B^T M [y]_B = x^T y]
  Computed value: -0.35765144361598944
  Expected value: -0.35765144361598955
  check passed

[expanded form]
  Computed value: -0.35765144361598955
  Expected value: -0.35765144361598955
  check passed

▶ Step 3: n-dimensional case: M_ij = <f_i, f_j>, and M is positive definite
----------------------------------------

[random B is invertible]
  Computed value: 4
  Expected value: 4
  check passed

[B^T B = [<f_i, f_j>]]
  Computed value: [[ 2.7765 -0.0872 -0.5653 -0.1143]
 [-0.0872  6.5977  1.4419 -4.3061]
 [-0.5653  1.4419  0.817   0.053 ]
 [-0.1143 -4.3061  0.053   5.3754]]
  Expected value: [[ 2.7765 -0.0872 -0.5653 -0.1143]
 [-0.0872  6.5977  1.4419 -4.3061]
 [-0.5653  1.4419  0.817   0.053 ]
 [-0.1143 -4.3061  0.053   5.3754]]
  check passed

[[u]_B^T M [v]_B = u^T v]
  Computed value: 0.42200040886616597
  Expected value: 0.4220004088661687
  check passed

[M is positive definite]
  Computed value: True
  Expected value: True
  check passed

Orthogonal Projection and the Method of Least Squares

============================================================
  Exercise 9-7 | Expansion in an Orthogonal Basis
============================================================

▶ Step 1: Pairwise inner products and the basis test
----------------------------------------

[<u0,u1>, <u0,u2>, <u1,u2>]
  Computed value: [np.float64(0.0), np.float64(0.0), np.float64(0.0)]
  Expected value: [0, 0, 0]
  check passed

[rank([u0 u1 u2])]
  Computed value: 3
  Expected value: 3
  check passed

▶ Step 2: Cosine of the angle between u0 and w
----------------------------------------

[cos θ]
  Computed value: 0.7559289460184544
  Expected value: 0.7559289460184545
  check passed

▶ Step 3: Projection coefficients c_i = <u_i, v> / <u_i, u_i>
----------------------------------------

[(c0, c1, c2)]
  Computed value: [3.     1.1111 1.5556]
  Expected value: [3, 1.1111111111111112, 1.5555555555555556]
  check passed

[c0 u0 + c1 u1 + c2 u2]
  Computed value: [5. 6. 1.]
  Expected value: [5. 6. 1.]
  check passed
============================================================
  Exercise 9-8 | Projecting a Vector onto a Plane
============================================================

▶ Step 1: Normal equations: coefficient matrix B^T B and right-hand side B^T u
----------------------------------------

[B^T B]
  Computed value: [[ 6. -1.]
 [-1.  6.]]
  Expected value: [[6, -1], [-1, 6]]
  check passed

[B^T u]
  Computed value: [13. -8.]
  Expected value: [13, -8]
  check passed

▶ Step 2: Solve for (α, β)
----------------------------------------

[(α, β)]
  Computed value: [ 2. -1.]
  Expected value: [2, -1]
  check passed

▶ Step 3: Projection vector and check of perpendicularity
----------------------------------------

[u_p]
  Computed value: [ 1.  2. -2.  5.]
  Expected value: [1, 2, -2, 5]
  check passed

[B^T (u - u_p)]
  Computed value: [0. 0.]
  Expected value: [0, 0]
  check passed
============================================================
  Exercise 9-12 | Best-Fit Line
============================================================

▶ Step 1: Part 2: build A = (a 1) and the normal equations
----------------------------------------

[A^T A]
  Computed value: [[18.  8.]
 [ 8.  4.]]
  Expected value: [[18, 8], [8, 4]]
  check passed

[A^T b]
  Computed value: [13.  6.]
  Expected value: [13, 6]
  check passed

▶ Step 2: Solve the normal equations for (m, k)
----------------------------------------

[(m, k)]
  Computed value: [0.5 0.5]
  Expected value: [0.5, 0.5]
  check passed

[agrees with np.linalg.lstsq]
  Computed value: [0.5 0.5]
  Expected value: [0.5 0.5]
  check passed

▶ Step 3: Residual perpendicular to Col(A); minimum sum of squared errors
----------------------------------------

[residual r]
  Computed value: [-0.  -0.5  0.5  0. ]
  Expected value: [0, -0.5, 0.5, 0]
  check passed

[A^T r]
  Computed value: [-0. -0.]
  Expected value: [0, 0]
  check passed

[‖r‖²]
  Computed value: 0.5
  Expected value: 0.5
  check passed

▶ Step 4: Parts 3 and 4: averages formula and passing through the center (including random data)
----------------------------------------

[averages formula (data of part 2)]
  Computed value: [0.5 0.5]
  Expected value: [0.5, 0.5]
  check passed

[m·ā + k = b̄ (data of part 2)]
  Computed value: 1.5
  Expected value: 1.5
  check passed

[averages formula = lstsq (random data)]
  Computed value: [ 3.0027 -1.097 ]
  Expected value: [ 3.0027 -1.097 ]
  check passed

[m·ā + k = b̄ (random data)]
  Computed value: -0.5217863913811589
  Expected value: -0.5217863913811593
  check passed
============================================================
  Exercise 9-13 | Best-Fit Plane
============================================================

▶ Step 1: Normal equations: A^T A and A^T z
----------------------------------------

[A^T A]
  Computed value: [[2. 0. 0.]
 [0. 2. 0.]
 [0. 0. 5.]]
  Expected value: [[2 0 0]
 [0 2 0]
 [0 0 5]]
  check passed

[A^T z]
  Computed value: [ 2.  1. -1.]
  Expected value: [2, 1, -1]
  check passed

▶ Step 2: Solve for (m, n, k)
----------------------------------------

[(m, n, k)]
  Computed value: [ 1.   0.5 -0.2]
  Expected value: [1, 0.5, -0.2]
  check passed

[agrees with np.linalg.lstsq]
  Computed value: [ 1.   0.5 -0.2]
  Expected value: [ 1.   0.5 -0.2]
  check passed

▶ Step 3: Residual perpendicular to Col(A), minimum sum of squared errors, passing through the center
----------------------------------------

[residual r]
  Computed value: [ 0.2  0.2 -0.3  0.2 -0.3]
  Expected value: [0.2, 0.2, -0.3, 0.2, -0.3]
  check passed

[A^T r]
  Computed value: [ 0.  0. -0.]
  Expected value: [0, 0, 0]
  check passed

[‖r‖²]
  Computed value: 0.3
  Expected value: 0.3
  check passed

[m·x̄ + n·ȳ + k = z̄]
  Computed value: -0.2
  Expected value: -0.2
  check passed

Gram-Schmidt Orthogonalization and QR Decomposition

============================================================
  Exercise 9-14 | Gram-Schmidt and QR
============================================================

▶ Step 1: Build x_0, x_1, x_2 and set w_0 = x_0
----------------------------------------

[||w_0||]
  Computed value: 3.0
  Expected value: 3
  check passed

▶ Step 2: w_1 = x_1 - Proj_{W_0}(x_1)
----------------------------------------

[a_01]
  Computed value: 0.3333333333333333
  Expected value: 0.3333333333333333
  check passed

[w_1]
  Computed value: [ 0.6667 -0.6667  0.3333]
  Expected value: [0.6666666666666666, -0.6666666666666666, 0.3333333333333333]
  check passed

[||w_1||]
  Computed value: 1.0
  Expected value: 1
  check passed

▶ Step 3: w_2 = x_2 - Proj_{W_1}(x_2), then rewrite it as a combination of x_0, x_1
----------------------------------------

[Proj_{W_1}(x_2)]
  Computed value: [0.4444 0.2222 0.5556]
  Expected value: [0.4444444444444444, 0.2222222222222222, 0.5555555555555556]
  check passed

[w_2]
  Computed value: [-0.4444 -0.2222  0.4444]
  Expected value: [-0.4444444444444444, -0.2222222222222222, 0.4444444444444444]
  check passed

[||w_2||]
  Computed value: 0.6666666666666666
  Expected value: 0.6666666666666666
  check passed

[Proj = a_02 x_0 + a_12 x_1]
  Computed value: [0.4444 0.2222 0.5556]
  Expected value: [0.4444 0.2222 0.5556]
  check passed

[(a_02, a_12)]
  Computed value: [np.float64(0.11111111111111109), np.float64(0.33333333333333337)]
  Expected value: [0.1111111111111111, 0.3333333333333333]
  check passed

▶ Step 4: Orthogonality and X = W U^{-1}
----------------------------------------

[off-diagonal entries of W^T W]
  Computed value: [[ 0.  0.  0.]
 [ 0.  0. -0.]
 [ 0. -0.  0.]]
  Expected value: [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
  check passed

[W = X U]
  Computed value: [[ 1.      0.6667 -0.4444]
 [ 2.     -0.6667 -0.2222]
 [ 2.      0.3333  0.4444]]
  Expected value: [[ 1.      0.6667 -0.4444]
 [ 2.     -0.6667 -0.2222]
 [ 2.      0.3333  0.4444]]
  check passed

[U^{-1}]
  Computed value: [[1.     0.3333 0.2222]
 [0.     1.     0.3333]
 [0.     0.     1.    ]]
  Expected value: [[1, 0.3333333333333333, 0.2222222222222222], [0, 1, 0.3333333333333333], [0, 0, 1]]
  check passed

[X = W U^{-1}]
  Computed value: [[1. 1. 0.]
 [2. 0. 0.]
 [2. 1. 1.]]
  Expected value: [[1. 1. 0.]
 [2. 0. 0.]
 [2. 1. 1.]]
  check passed

▶ Step 5: Normalize to get Q; R = D U^{-1} = Q^T X
----------------------------------------

[Q]
  Computed value: [[ 0.3333  0.6667 -0.6667]
 [ 0.6667 -0.6667 -0.3333]
 [ 0.6667  0.3333  0.6667]]
  Expected value: [[ 0.3333  0.6667 -0.6667]
 [ 0.6667 -0.6667 -0.3333]
 [ 0.6667  0.3333  0.6667]]
  check passed

[R]
  Computed value: [[3.     1.     0.6667]
 [0.     1.     0.3333]
 [0.     0.     0.6667]]
  Expected value: [[3, 1, 0.6666666666666666], [0, 1, 0.3333333333333333], [0, 0, 0.6666666666666666]]
  check passed

[R = Q^T X]
  Computed value: [[ 3.      1.      0.6667]
 [ 0.      1.      0.3333]
 [-0.     -0.      0.6667]]
  Expected value: [[3.     1.     0.6667]
 [0.     1.     0.3333]
 [0.     0.     0.6667]]
  check passed

[Q^T Q = I]
  Computed value: [[ 1.  0. -0.]
 [ 0.  1. -0.]
 [-0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[QR = X]
  Computed value: [[ 1.  1.  0.]
 [ 2.  0. -0.]
 [ 2.  1.  1.]]
  Expected value: [[1. 1. 0.]
 [2. 0. 0.]
 [2. 1. 1.]]
  check passed
============================================================
  Exercise 9-15 | Stepwise Orthogonalization
============================================================

▶ Step 1: Build q_0, q_1, q_2 one at a time
----------------------------------------

[q_0]
  Computed value: [0.3333 0.6667 0.6667]
  Expected value: [0.3333 0.6667 0.6667]
  check passed

[q_1]
  Computed value: [ 0.6667 -0.6667  0.3333]
  Expected value: [ 0.6667 -0.6667  0.3333]
  check passed

[q_2]
  Computed value: [-0.6667 -0.3333  0.6667]
  Expected value: [-0.6667 -0.3333  0.6667]
  check passed

▶ Step 2: Check <x_k, q_k> = ||w_k|| and X = QR
----------------------------------------

[diagonal entries (||w_0||, ||w_1||, ||w_2||)]
  Computed value: [3.     1.     0.6667]
  Expected value: [3, 1, 0.6666666666666666]
  check passed

[<x_k, q_k>]
  Computed value: [np.float64(3.0), np.float64(1.0), np.float64(0.6666666666666666)]
  Expected value: [3, 1, 0.6666666666666666]
  check passed

[R]
  Computed value: [[3.     1.     0.6667]
 [0.     1.     0.3333]
 [0.     0.     0.6667]]
  Expected value: [[3, 1, 0.6666666666666666], [0, 1, 0.3333333333333333], [0, 0, 0.6666666666666666]]
  check passed

[QR = X]
  Computed value: [[ 1.  1.  0.]
 [ 2.  0. -0.]
 [ 2.  1.  1.]]
  Expected value: [[1. 1. 0.]
 [2. 0. 0.]
 [2. 1. 1.]]
  check passed

▶ Step 3: Compare with the previous exercise and with numpy's QR (diagonal made positive)
----------------------------------------

[Q agrees with numpy]
  Computed value: [[ 0.3333  0.6667 -0.6667]
 [ 0.6667 -0.6667 -0.3333]
 [ 0.6667  0.3333  0.6667]]
  Expected value: [[ 0.3333  0.6667 -0.6667]
 [ 0.6667 -0.6667 -0.3333]
 [ 0.6667  0.3333  0.6667]]
  check passed

[R agrees with numpy]
  Computed value: [[3.     1.     0.6667]
 [0.     1.     0.3333]
 [0.     0.     0.6667]]
  Expected value: [[3.     1.     0.6667]
 [0.     1.     0.3333]
 [0.     0.     0.6667]]
  check passed
============================================================
  Exercise 9-16 | QR Decomposition of Square Matrices
============================================================

▶ Step 1: Part 1: QR decomposition of A
----------------------------------------

[Q]
  Computed value: [[ 0.7071  0.     -0.7071]
 [ 0.      1.      0.    ]
 [ 0.7071  0.      0.7071]]
  Expected value: [[np.float64(0.7071067811865475), 0, np.float64(-0.7071067811865475)], [0, 1, 0], [np.float64(0.7071067811865475), 0, np.float64(0.7071067811865475)]]
  check passed

[R]
  Computed value: [[1.4142 0.     0.7071]
 [0.     1.     0.    ]
 [0.     0.     0.7071]]
  Expected value: [[np.float64(1.4142135623730951), 0, np.float64(0.7071067811865475)], [0, 1, 0], [0, 0, np.float64(0.7071067811865475)]]
  check passed

[Q^T Q = I]
  Computed value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[QR = A]
  Computed value: [[ 1.  0. -0.]
 [ 0.  1.  0.]
 [ 1.  0.  1.]]
  Expected value: [[1 0 0]
 [0 1 0]
 [1 0 1]]
  check passed

▶ Step 2: Part 2: first confirm that B is invertible
----------------------------------------

[rank(B)]
  Computed value: 4
  Expected value: 4
  check passed

[det(B)]
  Computed value: -15.999999999999998
  Expected value: -16
  check passed

▶ Step 3: Part 2: QR decomposition of B
----------------------------------------

[Q]
  Computed value: [[ 0.5  0.5  0.5 -0.5]
 [ 0.5 -0.5  0.5  0.5]
 [ 0.5  0.5 -0.5  0.5]
 [ 0.5 -0.5 -0.5 -0.5]]
  Expected value: [[ 0.5  0.5  0.5 -0.5]
 [ 0.5 -0.5  0.5  0.5]
 [ 0.5  0.5 -0.5  0.5]
 [ 0.5 -0.5 -0.5 -0.5]]
  check passed

[R]
  Computed value: [[ 2. -2.  2. -2.]
 [ 0.  2. -2.  2.]
 [ 0.  0.  2. -2.]
 [ 0.  0.  0.  2.]]
  Expected value: [[2, -2, 2, -2], [0, 2, -2, 2], [0, 0, 2, -2], [0, 0, 0, 2]]
  check passed

[Q^T Q = I]
  Computed value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  Expected value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  check passed

[QR = B]
  Computed value: [[ 1.  0.  1. -2.]
 [ 1. -2.  3. -2.]
 [ 1.  0. -1.  2.]
 [ 1. -2.  1. -2.]]
  Expected value: [[ 1  0  1 -2]
 [ 1 -2  3 -2]
 [ 1  0 -1  2]
 [ 1 -2  1 -2]]
  check passed

[det(Q)·det(R)]
  Computed value: -15.999999999999998
  Expected value: -16
  check passed
============================================================
  Exercise 9-17 | Solving a System with QR
============================================================

▶ Step 1: Check the given QR decomposition
----------------------------------------

[Q^T Q = I]
  Computed value: [[ 1. -0.  0.]
 [-0.  1. -0.]
 [ 0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[QR = A]
  Computed value: [[ 2. -2.  2.]
 [ 2.  0. -3.]
 [ 1.  1.  5.]]
  Expected value: [[ 2. -2.  2.]
 [ 2.  0. -3.]
 [ 1.  1.  5.]]
  check passed

▶ Step 2: c = Q^T b
----------------------------------------

[c]
  Computed value: [3. 0. 6.]
  Expected value: [3, 0, 6]
  check passed

▶ Step 3: Solve R x = c by back substitution and verify A x = b
----------------------------------------

[x]
  Computed value: [ 0.5 -0.5  1. ]
  Expected value: [0.5, -0.5, 1]
  check passed

[A x = b]
  Computed value: [ 4. -2.  5.]
  Expected value: [ 4. -2.  5.]
  check passed

[agrees with np.linalg.solve]
  Computed value: [ 0.5 -0.5  1. ]
  Expected value: [ 0.5 -0.5  1. ]
  check passed

Orthogonal Matrices and Orthogonal Transformations

============================================================
  Exercise 9-20 | Completing an Orthonormal Basis
============================================================

▶ Step 1: Part 1: q_0 = (1,0,0), verified for several θ and ε
----------------------------------------

[Q^T Q for θ = π/3, ε = 1]
  Computed value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[det(Q) for θ = π/3, ε = 1]
  Computed value: 0.9999999999999999
  Expected value: 1
  check passed
  verified 14 pairs (θ, ε) in total

▶ Step 2: Part 2: q_0 = (1,2,-2)/3, build an orthonormal basis {u, v} of W
----------------------------------------

[v = q_0 × u]
  Computed value: [ 0.6667 -0.6667 -0.3333]
  Expected value: [ 0.6667 -0.6667 -0.3333]
  check passed

[Gram matrix of {q_0, u, v}]
  Computed value: [[ 1.  0. -0.]
 [ 0.  1.  0.]
 [-0.  0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

▶ Step 3: Part 2: verify that the parametric and expanded forms agree and all give orthonormal bases
----------------------------------------

[q_1 given by the parametric form for θ = 0, ε = 1]
  Computed value: [0.6667 0.3333 0.6667]
  Expected value: [0.6667 0.3333 0.6667]
  check passed

[q_2 given by the parametric form for θ = 0, ε = 1]
  Computed value: [ 0.6667 -0.6667 -0.3333]
  Expected value: [ 0.6667 -0.6667 -0.3333]
  check passed

[Gram matrix of [q_0 q_1 q_2] for θ = 0, ε = 1]
  Computed value: [[ 1.  0. -0.]
 [ 0.  1.  0.]
 [-0.  0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed
  verified 14 pairs (θ, ε) in total
============================================================
  Exercise 9-23 | 2×2 Orthogonal Matrices
============================================================

▶ Step 1: A random orthogonal matrix is always R(θ) or S(θ)
----------------------------------------

[rotation R(θ), θ = 3.0372]
  Computed value: [[-0.9946 -0.1042]
 [ 0.1042 -0.9946]]
  Expected value: [[-0.9946 -0.1042]
 [ 0.1042 -0.9946]]
  check passed

[reflection S(θ), θ = 4.2696]
  Computed value: [[-0.4285 -0.9036]
 [-0.9036  0.4285]]
  Expected value: [[-0.4285 -0.9036]
 [-0.9036  0.4285]]
  check passed

[rotation R(θ), θ = 3.4868]
  Computed value: [[-0.941   0.3384]
 [-0.3384 -0.941 ]]
  Expected value: [[-0.941   0.3384]
 [-0.3384 -0.941 ]]
  check passed

[reflection S(θ), θ = 4.2271]
  Computed value: [[-0.4665 -0.8845]
 [-0.8845  0.4665]]
  Expected value: [[-0.4665 -0.8845]
 [-0.8845  0.4665]]
  check passed

▶ Step 2: Determinants: det R = 1, det S = -1
----------------------------------------

[det R(π/3)]
  Computed value: 1.0
  Expected value: 1
  check passed

[det S(π/3)]
  Computed value: -1.0
  Expected value: -1
  check passed

▶ Step 3: Eigenvalues of the rotation matrix: e^{±iθ}
----------------------------------------

[eigenvalues of R(π/3)]
  Computed value: [0.5-0.866j 0.5+0.866j]
  Expected value: [0.5-0.866j 0.5+0.866j]
  check passed

[R(θ)(1,-i)ᵀ = e^{iθ}(1,-i)ᵀ]
  Computed value: [0.5  +0.866j 0.866-0.5j  ]
  Expected value: [0.5  +0.866j 0.866-0.5j  ]
  check passed

[R(π) = -I (every nonzero vector is an eigenvector)]
  Computed value: [[-1. -0.]
 [ 0. -1.]]
  Expected value: [[-1. -0.]
 [-0. -1.]]
  check passed

[eigenvalues of R(π)]
  Computed value: [-1. -1.]
  Expected value: [-1, -1]
  check passed

▶ Step 4: Eigenvalues and eigenvectors of the reflection matrix
----------------------------------------

[eigenvalues of S(π/3)]
  Computed value: [-1.  1.]
  Expected value: [-1, 1]
  check passed

[S u = u]
  Computed value: [0.866 0.5  ]
  Expected value: [0.866 0.5  ]
  check passed

[S n = -n]
  Computed value: [ 0.5   -0.866]
  Expected value: [ 0.5   -0.866]
  check passed

[S = |u⟩⟨u| - |n⟩⟨n|]
  Computed value: [[ 0.5    0.866]
 [ 0.866 -0.5  ]]
  Expected value: [[ 0.5    0.866]
 [ 0.866 -0.5  ]]
  check passed

[S = I - 2|n⟩⟨n|]
  Computed value: [[ 0.5    0.866]
 [ 0.866 -0.5  ]]
  Expected value: [[ 0.5    0.866]
 [ 0.866 -0.5  ]]
  check passed
============================================================
  Exercise 9-24 | Householder Reflections
============================================================

▶ Step 1: Q for n = (1,2,1)ᵀ/√6
----------------------------------------

[Q]
  Computed value: [[ 0.6667 -0.6667 -0.3333]
 [-0.6667 -0.3333 -0.6667]
 [-0.3333 -0.6667  0.6667]]
  Expected value: [[ 0.6667 -0.6667 -0.3333]
 [-0.6667 -0.3333 -0.6667]
 [-0.3333 -0.6667  0.6667]]
  check passed

[QᵀQ]
  Computed value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[det Q]
  Computed value: -1.0000000000000004
  Expected value: -1
  check passed

▶ Step 2: A general unit vector n (ℝ⁵): T preserves inner products
----------------------------------------

[⟨Tx, Ty⟩ = ⟨x, y⟩]
  Computed value: -2.9397317989147935
  Expected value: -2.939731798914793
  check passed

▶ Step 3: Algebraic check: Qᵀ = Q, QᵀQ = I
----------------------------------------

[Qᵀ = Q]
  Computed value: [[-0.0592 -0.269   0.912   0.1467  0.2662]
 [-0.269   0.9317  0.2316  0.0373  0.0676]
 [ 0.912   0.2316  0.2148 -0.1263 -0.2292]
 [ 0.1467  0.0373 -0.1263  0.9797 -0.0369]
 [ 0.2662  0.0676 -0.2292 -0.0369  0.9331]]
  Expected value: [[-0.0592 -0.269   0.912   0.1467  0.2662]
 [-0.269   0.9317  0.2316  0.0373  0.0676]
 [ 0.912   0.2316  0.2148 -0.1263 -0.2292]
 [ 0.1467  0.0373 -0.1263  0.9797 -0.0369]
 [ 0.2662  0.0676 -0.2292 -0.0369  0.9331]]
  check passed

[QᵀQ = I₅]
  Computed value: [[ 1. -0.  0.  0.  0.]
 [-0.  1.  0.  0.  0.]
 [ 0.  0.  1. -0. -0.]
 [ 0.  0. -0.  1.  0.]
 [ 0.  0. -0.  0.  1.]]
  Expected value: [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
  check passed

▶ Step 4: Eigenvalues ±1 and eigenspaces
----------------------------------------

[eigenvalues of Q (increasing)]
  Computed value: [-1.  1.  1.]
  Expected value: [-1, 1, 1]
  check passed

[Q n = -n]
  Computed value: [-0.4082 -0.8165 -0.4082]
  Expected value: [-0.4082 -0.8165 -0.4082]
  check passed

[nᵀh = 0 (h ∈ E_1)]
  Computed value: [0. 0.]
  Expected value: [0, 0]
  check passed

[Q h = h]
  Computed value: [[ 2. -1.  0.]
 [ 1.  0. -1.]]
  Expected value: [[ 2. -1.  0.]
 [ 1.  0. -1.]]
  check passed
============================================================
  Exercise 9-25 | Reflection Across a Subspace
============================================================

▶ Step 1: A random 2-dimensional subspace of ℝ⁵: Q = 2P - I is an orthogonal matrix
----------------------------------------

[P² = P]
  Computed value: [[ 0.0318  0.0542 -0.0397  0.1171  0.1122]
 [ 0.0542  0.6012 -0.2743  0.3155 -0.2491]
 [-0.0397 -0.2743  0.1335 -0.1933  0.0386]
 [ 0.1171  0.3155 -0.1933  0.4571  0.3123]
 [ 0.1122 -0.2491  0.0386  0.3123  0.7763]]
  Expected value: [[ 0.0318  0.0542 -0.0397  0.1171  0.1122]
 [ 0.0542  0.6012 -0.2743  0.3155 -0.2491]
 [-0.0397 -0.2743  0.1335 -0.1933  0.0386]
 [ 0.1171  0.3155 -0.1933  0.4571  0.3123]
 [ 0.1122 -0.2491  0.0386  0.3123  0.7763]]
  check passed

[Pᵀ = P]
  Computed value: [[ 0.0318  0.0542 -0.0397  0.1171  0.1122]
 [ 0.0542  0.6012 -0.2743  0.3155 -0.2491]
 [-0.0397 -0.2743  0.1335 -0.1933  0.0386]
 [ 0.1171  0.3155 -0.1933  0.4571  0.3123]
 [ 0.1122 -0.2491  0.0386  0.3123  0.7763]]
  Expected value: [[ 0.0318  0.0542 -0.0397  0.1171  0.1122]
 [ 0.0542  0.6012 -0.2743  0.3155 -0.2491]
 [-0.0397 -0.2743  0.1335 -0.1933  0.0386]
 [ 0.1171  0.3155 -0.1933  0.4571  0.3123]
 [ 0.1122 -0.2491  0.0386  0.3123  0.7763]]
  check passed

[QᵀQ = I₅]
  Computed value: [[ 1.  0. -0. -0. -0.]
 [ 0.  1. -0.  0.  0.]
 [-0. -0.  1. -0.  0.]
 [-0.  0. -0.  1. -0.]
 [-0.  0.  0. -0.  1.]]
  Expected value: [[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
  check passed

▶ Step 2: Eigenvalues ±1, E_1 = W, E_{-1} = W⊥
----------------------------------------

[eigenvalues of Q (increasing)]
  Computed value: [-1. -1. -1.  1.  1.]
  Expected value: [-1, -1, -1, 1, 1]
  check passed

[Q w = w (basis of W)]
  Computed value: [[ 0.354  -0.005 ]
 [-0.5316 -2.2766]
 [ 0.0187  0.9274]
 [ 1.0433 -0.5362]
 [ 2.2294  1.9437]]
  Expected value: [[ 0.354  -0.005 ]
 [-0.5316 -2.2766]
 [ 0.0187  0.9274]
 [ 1.0433 -0.5362]
 [ 2.2294  1.9437]]
  check passed

[Bᵀ W⊥ = 0]
  Computed value: [[-0. -0.  0.]
 [-0.  0. -0.]]
  Expected value: [[0. 0. 0.]
 [0. 0. 0.]]
  check passed

[Q v = -v (basis of W⊥)]
  Computed value: [[-0.0591  0.5005  0.8451]
 [-0.3552  0.4054 -0.329 ]
 [-0.9245 -0.0998  0.0415]
 [-0.1243 -0.6798  0.2554]
 [-0.0094  0.3362 -0.3325]]
  Expected value: [[-0.0591  0.5005  0.8451]
 [-0.3552  0.4054 -0.329 ]
 [-0.9245 -0.0998  0.0415]
 [-0.1243 -0.6798  0.2554]
 [-0.0094  0.3362 -0.3325]]
  check passed

▶ Step 3: W = span{e₀, e₁} ⊂ ℝ⁴
----------------------------------------

[P_W]
  Computed value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
  Expected value: [[1 0 0 0]
 [0 1 0 0]
 [0 0 0 0]
 [0 0 0 0]]
  check passed

[Q]
  Computed value: [[ 1.  0.  0.  0.]
 [ 0.  1.  0.  0.]
 [ 0.  0. -1.  0.]
 [ 0.  0.  0. -1.]]
  Expected value: [[ 1  0  0  0]
 [ 0  1  0  0]
 [ 0  0 -1  0]
 [ 0  0  0 -1]]
  check passed

[det Q]
  Computed value: 1.0
  Expected value: 1
  check passed
============================================================
  Exercise 9-26 | Orthogonal Transformations under a Nonstandard Inner Product
============================================================

▶ Step 1: G is positive definite (leading principal minors)
----------------------------------------

[(D_1, D_2): determinants of the upper-left 1×1 and 2×2 submatrices]
  Computed value: [np.int64(2), np.float64(1.0)]
  Expected value: [2, 1]
  check passed

▶ Step 2: B is orthonormal for ⟨·,·⟩_G
----------------------------------------

[(⟨b0,b0⟩, ⟨b1,b1⟩, ⟨b0,b1⟩)]
  Computed value: [np.int64(1), np.int64(1), np.int64(0)]
  Expected value: [1, 1, 0]
  check passed

▶ Step 3: T preserves ⟨·,·⟩_G: AᵀGA = G
----------------------------------------

[AᵀGA]
  Computed value: [[2 1]
 [1 1]]
  Expected value: [[2 1]
 [1 1]]
  check passed

[⟨Tu, Tv⟩_G = ⟨u, v⟩_G]
  Computed value: -15.656094408098612
  Expected value: -15.65609440809861
  check passed

[AᵀA (not orthogonal for the standard inner product)]
  Computed value: [[5 2]
 [2 1]]
  Expected value: [[5, 2], [2, 1]]
  check passed

▶ Step 4: Matrix representation [T]_B
----------------------------------------

[[T]_B]
  Computed value: [[ 1.  0.]
 [ 0. -1.]]
  Expected value: [[1, 0], [0, -1]]
  check passed

[QᵀQ]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed
============================================================
  Exercise 9-27 | Matrix Representations of Orthogonal Transformations
============================================================

▶ Step 1: The matrix representation with respect to an orthonormal basis is orthogonal (random example in ℝ⁴)
----------------------------------------

[[T]_Bᵀ [T]_B]
  Computed value: [[ 1. -0. -0.  0.]
 [-0.  1. -0.  0.]
 [-0. -0.  1. -0.]
 [ 0.  0. -0.  1.]]
  Expected value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  check passed

▶ Step 2: Counterexample with the non-orthogonal basis A = {(1,0)ᵀ, (1,1)ᵀ}
----------------------------------------

[[T]_A]
  Computed value: [[-1. -2.]
 [ 1.  1.]]
  Expected value: [[-1, -2], [1, 1]]
  check passed

[[T]_Aᵀ [T]_A (≠ I)]
  Computed value: [[2. 3.]
 [3. 5.]]
  Expected value: [[2, 3], [3, 5]]
  check passed

[[T]_Aᵀ G_A [T]_A = G_A]
  Computed value: [[1. 1.]
 [1. 2.]]
  Expected value: [[1 1]
 [1 2]]
  check passed
============================================================
  Exercise 9-28 | Orientation of Bases
============================================================

▶ Step 1: Same orientation is an equivalence relation: multiplicativity of the determinant
----------------------------------------

[det(M⁻¹) = 1/det M]
  Computed value: -0.6314246303908053
  Expected value: -0.6314246303908053
  check passed

[det(NM) = det N · det M]
  Computed value: 2.2698124482564226
  Expected value: 2.2698124482564226
  check passed

[same orientation ⇔ dets of the same sign (only two classes)]
  Computed value: [[ True  True False  True  True False]
 [ True  True False  True  True False]
 [False False  True False False  True]
 [ True  True False  True  True False]
 [ True  True False  True  True False]
 [False False  True False False  True]]
  Expected value: [[ True  True False  True  True False]
 [ True  True False  True  True False]
 [False False  True False False  True]
 [ True  True False  True  True False]
 [ True  True False  True  True False]
 [False False  True False False  True]]
  check passed

▶ Step 2: Positively oriented ⇔ det F > 0: verify against another positively oriented basis G
----------------------------------------
  12 of the 20 random bases are positively oriented

['same orientation as the positively oriented G' ⇔ 'det F > 0']
  Computed value: [ True  True False  True False  True False  True  True  True False  True False  True False False
 False  True  True  True]
  Expected value: [ True  True False  True False  True False  True  True  True False  True False  True False False
 False  True  True  True]
  check passed

▶ Step 3: ℝ²: det(f₀ f₁) = r₀ r₁ sin φ, where φ is the signed angle from f₀ to f₁
----------------------------------------

[φ = +1.5708 (counterclockwise → positively oriented)]
  Computed value: 1.9999999999999998
  Expected value: 2.0
  check passed

[φ = +2.5000 (counterclockwise → positively oriented)]
  Computed value: 1.7954164323118693
  Expected value: 1.7954164323118693
  check passed

[φ = -1.2000 (clockwise → negatively oriented)]
  Computed value: -0.9320390859672263
  Expected value: -0.9320390859672263
  check passed

[det(e₀ e₁)]
  Computed value: 1.0
  Expected value: 1
  check passed

[det(e₁ e₀)]
  Computed value: -1.0
  Expected value: -1
  check passed

▶ Step 4: ℝ³: det(u, v, u×v) = ‖u×v‖² > 0
----------------------------------------

[det(u v u×v)]
  Computed value: 5.820236498742807
  Expected value: 5.820236498742807
  check passed

▶ Step 5: Orthogonal matrices in ℝ³: det Q = 1 ⇔ q₀ × q₁ = q₂ (right-handed), det Q = -1 ⇔ q₀ × q₁ = -q₂
----------------------------------------

[det Q = +1: q₀ × q₁ = (det Q)·q₂]
  Computed value: [0.9289 0.3283 0.1716]
  Expected value: [0.9289 0.3283 0.1716]
  check passed

[det Q = -1: q₀ × q₁ = (det Q)·q₂]
  Computed value: [ 0.2487  0.4286 -0.8686]
  Expected value: [ 0.2487  0.4286 -0.8686]
  check passed

[det Q = +1: q₀ × q₁ = (det Q)·q₂]
  Computed value: [-0.0554 -0.1586  0.9858]
  Expected value: [-0.0554 -0.1586  0.9858]
  check passed

[det Q = -1: q₀ × q₁ = (det Q)·q₂]
  Computed value: [ 0.6804 -0.676   0.283 ]
  Expected value: [ 0.6804 -0.676   0.283 ]
  check passed

▶ Step 6: Change-of-basis matrix between the two bases of W
----------------------------------------

[A M = C]
  Computed value: [[1. 1.]
 [1. 2.]
 [1. 1.]]
  Expected value: [[1. 1.]
 [1. 2.]
 [1. 1.]]
  check passed

[M]
  Computed value: [[1. 1.]
 [1. 2.]]
  Expected value: [[1, 1], [1, 2]]
  check passed

[det M]
  Computed value: 0.9999999999999997
  Expected value: 1
  check passed
============================================================
  Exercise 9-29 | Rotation Axis and Rotation Angle
============================================================

▶ Step 1: Orthogonal with det = 1
----------------------------------------

[QᵀQ]
  Computed value: [[ 1. -0.  0.]
 [-0.  1. -0.]
 [ 0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[det Q]
  Computed value: 1.0
  Expected value: 1
  check passed

▶ Step 2: Rotation axis: Ker(Q - I)
----------------------------------------

[rank(Q - I)]
  Computed value: 2
  Expected value: 2
  check passed

[unit axis vector]
  Computed value: [0.5774 0.5774 0.5774]
  Expected value: [0.5774 0.5774 0.5774]
  check passed

[Q q₂ = q₂]
  Computed value: [0.5774 0.5774 0.5774]
  Expected value: [0.5774 0.5774 0.5774]
  check passed

▶ Step 3: Rotation angle: 1 + 2cosθ = tr Q
----------------------------------------

[tr Q]
  Computed value: 2.0
  Expected value: 2
  check passed

[θ]
  Computed value: 1.0471975511965979
  Expected value: 1.0471975511965976
  check passed

[q₁ = q₂ × q₀]
  Computed value: [ 0.4082  0.4082 -0.8165]
  Expected value: [ 0.4082  0.4082 -0.8165]
  check passed

[det P (positively oriented basis)]
  Computed value: 1.0
  Expected value: 1
  check passed

[PᵀQP = R_z(π/3)]
  Computed value: [[ 0.5   -0.866  0.   ]
 [ 0.866  0.5    0.   ]
 [-0.    -0.     1.   ]]
  Expected value: [[ 0.5   -0.866  0.   ]
 [ 0.866  0.5    0.   ]
 [ 0.     0.     1.   ]]
  check passed

[Dirac expansion = Q]
  Computed value: [[ 0.6667 -0.3333  0.6667]
 [ 0.6667  0.6667 -0.3333]
 [-0.3333  0.6667  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.6667]
 [ 0.6667  0.6667 -0.3333]
 [-0.3333  0.6667  0.6667]]
  check passed
============================================================
  Exercise 9-30 | Rotation Decomposition of an Orthogonal Matrix
============================================================

▶ Step 1: Q = -Q' (the matrix of Exercise 9-29), det Q = -1
----------------------------------------

[Q = -Q']
  Computed value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  Expected value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  check passed

[QᵀQ]
  Computed value: [[ 1. -0.  0.]
 [-0.  1. -0.]
 [ 0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[det Q]
  Computed value: -1.0
  Expected value: -1
  check passed

▶ Step 2: A = Q R (column 2 negated), det A = 1
----------------------------------------

[A]
  Computed value: [[-0.6667  0.3333  0.6667]
 [-0.6667 -0.6667 -0.3333]
 [ 0.3333 -0.6667  0.6667]]
  Expected value: [[-0.6667  0.3333  0.6667]
 [-0.6667 -0.6667 -0.3333]
 [ 0.3333 -0.6667  0.6667]]
  check passed

[det A]
  Computed value: 1.0
  Expected value: 1
  check passed

▶ Step 3: Solve for α, β, γ from the entries of A
----------------------------------------

[sin β = 1/3]
  Computed value: 0.33333333333333337
  Expected value: 0.3333333333333333
  check passed

[β = arcsin A[2,0]]
  Computed value: 0.3398369094541219
  Expected value: 0.339836909454122
  check passed

[γ = atan2(A[2,1], A[2,2])]
  Computed value: -0.7853981633974483
  Expected value: -0.7853981633974483
  check passed

[α = atan2(A[1,0], A[0,0])]
  Computed value: -2.356194490192345
  Expected value: -2.356194490192345
  check passed
  β ≈ 0.3398 rad ≈ 19.47°

▶ Step 4: Check the product
----------------------------------------

[R_y(β) R_x(γ)]
  Computed value: [[ 0.9428  0.2357 -0.2357]
 [ 0.      0.7071  0.7071]
 [ 0.3333 -0.6667  0.6667]]
  Expected value: [[np.float64(0.9428090415820635), np.float64(0.2357022603955158), np.float64(-0.2357022603955158)], [0, np.float64(0.7071067811865475), np.float64(0.7071067811865475)], [0.3333333333333333, -0.6666666666666666, 0.6666666666666666]]
  check passed

[R_z(α) R_y(β) R_x(γ) R = Q]
  Computed value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  Expected value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  check passed

▶ Step 5: Another view: Q = Q' R_z(π) R, a rotoreflection
----------------------------------------

[-I = R_z(π) R]
  Computed value: [[-1. -0.  0.]
 [ 0. -1.  0.]
 [ 0.  0. -1.]]
  Expected value: [[-1. -0. -0.]
 [-0. -1. -0.]
 [-0. -0. -1.]]
  check passed

[Q = Q' R_z(π) R]
  Computed value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  Expected value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  check passed

[Q' = rotation by π/3 about q]
  Computed value: [[ 0.6667 -0.3333  0.6667]
 [ 0.6667  0.6667 -0.3333]
 [-0.3333  0.6667  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.6667]
 [ 0.6667  0.6667 -0.3333]
 [-0.3333  0.6667  0.6667]]
  check passed

[Q = rotation by 4π/3 about q, then reflection across q⊥]
  Computed value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  Expected value: [[-0.6667  0.3333 -0.6667]
 [-0.6667 -0.6667  0.3333]
 [ 0.3333 -0.6667 -0.6667]]
  check passed

[Q q = -q]
  Computed value: [-0.5774 -0.5774 -0.5774]
  Expected value: [-0.5774 -0.5774 -0.5774]
  check passed

Orthogonal Diagonalization of Symmetric Matrices

============================================================
  Exercise 9-31 | Orthogonal Diagonalization
============================================================

▶ Step 1: Eigenvalues (in increasing order) and eigenspace bases
----------------------------------------

[eigenvalues of A]
  Computed value: [0. 5.]
  Expected value: [0, 5]
  check passed

[eigenvalues of B]
  Computed value: [1. 1. 4.]
  Expected value: [1, 1, 4]
  check passed

[eigenvalues of C]
  Computed value: [-1. -1.  1.  1.]
  Expected value: [-1, -1, 1, 1]
  check passed

[Av = 0v, v = [ 2. -1.]]
  Computed value: [0. 0.]
  Expected value: [ 0. -0.]
  check passed

[Av = 5v, v = [1. 2.]]
  Computed value: [ 5. 10.]
  Expected value: [ 5. 10.]
  check passed

[Bv = 1v, v = [ 1. -1.  0.]]
  Computed value: [ 1. -1.  0.]
  Expected value: [ 1. -1.  0.]
  check passed

[Bv = 1v, v = [ 1.  0. -1.]]
  Computed value: [ 1.  0. -1.]
  Expected value: [ 1.  0. -1.]
  check passed

[Bv = 4v, v = [1. 1. 1.]]
  Computed value: [4. 4. 4.]
  Expected value: [4. 4. 4.]
  check passed

[Cv = -1v, v = [ 1.  0.  0. -1.]]
  Computed value: [-1.  0.  0.  1.]
  Expected value: [-1. -0. -0.  1.]
  check passed

[Cv = -1v, v = [ 0.  1. -1.  0.]]
  Computed value: [ 0. -1.  1.  0.]
  Expected value: [-0. -1.  1. -0.]
  check passed

[Cv = 1v, v = [1. 0. 0. 1.]]
  Computed value: [1. 0. 0. 1.]
  Expected value: [1. 0. 0. 1.]
  check passed

[Cv = 1v, v = [0. 1. 1. 0.]]
  Computed value: [0. 1. 1. 0.]
  Expected value: [0. 1. 1. 0.]
  check passed

▶ Step 2: Gram-Schmidt and the orthogonal matrix Q
----------------------------------------

[E_1 of B after Gram-Schmidt]
  Computed value: [[ 0.7071  0.4082]
 [-0.7071  0.4082]
 [ 0.     -0.8165]]
  Expected value: [[ 0.7071  0.4082]
 [-0.7071  0.4082]
 [ 0.     -0.8165]]
  check passed

V^T V when we only normalize, without Gram-Schmidt =
 [[ 1.   0.5 -0. ]
 [ 0.5  1.  -0. ]
 [-0.  -0.   1. ]]

[Q^T Q = I (A)]
  Computed value: [[ 1. -0.]
 [-0.  1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[Q^T A Q = Λ]
  Computed value: [[0. 0.]
 [0. 5.]]
  Expected value: [[0 0]
 [0 5]]
  check passed

[Q^T Q = I (B)]
  Computed value: [[ 1.  0. -0.]
 [ 0.  1. -0.]
 [-0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[Q^T B Q = Λ]
  Computed value: [[ 1.  0.  0.]
 [ 0.  1. -0.]
 [ 0. -0.  4.]]
  Expected value: [[1 0 0]
 [0 1 0]
 [0 0 4]]
  check passed

[Q^T Q = I (C)]
  Computed value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  Expected value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  check passed

[Q^T C Q = Λ]
  Computed value: [[-1.  0.  0.  0.]
 [ 0. -1.  0.  0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]
  Expected value: [[-1  0  0  0]
 [ 0 -1  0  0]
 [ 0  0  1  0]
 [ 0  0  0  1]]
  check passed

▶ Step 3: Fifth power M^5 = Q Λ^5 Q^T
----------------------------------------

[A^5 (direct multiplication)]
  Computed value: [[ 625. 1250.]
 [1250. 2500.]]
  Expected value: [[ 625 1250]
 [1250 2500]]
  check passed

[A^5 (Q Λ^5 Q^T)]
  Computed value: [[ 625. 1250.]
 [1250. 2500.]]
  Expected value: [[ 625 1250]
 [1250 2500]]
  check passed

[B^5 (direct multiplication)]
  Computed value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  Expected value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  check passed

[B^5 (Q Λ^5 Q^T)]
  Computed value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  Expected value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  check passed

[C^5 (direct multiplication)]
  Computed value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  Expected value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  check passed

[C^5 (Q Λ^5 Q^T)]
  Computed value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  Expected value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  check passed
============================================================
  Exercise 9-32 | Spectral Decomposition of a 2×2 Matrix
============================================================

▶ Step 1: Parts 1–2: properties of the projection matrices (using q0, q1 from part 5 as an example)
----------------------------------------

[P0^2 = P0]
  Computed value: [[0.2 0.4]
 [0.4 0.8]]
  Expected value: [[0.2 0.4]
 [0.4 0.8]]
  check passed

[P0^T = P0]
  Computed value: [[0.2 0.4]
 [0.4 0.8]]
  Expected value: [[0.2 0.4]
 [0.4 0.8]]
  check passed

[P0 leaves its own q unchanged]
  Computed value: [0.4472 0.8944]
  Expected value: [0.4472 0.8944]
  check passed

[P0 sends the other q to 0]
  Computed value: [0. 0.]
  Expected value: [0. 0.]
  check passed

[P1^2 = P1]
  Computed value: [[ 0.8 -0.4]
 [-0.4  0.2]]
  Expected value: [[ 0.8 -0.4]
 [-0.4  0.2]]
  check passed

[P1^T = P1]
  Computed value: [[ 0.8 -0.4]
 [-0.4  0.2]]
  Expected value: [[ 0.8 -0.4]
 [-0.4  0.2]]
  check passed

[P1 leaves its own q unchanged]
  Computed value: [ 0.8944 -0.4472]
  Expected value: [ 0.8944 -0.4472]
  check passed

[P1 sends the other q to 0]
  Computed value: [0. 0.]
  Expected value: [0. 0.]
  check passed

[P0 P1 = 0]
  Computed value: [[0. 0.]
 [0. 0.]]
  Expected value: [[0. 0.]
 [0. 0.]]
  check passed

[P1 P0 = 0]
  Computed value: [[0. 0.]
 [0. 0.]]
  Expected value: [[0. 0.]
 [0. 0.]]
  check passed

[P0 + P1 = I_2]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

▶ Step 2: Part 3: combination of outer products of the natural basis
----------------------------------------

[3 e0 e0^T - 5 e1 e1^T]
  Computed value: [[ 3.  0.]
 [ 0. -5.]]
  Expected value: [[3, 0], [0, -5]]
  check passed

[its eigenvalues (in increasing order)]
  Computed value: [-5.  3.]
  Expected value: [-5, 3]
  check passed

▶ Step 3: Parts 4–5: assemble A from the spectral decomposition and diagonalize it
----------------------------------------

[A = 4 q0 q0^T - q1 q1^T]
  Computed value: [[0. 2.]
 [2. 3.]]
  Expected value: [[0, 2], [2, 3]]
  check passed

[A is symmetric]
  Computed value: [[0. 2.]
 [2. 3.]]
  Expected value: [[0. 2.]
 [2. 3.]]
  check passed

[A q0 = 4 q0]
  Computed value: [1.7889 3.5777]
  Expected value: [1.7889 3.5777]
  check passed

[A q1 = -q1]
  Computed value: [-0.8944  0.4472]
  Expected value: [-0.8944  0.4472]
  check passed

[Q^T A Q]
  Computed value: [[ 4.  0.]
 [ 0. -1.]]
  Expected value: [[ 4  0]
 [ 0 -1]]
  check passed

▶ Step 4: Part 6: spectral decomposition of A = [[5,-1],[-1,5]]
----------------------------------------

[eigenvalues of A]
  Computed value: [4. 6.]
  Expected value: [4, 6]
  check passed

[4|q0⟩⟨q0| + 6|q1⟩⟨q1|]
  Computed value: [[ 5. -1.]
 [-1.  5.]]
  Expected value: [[ 5. -1.]
 [-1.  5.]]
  check passed
============================================================
  Exercise 9-33 | Projection Matrices and Spectral Decomposition
============================================================

▶ Step 1: Part 1: n ⊥ W and the two projection matrices
----------------------------------------

[B^T n = 0 (n ⊥ W)]
  Computed value: [0. 0.]
  Expected value: [0, 0]
  check passed

[P0 = Proj_W]
  Computed value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  check passed

[P1 = Proj_n]
  Computed value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  Expected value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  check passed

[P0^T = P0]
  Computed value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  check passed

[P0^2 = P0]
  Computed value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  check passed

[P1^T = P1]
  Computed value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  Expected value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  check passed

[P1^2 = P1]
  Computed value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  Expected value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  check passed

[P0 P1 = 0]
  Computed value: [[ 0.  0. -0.]
 [ 0.  0. -0.]
 [ 0.  0. -0.]]
  Expected value: [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
  check passed

[P1 P0 = 0]
  Computed value: [[ 0.  0.  0.]
 [ 0.  0.  0.]
 [-0. -0. -0.]]
  Expected value: [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
  check passed

[P0 + P1 = I_3]
  Computed value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

▶ Step 2: Part 2: orthogonal diagonalization of A = P0 + 4 P1
----------------------------------------

[A]
  Computed value: [[ 2.  1. -1.]
 [ 1.  2. -1.]
 [-1. -1.  2.]]
  Expected value: [[2, 1, -1], [1, 2, -1], [-1, -1, 2]]
  check passed

[eigenvalues of A]
  Computed value: [1. 1. 4.]
  Expected value: [1, 1, 4]
  check passed

[Gram-Schmidt yields q1]
  Computed value: [-0.4082  0.8165  0.4082]
  Expected value: [-0.4082  0.8165  0.4082]
  check passed

[Q^T Q = I_3]
  Computed value: [[ 1. -0.  0.]
 [-0.  1. -0.]
 [ 0. -0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[Q^T A Q = Λ]
  Computed value: [[ 1. -0.  0.]
 [-0.  1.  0.]
 [ 0.  0.  4.]]
  Expected value: [[1 0 0]
 [0 1 0]
 [0 0 4]]
  check passed

▶ Step 3: Comparison: projection matrix = Σ|q⟩⟨q| over the eigenspace
----------------------------------------

[|q0⟩⟨q0| + |q1⟩⟨q1| = P0]
  Computed value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333  0.3333]
 [-0.3333  0.6667  0.3333]
 [ 0.3333  0.3333  0.6667]]
  check passed

[|q2⟩⟨q2| = P1]
  Computed value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  Expected value: [[ 0.3333  0.3333 -0.3333]
 [ 0.3333  0.3333 -0.3333]
 [-0.3333 -0.3333  0.3333]]
  check passed
============================================================
  Exercise 9-34 | Computing Powers via the Spectral Decomposition
============================================================

▶ Step 1: Part 1: spectral decomposition M = Σ λ_i |q_i⟩⟨q_i|
----------------------------------------

[Σ λ_i |q_i⟩⟨q_i| = A]
  Computed value: [[1. 2.]
 [2. 4.]]
  Expected value: [[1. 2.]
 [2. 4.]]
  check passed

[Σ λ_i |q_i⟩⟨q_i| = B]
  Computed value: [[2. 1. 1.]
 [1. 2. 1.]
 [1. 1. 2.]]
  Expected value: [[2. 1. 1.]
 [1. 2. 1.]
 [1. 1. 2.]]
  check passed

[Σ λ_i |q_i⟩⟨q_i| = C]
  Computed value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  Expected value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  check passed

[the λ=5 term of A, |q1⟩⟨q1|]
  Computed value: [[0.2 0.4]
 [0.4 0.8]]
  Expected value: [[0.2 0.4]
 [0.4 0.8]]
  check passed

[B: projection onto E_1 = I - J/3]
  Computed value: [[ 0.6667 -0.3333 -0.3333]
 [-0.3333  0.6667 -0.3333]
 [-0.3333 -0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333 -0.3333]
 [-0.3333  0.6667 -0.3333]
 [-0.3333 -0.3333  0.6667]]
  check passed

[B: projection onto E_4 = J/3]
  Computed value: [[0.3333 0.3333 0.3333]
 [0.3333 0.3333 0.3333]
 [0.3333 0.3333 0.3333]]
  Expected value: [[0.3333 0.3333 0.3333]
 [0.3333 0.3333 0.3333]
 [0.3333 0.3333 0.3333]]
  check passed

[B: sum of projections for another basis of E_1]
  Computed value: [[ 0.6667 -0.3333 -0.3333]
 [-0.3333  0.6667 -0.3333]
 [-0.3333 -0.3333  0.6667]]
  Expected value: [[ 0.6667 -0.3333 -0.3333]
 [-0.3333  0.6667 -0.3333]
 [-0.3333 -0.3333  0.6667]]
  check passed

[C: P_- = (I - C)/2]
  Computed value: [[ 0.5  0.   0.  -0.5]
 [ 0.   0.5 -0.5  0. ]
 [ 0.  -0.5  0.5 -0. ]
 [-0.5  0.  -0.   0.5]]
  Expected value: [[ 0.5  0.   0.  -0.5]
 [ 0.   0.5 -0.5  0. ]
 [ 0.  -0.5  0.5  0. ]
 [-0.5  0.   0.   0.5]]
  check passed

[C: P_+ = (I + C)/2]
  Computed value: [[0.5 0.  0.  0.5]
 [0.  0.5 0.5 0. ]
 [0.  0.5 0.5 0. ]
 [0.5 0.  0.  0.5]]
  Expected value: [[0.5 0.  0.  0.5]
 [0.  0.5 0.5 0. ]
 [0.  0.5 0.5 0. ]
 [0.5 0.  0.  0.5]]
  check passed

▶ Step 2: Part 2: M^5 = Σ λ_i^5 |q_i⟩⟨q_i|
----------------------------------------

[Σ λ_i^5 |q_i⟩⟨q_i| (A)]
  Computed value: [[ 625. 1250.]
 [1250. 2500.]]
  Expected value: [[625, 1250], [1250, 2500]]
  check passed

[agrees with direct computation of A^5]
  Computed value: [[ 625. 1250.]
 [1250. 2500.]]
  Expected value: [[ 625. 1250.]
 [1250. 2500.]]
  check passed

[Σ λ_i^5 |q_i⟩⟨q_i| (B)]
  Computed value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  Expected value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  check passed

[agrees with direct computation of B^5]
  Computed value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  Expected value: [[342. 341. 341.]
 [341. 342. 341.]
 [341. 341. 342.]]
  check passed

[Σ λ_i^5 |q_i⟩⟨q_i| (C)]
  Computed value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  Expected value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  check passed

[agrees with direct computation of C^5]
  Computed value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  Expected value: [[0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]]
  check passed
============================================================
  Exercise 9-36 | The Rayleigh Quotient of a 2×2 Matrix
============================================================

▶ Step 1: Part 1: eigenvalues and eigenvectors
----------------------------------------

[eigenvalues of A (in increasing order)]
  Computed value: [-1.  4.]
  Expected value: [-1, 4]
  check passed

[A q0 = -q0]
  Computed value: [-0.4472  0.8944]
  Expected value: [-0.4472  0.8944]
  check passed

[A q1 = 4 q1]
  Computed value: [3.5777 1.7889]
  Expected value: [3.5777 1.7889]
  check passed

[spectral decomposition -|q0⟩⟨q0| + 4|q1⟩⟨q1|]
  Computed value: [[3. 2.]
 [2. 0.]]
  Expected value: [[3. 2.]
 [2. 0.]]
  check passed

▶ Step 2: Part 2: homogeneity R_A(c x) = R_A(x)
----------------------------------------

[R_A(-3.0 x) = R_A(x)]
  Computed value: -0.9954128440366973
  Expected value: -0.9954128440366972
  check passed

[R_A(0.5 x) = R_A(x)]
  Computed value: -0.9954128440366972
  Expected value: -0.9954128440366972
  check passed

[R_A(10.0 x) = R_A(x)]
  Computed value: -0.9954128440366973
  Expected value: -0.9954128440366972
  check passed

▶ Step 3: Part 3: scan the directions on the unit circle
----------------------------------------

[max R_A]
  Computed value: 3.999999999905701
  Expected value: 4
  check passed

[min R_A]
  Computed value: -0.9999999999057013
  Expected value: -1
  check passed

[direction of the maximum ∥ (2,1) (|det(u, (2,1))| < 1e-4, limited by the grid spacing)]
  Computed value: True
  Expected value: True
  check passed

[direction of the minimum ∥ (1,-2) (|det(u, (1,-2))| < 1e-4, limited by the grid spacing)]
  Computed value: True
  Expected value: True
  check passed

[R_A((2,1)) = 4]
  Computed value: 4.0
  Expected value: 4
  check passed

[R_A((1,-2)) = -1]
  Computed value: -1.0
  Expected value: -1
  check passed

▶ Step 4: Part 4: q_A = R_A on unit vectors
----------------------------------------

[q_A([0.8944 0.4472]) = R_A([0.8944 0.4472])]
  Computed value: 3.9999999999999996
  Expected value: 4.0
  check passed

[q_A([-0.8944 -0.4472]) = R_A([-0.8944 -0.4472])]
  Computed value: 3.9999999999999996
  Expected value: 4.0
  check passed

[q_A([ 0.4472 -0.8944]) = R_A([ 0.4472 -0.8944])]
  Computed value: -0.9999999999999999
  Expected value: -1.0
  check passed

[q_A([-0.4472  0.8944]) = R_A([-0.4472  0.8944])]
  Computed value: -0.9999999999999999
  Expected value: -1.0
  check passed

R_A of all 1000 random vectors lies in [-1, 4]: check passed

Complex Inner Product Spaces

We now turn to complex inner product spaces. Unless stated otherwise, the inner product on Cn\mathbb{C}^n is always the standard inner product ⟨z,w⟩=z∗w=⟨z∣w⟩⟨\mathbf{z}, \mathbf{w}⟩ = \mathbf{z}^*\mathbf{w} = ⟨\mathbf{z}|\mathbf{w}⟩, which is linear in the second argument and conjugate linear in the first (see Note).

============================================================
  Exercise 9-38 | Length of a Complex Vector
============================================================

▶ Step 1: Keep the real inner product formula x^T x
----------------------------------------

[x^T x]
  Computed value: 0j
  Expected value: 0
  check passed

▶ Step 2: Complex inner product x^* x
----------------------------------------

[x^* x]
  Computed value: (2+0j)
  Expected value: 2
  check passed

[||x||]
  Computed value: 1.4142135623730951
  Expected value: 1.4142135623730951
  check passed

▶ Step 3: Map C^n to R^{2n}: preserves length and addition; random check of the triangle inequality
----------------------------------------

[||z|| = ||phi(z)|| (1000 trials)]
  Computed value: True
  Expected value: True
  check passed

[phi(x+y) = phi(x)+phi(y) (1000 trials)]
  Computed value: True
  Expected value: True
  check passed

[Triangle inequality (1000 trials)]
  Computed value: True
  Expected value: True
  check passed
============================================================
  Exercise 9-39 | The Pythagorean Theorem in Complex Space
============================================================

▶ Step 1: Find k from <u,v> = (1-i)(1+k) = 0
----------------------------------------

[<u,v> = u^* v]
  Computed value: 0j
  Expected value: 0
  check passed

[k obtained by mistakenly using u^T v]
  Computed value: 1j
  Expected value: 1j
  check passed

[Then u^* v is not 0]
  Computed value: False
  Expected value: False
  check passed

▶ Step 2: Legs and length of the hypotenuse
----------------------------------------

[||u||^2]
  Computed value: 3.0
  Expected value: 3
  check passed

[||v||^2]
  Computed value: 3.0
  Expected value: 3
  check passed

[u - v]
  Computed value: [0.+1.j 2.+1.j]
  Expected value: [1j, (2+1j)]
  check passed

[||u - v||]
  Computed value: 2.449489742783178
  Expected value: 2.449489742783178
  check passed

[||u + v||]
  Computed value: 2.449489742783178
  Expected value: 2.449489742783178
  check passed

▶ Step 3: ||u-v||^2 = ||u||^2 + ||v||^2 - 2 Re<u,v> (random vectors)
----------------------------------------

[Expansion]
  Computed value: 12.190631774138552
  Expected value: 12.190631774138549
  check passed

[Counterexample to the converse: <u,v>]
  Computed value: 1j
  Expected value: 1j
  check passed

[Counterexample to the converse: ||u-v||^2]
  Computed value: 2.0000000000000004
  Expected value: 2
  check passed
============================================================
  Exercise 9-40 | Unitary Matrices of Order 2
============================================================

▶ Step 1: Unit vectors orthogonal to (1,0): (0, e^{i phi})
----------------------------------------

[phi=0.0000: (<e0,x>, ||x||)]
  Computed value: [np.complex128(0j), np.float64(1.0)]
  Expected value: [0, 1]
  check passed

[phi=1.5708: (<e0,x>, ||x||)]
  Computed value: [np.complex128(0j), np.float64(1.0)]
  Expected value: [0, 1]
  check passed

[phi=0.7854: (<e0,x>, ||x||)]
  Computed value: [np.complex128(0j), np.float64(1.0)]
  Expected value: [0, 1]
  check passed

[phi=2.0000: (<e0,x>, ||x||)]
  Computed value: [np.complex128(0j), np.float64(1.0)]
  Expected value: [0, 1]
  check passed

▶ Step 2: Unitary matrices of the form [[1,z],[0,w]]: z=0, |w|=1
----------------------------------------

[diag(1,e^{0.7i})^* diag(1,e^{0.7i})]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[Not unitary when z=0.1]
  Computed value: False
  Expected value: False
  check passed

▶ Step 3: Parametrization: every choice of z,w,theta gives a unitary matrix; every unitary matrix has this form
----------------------------------------

[U^* U]
  Computed value: [[ 1.+0.j -0.+0.j]
 [-0.+0.j  1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[Column 1 of the random unitary matrix = lambda (-conj(w), conj(z))]
  Computed value: [ 0.1259+0.2737j -0.9527+0.0398j]
  Expected value: [ 0.1259+0.2737j -0.9527+0.0398j]
  check passed

[Random unitary matrix = U(z,w,lambda)]
  Computed value: [[-0.7145-0.6315j  0.1259+0.2737j]
 [-0.2703+0.1331j -0.9527+0.0398j]]
  Expected value: [[-0.7145-0.6315j  0.1259+0.2737j]
 [-0.2703+0.1331j -0.9527+0.0398j]]
  check passed

▶ Step 4: Length preservation: ||U u|| = 1
----------------------------------------

[||U u||]
  Computed value: 0.9999999999999999
  Expected value: 1
  check passed

▶ Step 5: det(U) = lambda
----------------------------------------

[det(U)]
  Computed value: (0.33046510807172985+0.943818209374634j)
  Expected value: (0.33046510807172985+0.9438182093746337j)
  check passed

[det = 1 when lambda=1]
  Computed value: (1.0000000000000002+0j)
  Expected value: 1
  check passed

▶ Step 6: A real unitary matrix is orthogonal: lambda = ±1 gives rotations and reflections
----------------------------------------

[Rotation R^T R]
  Computed value: [[ 1. -0.]
 [-0.  1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[Reflection F^T F]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[(det R, det F)]
  Computed value: [np.float64(0.9999999999999999), np.float64(-0.9999999999999999)]
  Expected value: [1, -1]
  check passed
============================================================
  Exercise 9-41 | The Polarization Identity
============================================================

▶ Step 1: Real inner product space: <x,y> = (||x+y||^2 - ||x-y||^2)/4
----------------------------------------

[Real polarization]
  Computed value: -0.2789963481704243
  Expected value: -0.2789963481704246
  check passed

▶ Step 2: Counterexample x=(1,0), y=(i,0)
----------------------------------------

[<x,y> = x^* y]
  Computed value: 1j
  Expected value: 1j
  check passed

[Right-hand side of the real formula]
  Computed value: 0.0
  Expected value: 0
  check passed

▶ Step 3: Complex polarization identity under the convention of this book
----------------------------------------

[Counterexample vectors: formula of this book]
  Computed value: 1j
  Expected value: 1j
  check passed

[Counterexample vectors: formula of the original manuscript]
  Computed value: -1j
  Expected value: (-0-1j)
  check passed

[Formula of this book = <x,y> (200 trials)]
  Computed value: True
  Expected value: True
  check passed

[Formula of the original manuscript = <y,x> = conj<x,y> (200 trials)]
  Computed value: True
  Expected value: True
  check passed

▶ Step 4: ||x + c y||^2 = S + 2 Re(c <x,y>), c = 1, -1, i, -i
----------------------------------------

[c = 1]
  Computed value: 30.402869408160406
  Expected value: 30.402869408160402
  check passed

[c = -1]
  Computed value: 12.106362181738684
  Expected value: 12.106362181738685
  check passed

[c = 1j]
  Computed value: 6.678720945713077
  Expected value: 6.678720945713078
  check passed

[c = (-0-1j)]
  Computed value: 35.830510644186006
  Expected value: 35.830510644186006
  check passed
============================================================
  Exercise 9-42 | Complex Projections and the QR Decomposition
============================================================

▶ Step 1: P = u u^*: P^* = P, P^2 = P, Im ⟂ Ker, direct sum decomposition
----------------------------------------

[P^* = P]
  Computed value: [[ 0.4732+0.j      0.2698+0.2944j -0.0025-0.1824j  0.1097-0.211j ]
 [ 0.2698-0.2944j  0.337 +0.j     -0.1149-0.1024j -0.0687-0.1885j]
 [-0.0025+0.1824j -0.1149+0.1024j  0.0703+0.j      0.0807+0.0434j]
 [ 0.1097+0.211j  -0.0687+0.1885j  0.0807-0.0434j  0.1195-0.j    ]]
  Expected value: [[ 0.4732-0.j      0.2698+0.2944j -0.0025-0.1824j  0.1097-0.211j ]
 [ 0.2698-0.2944j  0.337 -0.j     -0.1149-0.1024j -0.0687-0.1885j]
 [-0.0025+0.1824j -0.1149+0.1024j  0.0703-0.j      0.0807+0.0434j]
 [ 0.1097+0.211j  -0.0687+0.1885j  0.0807-0.0434j  0.1195+0.j    ]]
  check passed

[P^2 = P]
  Computed value: [[ 0.4732+0.j      0.2698+0.2944j -0.0025-0.1824j  0.1097-0.211j ]
 [ 0.2698-0.2944j  0.337 +0.j     -0.1149-0.1024j -0.0687-0.1885j]
 [-0.0025+0.1824j -0.1149+0.1024j  0.0703+0.j      0.0807+0.0434j]
 [ 0.1097+0.211j  -0.0687+0.1885j  0.0807-0.0434j  0.1195+0.j    ]]
  Expected value: [[ 0.4732-0.j      0.2698+0.2944j -0.0025-0.1824j  0.1097-0.211j ]
 [ 0.2698-0.2944j  0.337 -0.j     -0.1149-0.1024j -0.0687-0.1885j]
 [-0.0025+0.1824j -0.1149+0.1024j  0.0703-0.j      0.0807+0.0434j]
 [ 0.1097+0.211j  -0.0687+0.1885j  0.0807-0.0434j  0.1195+0.j    ]]
  check passed

[P x = <u,x> u]
  Computed value: [-0.5829-0.3356j -0.5412+0.1713j  0.1325-0.2229j  0.0146-0.3377j]
  Expected value: [-0.5829-0.3356j -0.5412+0.1713j  0.1325-0.2229j  0.0146-0.3377j]
  check passed

[x - Px ∈ Ker(P)]
  Computed value: [ 0.+0.j  0.-0.j -0.+0.j  0.+0.j]
  Expected value: [0. 0. 0. 0.]
  check passed

[<Px, x - Px> = 0]
  Computed value: (-5.551115123125783e-17-2.220446049250313e-16j)
  Expected value: 0
  check passed

[rank(P)]
  Computed value: 1
  Expected value: 1
  check passed

▶ Step 2: Projection onto a k-dimensional subspace: B (B^*B)^{-1} B^* = Q Q^*
----------------------------------------

[B(B^*B)^{-1}B^* = Q Q^*]
  Computed value: [[ 0.5507+0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384-0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019-0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089+0.j    ]]
  Expected value: [[ 0.5507+0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384-0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019+0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089+0.j    ]]
  check passed

[P_W^* = P_W]
  Computed value: [[ 0.5507-0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384+0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019+0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089-0.j    ]]
  Expected value: [[ 0.5507+0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384-0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019-0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089+0.j    ]]
  check passed

[P_W^2 = P_W]
  Computed value: [[ 0.5507+0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384+0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019-0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089+0.j    ]]
  Expected value: [[ 0.5507+0.j     -0.0913+0.1265j  0.2078+0.3559j  0.0034-0.2308j]
 [-0.0913-0.1265j  0.2384-0.j      0.103 -0.2295j  0.2524-0.174j ]
 [ 0.2078-0.3559j  0.103 +0.2295j  0.4019-0.j      0.0736+0.0434j]
 [ 0.0034+0.2308j  0.2524+0.174j   0.0736-0.0434j  0.8089+0.j    ]]
  check passed

[P_W B = B (vectors in W are fixed)]
  Computed value: [[ 0.3688-0.4283j -0.9589-0.3521j]
 [ 0.8785+0.5323j -0.0499+0.3654j]
 [-0.1849+0.4127j -0.6809+0.4308j]
 [ 1.2225+2.1416j -0.1545-0.4064j]]
  Expected value: [[ 0.3688-0.4283j -0.9589-0.3521j]
 [ 0.8785+0.5323j -0.0499+0.3654j]
 [-0.1849+0.4127j -0.6809+0.4308j]
 [ 1.2225+2.1416j -0.1545-0.4064j]]
  check passed

[B^*(x - P_W x) = 0 (x - P_W x ∈ W^⟂)]
  Computed value: [-0.-0.j -0.+0.j]
  Expected value: [0. 0.]
  check passed

[P_W x is the best approximation (200 vectors in W)]
  Computed value: True
  Expected value: True
  check passed

▶ Step 3: Gram-Schmidt: v0 = (i,1), v1 = (0,i)
----------------------------------------

[r00]
  Computed value: 1.4142135623730951
  Expected value: 1.4142135623730951
  check passed

[r01 = <q0, v1>]
  Computed value: 0.7071067811865475j
  Expected value: 0.7071067811865475j
  check passed

[w1]
  Computed value: [0.5+0.j  0. +0.5j]
  Expected value: [0.5, 0.5j]
  check passed

[q1]
  Computed value: [0.7071+0.j     0.    +0.7071j]
  Expected value: [0.7071+0.j     0.    +0.7071j]
  check passed

[<q0, q1>]
  Computed value: 1.6653345369377348e-16j
  Expected value: 0
  check passed

▶ Step 4: A = Q R, where the diagonal of R is positive real
----------------------------------------

[Q]
  Computed value: [[0.    +0.7071j 0.7071+0.j    ]
 [0.7071+0.j     0.    +0.7071j]]
  Expected value: [[0.    +0.7071j 0.7071+0.j    ]
 [0.7071+0.j     0.    +0.7071j]]
  check passed

[R]
  Computed value: [[1.4142+0.j     0.    +0.7071j]
 [0.    +0.j     0.7071+0.j    ]]
  Expected value: [[np.float64(1.4142135623730951), 0.7071067811865475j], [0, np.float64(0.7071067811865475)]]
  check passed

[Q^* Q]
  Computed value: [[1.+0.j 0.+0.j]
 [0.-0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[Q R = A]
  Computed value: [[0.+1.j 0.+0.j]
 [1.+0.j 0.+1.j]]
  Expected value: [[0.+1.j 0.+0.j]
 [1.+0.j 0.+1.j]]
  check passed

[Q from numpy QR after phase adjustment]
  Computed value: [[ 0.    +0.7071j  0.7071-0.j    ]
 [ 0.7071+0.j     -0.    +0.7071j]]
  Expected value: [[0.    +0.7071j 0.7071+0.j    ]
 [0.7071+0.j     0.    +0.7071j]]
  check passed

[R from numpy QR after phase adjustment]
  Computed value: [[1.4142+0.j     0.    +0.7071j]
 [0.    +0.j     0.7071+0.j    ]]
  Expected value: [[1.4142+0.j     0.    +0.7071j]
 [0.    +0.j     0.7071+0.j    ]]
  check passed
============================================================
  Exercise 9-43 | Diagonalization of a Hermitian Matrix
============================================================

▶ Step 1: A^* = A
----------------------------------------

[A^*]
  Computed value: [[2.-0.j 1.-1.j]
 [1.+1.j 3.-0.j]]
  Expected value: [[2.+0.j 1.-1.j]
 [1.+1.j 3.+0.j]]
  check passed

▶ Step 2: Characteristic polynomial and eigenvalues
----------------------------------------

[Coefficients of the characteristic polynomial λ^2 - 5λ + 4]
  Computed value: [ 1.+0.j -5.+0.j  4.-0.j]
  Expected value: [1, -5, 4]
  check passed

[Eigenvalues (in increasing order)]
  Computed value: [1. 4.]
  Expected value: [1, 4]
  check passed

[Imaginary parts of the eigenvalues]
  Computed value: [-0.  0.]
  Expected value: [0, 0]
  check passed

▶ Step 3: Eigenvectors and orthogonality
----------------------------------------

[A v0 = 1·v0]
  Computed value: [-1.+1.j  1.+0.j]
  Expected value: [-1.+1.j  1.+0.j]
  check passed

[A v1 = 4·v1]
  Computed value: [4.-4.j 8.+0.j]
  Expected value: [4.-4.j 8.+0.j]
  check passed

[<v0, v1> = v0^* v1]
  Computed value: 0j
  Expected value: 0
  check passed

▶ Step 4: U^* A U = Λ
----------------------------------------

[(||v0||^2, ||v1||^2)]
  Computed value: [np.float64(3.0), np.float64(6.0)]
  Expected value: [3, 6]
  check passed

[U^* U]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[U^* A U]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 4.+0.j]]
  Expected value: [[1 0]
 [0 4]]
  check passed

[U Λ U^*]
  Computed value: [[2.-0.j 1.-1.j]
 [1.+1.j 3.+0.j]]
  Expected value: [[2.+0.j 1.-1.j]
 [1.+1.j 3.+0.j]]
  check passed
============================================================
  Exercise 9-44 | Spectral Decomposition of a Hermitian Matrix
============================================================

▶ Step 1: P_j = |u_j><u_j| and the spectral decomposition
----------------------------------------

[P0]
  Computed value: [[ 0.6667-0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  Expected value: [[ 0.6667+0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  check passed

[P1]
  Computed value: [[0.3333-0.j     0.3333-0.3333j]
 [0.3333+0.3333j 0.6667+0.j    ]]
  Expected value: [[0.3333+0.j     0.3333-0.3333j]
 [0.3333+0.3333j 0.6667+0.j    ]]
  check passed

[λ0 P0 + λ1 P1 = A]
  Computed value: [[2.-0.j 1.-1.j]
 [1.+1.j 3.+0.j]]
  Expected value: [[2.+0.j 1.-1.j]
 [1.+1.j 3.+0.j]]
  check passed

[Independent of the phase: (e^{iφ}u0)(e^{iφ}u0)^*]
  Computed value: [[ 0.6667+0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  Expected value: [[ 0.6667-0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  check passed

▶ Step 2: P0 + P1 = I, P0 P1 = 0, P_j^2 = P_j
----------------------------------------

[P0 + P1]
  Computed value: [[1.-0.j 0.+0.j]
 [0.+0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[P0 P1]
  Computed value: [[ 0.+0.j  0.+0.j]
 [-0.+0.j  0.+0.j]]
  Expected value: [[0. 0.]
 [0. 0.]]
  check passed

[P1 P0]
  Computed value: [[ 0.+0.j -0.+0.j]
 [ 0.+0.j  0.+0.j]]
  Expected value: [[0. 0.]
 [0. 0.]]
  check passed

[P0^2 = P0]
  Computed value: [[ 0.6667+0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  Expected value: [[ 0.6667-0.j     -0.3333+0.3333j]
 [-0.3333-0.3333j  0.3333+0.j    ]]
  check passed

[P1^2 = P1]
  Computed value: [[0.3333+0.j     0.3333-0.3333j]
 [0.3333+0.3333j 0.6667+0.j    ]]
  Expected value: [[0.3333-0.j     0.3333-0.3333j]
 [0.3333+0.3333j 0.6667+0.j    ]]
  check passed
============================================================
  Exercise 9-46 | Eigenvalues of Unitary Matrices
============================================================

▶ Step 1: Random unitary matrix: every eigenvalue has modulus 1
----------------------------------------

[U^* U = I]
  Computed value: [[ 1.+0.j  0.-0.j  0.+0.j  0.+0.j]
 [ 0.+0.j  1.+0.j -0.-0.j -0.-0.j]
 [ 0.-0.j -0.+0.j  1.+0.j -0.-0.j]
 [ 0.-0.j -0.+0.j -0.+0.j  1.+0.j]]
  Expected value: [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
  check passed
Eigenvalues of U: [ 0.0872+0.9962j  0.5241-0.8517j -0.4938-0.8695j -0.948 +0.3184j]

[|λ| (unitary matrix U)]
  Computed value: [1. 1. 1. 1.]
  Expected value: [1. 1. 1. 1.]
  check passed

▶ Step 2: Real orthogonal matrix (cyclic permutation): the complex eigenvalues also have modulus 1
----------------------------------------

[Q^T Q = I]
  Computed value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed
Eigenvalues of Q (the cube roots of 1): [-0.5+0.866j -0.5-0.866j  1. +0.j   ]

[|λ| (orthogonal matrix Q)]
  Computed value: [1. 1. 1.]
  Expected value: [1. 1. 1.]
  check passed

▶ Step 3: Eigenvalues e^{±iθ} of the rotation matrix R_θ (θ = π/3)
----------------------------------------

[Eigenvalues]
  Computed value: [0.5-0.866j 0.5+0.866j]
  Expected value: [np.complex128(0.5000000000000001-0.8660254037844386j), np.complex128(0.5000000000000001+0.8660254037844386j)]
  check passed
Discriminant 4cos²θ - 4 = -2.9999999999999996 < 0, so there are no real roots

▶ Step 4: Eigenvectors (1, -i), (1, i) in C^2 and unitary diagonalization
----------------------------------------

[R q₊ = e^{iθ} q₊]
  Computed value: [0.3536+0.6124j 0.6124-0.3536j]
  Expected value: [0.3536+0.6124j 0.6124-0.3536j]
  check passed

[R q₋ = e^{-iθ} q₋]
  Computed value: [0.3536-0.6124j 0.6124+0.3536j]
  Expected value: [0.3536-0.6124j 0.6124+0.3536j]
  check passed

[⟨q₊|q₋⟩ = 0]
  Computed value: 0j
  Expected value: 0
  check passed

[e^{iθ}|q₊⟩⟨q₊| + e^{-iθ}|q₋⟩⟨q₋| = R]
  Computed value: [[ 0.5  +0.j -0.866+0.j]
 [ 0.866+0.j  0.5  +0.j]]
  Expected value: [[ 0.5   -0.866]
 [ 0.866  0.5  ]]
  check passed

▶ Step 5: No real eigenvectors: for every real unit vector x, det(x, Rx) = sinθ ≠ 0
----------------------------------------

[Maximum error over 361 directions, max|det(x, Rx) - sinθ|]
  Computed value: 2.220446049250313e-16
  Expected value: 0
  check passed

[θ = 0.0000: R_θ = ±I (every nonzero vector is an eigenvector)]
  Computed value: [[ 1. -0.]
 [ 0.  1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[θ = 3.1416: R_θ = ±I (every nonzero vector is an eigenvector)]
  Computed value: [[-1. -0.]
 [ 0. -1.]]
  Expected value: [[-1. -0.]
 [-0. -1.]]
  check passed
============================================================
  Exercise 9-48 | True or False: Spectral Theory
============================================================

▶ Step 1: Part 1 (true): A = P D P^T is necessarily symmetric
----------------------------------------

[P^T P = I]
  Computed value: [[ 1. -0.  0.]
 [-0.  1.  0.]
 [ 0.  0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[A^T = A]
  Computed value: [[1.3399 1.9709 0.    ]
 [1.9709 0.6601 0.    ]
 [0.     0.     2.    ]]
  Expected value: [[1.3399 1.9709 0.    ]
 [1.9709 0.6601 0.    ]
 [0.     0.     2.    ]]
  check passed

▶ Step 2: Part 2 (false): the orthogonal matrix R_{π/2} is not symmetric and has no real eigenvalues
----------------------------------------

[A^T A = I (orthogonal)]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[A^T = -A (not symmetric)]
  Computed value: [[ 0.  1.]
 [-1.  0.]]
  Expected value: [[-0.  1.]
 [-1. -0.]]
  check passed

[Eigenvalues ±i (non-real, so not orthogonally diagonalizable)]
  Computed value: [0.-1.j 0.+1.j]
  Expected value: [(-0-1j), 1j]
  check passed

▶ Step 3: Part 3 (true): all eigenvalues positive ⟹ x^T A x > 0 for real vectors
----------------------------------------

[Eigenvalues]
  Computed value: [1. 2. 4.]
  Expected value: [1.0, 2.0, 4.0]
  check passed

[1000 random real vectors: x^T A x > 0]
  Computed value: True
  Expected value: True
  check passed

[x^T A x = Σ λ_i y_i²]
  Computed value: 5.417957870729893
  Expected value: 5.417957870729891
  check passed

[Reminder: transposing a complex vector without conjugating gives (1,i)^T I (1,i) = 0]
  Computed value: 0j
  Expected value: 0
  check passed

▶ Step 4: Part 4 (false): the eigenvalues of A^* = -A always have real part 0, but they can be 0 (counterexample O)
----------------------------------------

[A^* = -A]
  Computed value: [[ 0.-1.j -1.-2.j]
 [ 1.-2.j  0.-0.j]]
  Expected value: [[-0.-1.j -1.-2.j]
 [ 1.-2.j -0.-0.j]]
  check passed

[Eigenvalues]
  Computed value: [0.-1.7913j 0.+2.7913j]
  Expected value: [np.complex128(-0-1.79128784747792j), np.complex128(2.79128784747792j)]
  check passed

[Re λ = 0]
  Computed value: [0. 0.]
  Expected value: [0, 0]
  check passed

[Counterexample O: O^* = -O]
  Computed value: [[0. 0.]
 [0. 0.]]
  Expected value: [[-0. -0.]
 [-0. -0.]]
  check passed

[The eigenvalues of counterexample O are 0 (real part 0, but not purely imaginary)]
  Computed value: [0. 0.]
  Expected value: [0, 0]
  check passed

▶ Step 5: Part 5 (true): x^* y = 0 ⟹ the Pythagorean identity; the converse fails
----------------------------------------

[x^* y = ⟨x|y⟩]
  Computed value: 0j
  Expected value: 0
  check passed

[‖x+y‖² = ‖x‖² + ‖y‖²]
  Computed value: 4.0
  Expected value: 4.000000000000001
  check passed

[Converse direction: ‖x+y‖² = ‖x‖² + ‖y‖² = 2]
  Computed value: 2.0000000000000004
  Expected value: 2
  check passed

[but x^* y = i ≠ 0]
  Computed value: 1j
  Expected value: 1j
  check passed

▶ Step 6: Part 6 (false): all eigenvalues real, but not symmetric
----------------------------------------

[Eigenvalues (all real)]
  Computed value: [1. 2.]
  Expected value: [1.0, 2.0]
  check passed
A^T == A ? False

[Inner product of the eigenvectors (1,0), (1,1) ≠ 0]
  Computed value: 1
  Expected value: 1
  check passed

▶ Step 7: Part 7 (true): B = U^* A U is still Hermitian
----------------------------------------

[B^* = B]
  Computed value: [[-3.5778-0.j     -2.668 -0.7551j  0.9573-0.9689j]
 [-2.668 +0.7551j -2.9931-0.j     -0.313 +1.8387j]
 [ 0.9573+0.9689j -0.313 -1.8387j -0.3631-0.j    ]]
  Expected value: [[-3.5778+0.j     -2.668 -0.7551j  0.9573-0.9689j]
 [-2.668 +0.7551j -2.9931+0.j     -0.313 +1.8387j]
 [ 0.9573+0.9689j -0.313 -1.8387j -0.3631+0.j    ]]
  check passed

[B and A have the same eigenvalues]
  Computed value: [-6.1141 -2.6702  1.8503]
  Expected value: [-6.1141 -2.6702  1.8503]
  check passed
============================================================
  Exercise 9-49 | Complex Least Squares
============================================================

▶ Step 1: The system z h = w has no solution
----------------------------------------
h solved from the first and the second equation, respectively: [1.+2.j  1.+1.5j]

[h given by the two equations]
  Computed value: [1.+2.j  1.+1.5j]
  Expected value: [(1+2j), (1+1.5j)]
  check passed

▶ Step 2: Why z^*: z^T z = 0, but z^* z = 2
----------------------------------------

[z^T z (transpose only)]
  Computed value: 0j
  Expected value: 0
  check passed

[z^* z = ⟨z|z⟩]
  Computed value: (2+0j)
  Expected value: 2
  check passed

▶ Step 3: Normal equations z^* z ĥ = z^* w
----------------------------------------

[z^* w]
  Computed value: (2+3.5j)
  Expected value: (2+3.5j)
  check passed

[ĥ]
  Computed value: (1+1.75j)
  Expected value: (1+1.75j)
  check passed

[ĥ (np.linalg.lstsq)]
  Computed value: (0.9999999999999998+1.7499999999999996j)
  Expected value: (1+1.75j)
  check passed

▶ Step 4: Residual and least-squares error
----------------------------------------

[Residual r = w - z ĥ]
  Computed value: [0.  +0.25j 0.25+0.j  ]
  Expected value: [0.25j, 0.25]
  check passed

[z^* r = 0 (the residual is orthogonal to z)]
  Computed value: 0j
  Expected value: 0
  check passed

[‖w - z ĥ‖²]
  Computed value: 0.12500000000000003
  Expected value: 0.125
  check passed

[‖w‖² - |z^* w|² / ‖z‖²]
  Computed value: 0.12500000000000178
  Expected value: 0.125
  check passed

Testing and Factoring Positive Definite Matrices

============================================================
  Exercise 9-50 | Positive Definite Matrix of Order 2
============================================================

▶ Step 1: Complete the square: x^T A x = (2x_0 + x_1)^2 + 4x_1^2
----------------------------------------

[max |quadratic form - completed square| (1000 random vectors)]
  Computed value: 1.4210854715202004e-14
  Expected value: 0
  check passed

[minimum of the quadratic form > 0]
  Computed value: True
  Expected value: True
  check passed

▶ Step 2: Eigenvalues (9 ∓ √17)/2
----------------------------------------

[eigenvalues λ_0, λ_1]
  Computed value: [2.4384 6.5616]
  Expected value: [np.float64(2.4384471871911697), np.float64(6.561552812808831)]
  check passed

[all eigenvalues positive]
  Computed value: True
  Expected value: True
  check passed

▶ Step 3: Q = [[2, 1], [0, 2]] with A = Q^T Q
----------------------------------------

[Q^T Q]
  Computed value: [[4. 2.]
 [2. 5.]]
  Expected value: [[4. 2.]
 [2. 5.]]
  check passed

[det(Q)]
  Computed value: 4.0
  Expected value: 4.0
  check passed

[Q equals the transpose of the numpy Cholesky factor]
  Computed value: [[2. 1.]
 [0. 2.]]
  Expected value: [[2. 1.]
 [0. 2.]]
  check passed

[(Q')^T Q']
  Computed value: [[4. 2.]
 [2. 5.]]
  Expected value: [[4. 2.]
 [2. 5.]]
  check passed
============================================================
  Exercise 9-52 | Leading Principal Minor Test
============================================================

▶ Step 1: Leading principal minors of A and the sum-of-squares form
----------------------------------------

[(D_1, D_2, D_3) of A]
  Computed value: [2. 3. 4.]
  Expected value: [2, 3, 4]
  check passed

[max |x^T A x - sum of squares| (1000 random vectors)]
  Computed value: 7.105427357601002e-15
  Expected value: 0
  check passed

[eigenvalues of A]
  Computed value: [0.5858 2.     3.4142]
  Expected value: [np.float64(0.5857864376269049), 2, np.float64(3.414213562373095)]
  check passed

▶ Step 2: Leading principal minors of B
----------------------------------------

[(D_1, D_2, D_3) of B]
  Computed value: [ 1. -2.  8.]
  Expected value: [1, -2, 8]
  check passed

▶ Step 3: Counterexample x = (-2, 1, 0)^T
----------------------------------------

[B x]
  Computed value: [ 0. -2. -2.]
  Expected value: [0, -2, -2]
  check passed

[x^T B x]
  Computed value: -2.0
  Expected value: -2
  check passed
============================================================
  Exercise 9-53 | A Pitfall in Testing Semidefiniteness
============================================================

▶ Step 1: Counterexample A = diag(0, -1)
----------------------------------------

[(D_1, D_2)]
  Computed value: [np.float64(0.0), np.float64(0.0)]
  Expected value: [0, 0]
  check passed

[x^T A x, x = (0, 1)^T]
  Computed value: -1.0
  Expected value: -1
  check passed

[eigenvalues of A]
  Computed value: [-1.  0.]
  Expected value: [-1, 0]
  check passed

[original conditions a≥0, b≥0, det≥0 hold]
  Computed value: True
  Expected value: True
  check passed

▶ Step 2: Corrected statement: symmetric, a≥0, d≥0, det≥0 ⇒ positive semidefinite (random test)
----------------------------------------

[smallest eigenvalue over 2000 samples ≥ -1e-12]
  Computed value: True
  Expected value: True
  check passed

▶ Step 3: Symmetry cannot be dropped: C = [[1, 0], [-4, 1]]
----------------------------------------

[det(C)]
  Computed value: 1.0
  Expected value: 1
  check passed

[x^T C x, x = (1, 1)^T]
  Computed value: -2.0
  Expected value: -2
  check passed

[symmetric part (C + C^T)/2]
  Computed value: [[ 1. -2.]
 [-2.  1.]]
  Expected value: [[1, -2], [-2, 1]]
  check passed
============================================================

  Exercise 9-54 | Computing Cholesky Decompositions
============================================================

▶ Step 1: Decompose A
----------------------------------------

[L_A]
  Computed value: [[ 1. -3.]
 [ 0.  2.]]
  Expected value: [[1, -3], [0, 2]]
  check passed

[L_A^T L_A]
  Computed value: [[ 1. -3.]
 [-3. 13.]]
  Expected value: [[ 1. -3.]
 [-3. 13.]]
  check passed

▶ Step 2: Decompose B
----------------------------------------

[L_B]
  Computed value: [[ 2. -1.  2.]
 [ 0.  3. -1.]
 [ 0.  0.  2.]]
  Expected value: [[2, -1, 2], [0, 3, -1], [0, 0, 2]]
  check passed

[L_B^T L_B]
  Computed value: [[ 4. -2.  4.]
 [-2. 10. -5.]
 [ 4. -5.  9.]]
  Expected value: [[ 4. -2.  4.]
 [-2. 10. -5.]
 [ 4. -5.  9.]]
  check passed

▶ Step 3: Compare with numpy (numpy gives a lower triangular G with A = G G^T, so L = G^T)
----------------------------------------

[L_A = cholesky(A)^T]
  Computed value: [[ 1. -3.]
 [ 0.  2.]]
  Expected value: [[ 1. -3.]
 [ 0.  2.]]
  check passed

[L_B = cholesky(B)^T]
  Computed value: [[ 2. -1.  2.]
 [ 0.  3. -1.]
 [ 0.  0.  2.]]
  Expected value: [[ 2. -1.  2.]
 [ 0.  3. -1.]
 [ 0.  0.  2.]]
  check passed
============================================================
  Exercise 9-55 | Cholesky Diagonal Entries and Leading Principal Minors
============================================================

▶ Step 1: Random positive definite matrix: L_kk = sqrt(D_{k+1} / D_k), D_k = Π L_ii^2
----------------------------------------

[diagonal entries of L]
  Computed value: [3.2684 3.4051 2.2012 2.0533 1.7037 1.0058]
  Expected value: [3.2684 3.4051 2.2012 2.0533 1.7037 1.0058]
  check passed

[D_k = Π_{i<k} L_ii^2 (k = 0..6)]
  Computed value: [   1.       10.6827  123.864   600.1598 2530.1867 7344.1015 7430.1299]
  Expected value: [   1.       10.6827  123.864   600.1598 2530.1867 7344.1015 7430.1299]
  check passed

▶ Step 2: Redo A: D = (1, 1, 4)
----------------------------------------

[(D_0, D_1, D_2)]
  Computed value: [1. 1. 4.]
  Expected value: [1, 1, 4]
  check passed

[(L_00, L_11)]
  Computed value: [1. 2.]
  Expected value: [1, 2]
  check passed

[L_A]
  Computed value: [[ 1. -3.]
 [ 0.  2.]]
  Expected value: [[1, -3], [0, 2]]
  check passed

[L_A^T L_A]
  Computed value: [[ 1. -3.]
 [-3. 13.]]
  Expected value: [[ 1. -3.]
 [-3. 13.]]
  check passed

▶ Step 3: Redo B: D = (1, 4, 36, 144)
----------------------------------------

[(D_0, D_1, D_2, D_3)]
  Computed value: [  1.   4.  36. 144.]
  Expected value: [1, 4, 36, 144]
  check passed

[(L_00, L_11, L_22)]
  Computed value: [2. 3. 2.]
  Expected value: [2, 3, 2]
  check passed

[L_B]
  Computed value: [[ 2. -1.  2.]
 [ 0.  3. -1.]
 [ 0.  0.  2.]]
  Expected value: [[2, -1, 2], [0, 3, -1], [0, 0, 2]]
  check passed

[L_B^T L_B]
  Computed value: [[ 4. -2.  4.]
 [-2. 10. -5.]
 [ 4. -5.  9.]]
  Expected value: [[ 4. -2.  4.]
 [-2. 10. -5.]
 [ 4. -5.  9.]]
  check passed

[det(L_B) = sqrt(D_3)]
  Computed value: 12.0
  Expected value: 12
  check passed
============================================================
  Exercise 9-56 | Complex Cholesky Decomposition
============================================================

▶ Step 1: Hermitian property and leading principal minors
----------------------------------------

[A^* = A]
  Computed value: [[2.-0.j 1.-1.j]
 [1.+1.j 2.-0.j]]
  Expected value: [[2.+0.j 1.-1.j]
 [1.+1.j 2.+0.j]]
  check passed

[(D_1, D_2)]
  Computed value: [2.+0.j 2.+0.j]
  Expected value: [2, 2]
  check passed

[eigenvalues 2 ∓ √2]
  Computed value: [0.5858 3.4142]
  Expected value: [np.float64(0.5857864376269049), np.float64(3.414213562373095)]
  check passed

▶ Step 2: Upper triangular L with A = L^* L
----------------------------------------

[L^* L]
  Computed value: [[2.+0.j 1.-1.j]
 [1.+1.j 2.+0.j]]
  Expected value: [[2.+0.j 1.-1.j]
 [1.+1.j 2.+0.j]]
  check passed

[L_11 = sqrt(D_2 / D_1)]
  Computed value: (1+0j)
  Expected value: 1.0
  check passed

[L = cholesky(A)^*]
  Computed value: [[1.4142+0.j     0.7071-0.7071j]
 [0.    +0.j     1.    +0.j    ]]
  Expected value: [[1.4142-0.j     0.7071-0.7071j]
 [0.    -0.j     1.    -0.j    ]]
  check passed

▶ Step 3: x^* A x = ||L x||^2 (random complex vector)
----------------------------------------

[x^* A x]
  Computed value: 9.552299241830477
  Expected value: 9.552299241830479
  check passed

Bilinear Forms and Quadratic Forms

============================================================
  Exercise 9-57 | Matrix Representation of a Quadratic Form
============================================================

▶ Step 1: Real quadratic form: compare coefficients to get the symmetric matrix A
----------------------------------------

[A is symmetric]
  Computed value: [[3. 2.]
 [2. 5.]]
  Expected value: [[3. 2.]
 [2. 5.]]
  check passed

[x^T A x agrees with q(x)]
  Computed value: 2.0012286123012757
  Expected value: 2.0012286123012757
  check passed

[x^T A x agrees with q(x)]
  Computed value: 24.48958840003455
  Expected value: 24.48958840003455
  check passed

[x^T A x agrees with q(x)]
  Computed value: 0.975927973372148
  Expected value: 0.9759279733721479
  check passed

▶ Step 2: Hermitian quadratic form: compare coefficients to get B
----------------------------------------

[B is a Hermitian matrix (B^* = B)]
  Computed value: [[2.+0.j 1.+1.j]
 [1.-1.j 4.+0.j]]
  Expected value: [[2.-0.j 1.+1.j]
 [1.-1.j 4.-0.j]]
  check passed

[z^* B z agrees with q(z)]
  Computed value: (9.143377095140135+0j)
  Expected value: (9.143377095140135+0j)
  check passed

[Imaginary part of q(z) is 0]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[z^* B z agrees with q(z)]
  Computed value: (9.177125045551456+0j)
  Expected value: (9.177125045551456+0j)
  check passed

[Imaginary part of q(z) is 0]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[z^* B z agrees with q(z)]
  Computed value: (6.6839606658787325+0j)
  Expected value: (6.683960665878732+0j)
  check passed

[Imaginary part of q(z) is 0]
  Computed value: 0.0
  Expected value: 0.0
  check passed
============================================================
  Exercise 9-58 | Trace under Similarity and Congruence
============================================================

▶ Step 1: Linear operator: similarity transformation B = P^{-1} A P
----------------------------------------

[P^{-1}]
  Computed value: [[ 1.  -0.5]
 [ 0.   0.5]]
  Expected value: [[1, -0.5], [0, 0.5]]
  check passed

[B]
  Computed value: [[0. 3.]
 [1. 2.]]
  Expected value: [[0, 3], [1, 2]]
  check passed

[tr(B) = tr(A) = 2]
  Computed value: [np.float64(2.0), np.float64(2.0)]
  Expected value: [2, 2]
  check passed

[Eigenvalues of B and A (in increasing order)]
  Computed value: [array([-1.,  3.]), array([-1.,  3.])]
  Expected value: [[-1, 3], [-1, 3]]
  check passed

▶ Step 2: Quadratic form: congruence transformation C = P^T A P
----------------------------------------

[C]
  Computed value: [[ 1.  5.]
 [ 5. 13.]]
  Expected value: [[1, 5], [5, 13]]
  check passed

[tr(C) = 14, not equal to tr(A) = 2]
  Computed value: 14.0
  Expected value: 14
  check passed

[q_A(Py) = y^T C y]
  Computed value: 15.209999999999997
  Expected value: 15.209999999999997
  check passed

▶ Step 3: Quantities preserved by congruence: signs of the eigenvalues and of the determinant
----------------------------------------

[Eigenvalues of C: 7 ± sqrt(61)]
  Computed value: [-0.8102 14.8102]
  Expected value: [np.float64(-0.810249675906654), np.float64(14.810249675906654)]
  check passed

[Numbers of positive and negative eigenvalues of C and A: (1, 1)]
  Computed value: [np.int64(1), np.int64(1)]
  Expected value: [1, 1]
  check passed

[det C = det A · (det P)^2 = -12]
  Computed value: -11.999999999999995
  Expected value: -11.999999999999998
  check passed
============================================================
  Exercise 9-60 | Congruence Classification of 2×2 Symmetric Matrices
============================================================

▶ Step 1: Take a representative of each class and construct P so that P^T A P is the canonical form
----------------------------------------

[Positive definite: P^T A P]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1 0]
 [0 1]]
  check passed

[Negative definite: P^T A P]
  Computed value: [[-1.  0.]
 [ 0. -1.]]
  Expected value: [[-1  0]
 [ 0 -1]]
  check passed

[Positive semidefinite (one positive, one zero): P^T A P]
  Computed value: [[1. 0.]
 [0. 0.]]
  Expected value: [[1 0]
 [0 0]]
  check passed

[Negative semidefinite (one negative, one zero): P^T A P]
  Computed value: [[-1.  0.]
 [ 0.  0.]]
  Expected value: [[-1  0]
 [ 0  0]]
  check passed

[Indefinite: P^T A P]
  Computed value: [[ 1.  0.]
 [ 0. -1.]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

[Zero matrix: P^T A P]
  Computed value: [[0. 0.]
 [0. 0.]]
  Expected value: [[0 0]
 [0 0]]
  check passed

▶ Step 2: Independent classification: predict the canonical form from the signs of det A and tr A alone, then compare with the constructed result
----------------------------------------

[P^T A P agrees with the det/tr classification]
  Computed value: [[-1.  0.]
 [ 0. -1.]]
  Expected value: [[-1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[ 1.  0.]
 [-0. -1.]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[ 1. -0.]
 [-0. -1.]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[ 1.  0.]
 [ 0. -1.]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[-1.  0.]
 [-0. -1.]]
  Expected value: [[-1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[ 1. -0.]
 [-0. -1.]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[1. 0.]
 [0. 0.]]
  Expected value: [[1 0]
 [0 0]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[-1. -0.]
 [ 0.  0.]]
  Expected value: [[-1  0]
 [ 0  0]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1 0]
 [0 1]]
  check passed

[P^T A P agrees with the det/tr classification]
  Computed value: [[-1.  0.]
 [ 0. -1.]]
  Expected value: [[-1  0]
 [ 0 -1]]
  check passed
============================================================
  Exercise 9-62 | Eliminating Cross Terms (Real Quadratic Forms)
============================================================

▶ Step 1: Two variables: orthogonal matrix Q and Q^T A Q
----------------------------------------

[Q^T Q = I]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[det Q = 1 (rotation)]
  Computed value: 0.9999999999999998
  Expected value: 1.0
  check passed

[Q^T A Q = diag(3, 1)]
  Computed value: [[3. 0.]
 [0. 1.]]
  Expected value: [[3 0]
 [0 1]]
  check passed

▶ Step 2: Substitute x = Q y: q(x) = 3 y0^2 + y1^2
----------------------------------------

[q(x) vs. 3y0^2 + y1^2]
  Computed value: 5.310109708859807
  Expected value: 5.310109708859809
  check passed

[q(x) vs. 3y0^2 + y1^2]
  Computed value: 0.842607582274886
  Expected value: 0.8426075822748862
  check passed

[q(x) vs. 3y0^2 + y1^2]
  Computed value: 2.611367428461757
  Expected value: 2.611367428461758
  check passed

▶ Step 3: General n variables: Q^T = Q^{-1} makes congruence coincide with similarity
----------------------------------------

[Q^T = Q^{-1}]
  Computed value: [[ 0.2592  0.6353 -0.543   0.4841]
 [ 0.7283  0.3574  0.5083 -0.2887]
 [-0.0738  0.2177 -0.5161 -0.8251]
 [ 0.63   -0.6491 -0.4247  0.038 ]]
  Expected value: [[ 0.2592  0.6353 -0.543   0.4841]
 [ 0.7283  0.3574  0.5083 -0.2887]
 [-0.0738  0.2177 -0.5161 -0.8251]
 [ 0.63   -0.6491 -0.4247  0.038 ]]
  check passed

[Q^T A Q = Λ]
  Computed value: [[-2.4894 -0.      0.     -0.    ]
 [-0.     -1.5035 -0.     -0.    ]
 [ 0.     -0.      1.4873  0.    ]
 [-0.     -0.      0.      1.7897]]
  Expected value: [[-2.4894  0.      0.      0.    ]
 [ 0.     -1.5035  0.      0.    ]
 [ 0.      0.      1.4873  0.    ]
 [ 0.      0.      0.      1.7897]]
  check passed

[x^T A x = Σ λ_i <q_i|x>^2]
  Computed value: -4.917191772937468
  Expected value: -4.917191772937469
  check passed
============================================================
  Exercise 9-63 | Eliminating Cross Terms (Hermitian Quadratic Forms)
============================================================

▶ Step 1: H is a Hermitian matrix with eigenvalues ±1
----------------------------------------

[H^* = H]
  Computed value: [[ 0.-0.j -0.+1.j]
 [ 0.-1.j  0.-0.j]]
  Expected value: [[ 0.+0.j  0.+1.j]
 [-0.-1.j  0.+0.j]]
  check passed

[Eigenvalues (in increasing order)]
  Computed value: [-1.  1.]
  Expected value: [-1, 1]
  check passed

▶ Step 2: Unitary matrix U and U^* H U
----------------------------------------

[U^* U = I]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[U^* H U = diag(1, -1)]
  Computed value: [[ 1.+0.j  0.+0.j]
 [ 0.+0.j -1.+0.j]]
  Expected value: [[ 1  0]
 [ 0 -1]]
  check passed

▶ Step 3: Random z: q(z) = |w0|^2 - |w1|^2, w = U^* z
----------------------------------------

[q(z) vs. |w0|^2 - |w1|^2]
  Computed value: (-7.586351917374334+0j)
  Expected value: -7.586351917374331
  check passed

[q(z) vs. |w0|^2 - |w1|^2]
  Computed value: (2.5036005711435116+0j)
  Expected value: 2.5036005711435116
  check passed

[q(z) vs. |w0|^2 - |w1|^2]
  Computed value: (1.2903091506469337+0j)
  Expected value: 1.290309150646931
  check passed

▶ Step 4: General n: U^* H U = Λ (real diagonal)
----------------------------------------

[U^* H U = Λ]
  Computed value: [[-1.4976+0.j -0.    +0.j  0.    +0.j]
 [-0.    -0.j  1.3924-0.j  0.    +0.j]
 [ 0.    +0.j  0.    -0.j  2.3218+0.j]]
  Expected value: [[-1.4976  0.      0.    ]
 [ 0.      1.3924  0.    ]
 [ 0.      0.      2.3218]]
  check passed

[z^* H z = Σ λ_i |<u_i|z>|^2]
  Computed value: (3.02819130959171+0j)
  Expected value: 3.0281913095917092
  check passed

Applications of the Principal Axis Theorem: Conics, Quadric Surfaces, and Data Science

============================================================
  Exercise 9-64 | Principal Axes of a Conic
============================================================

▶ Step 1: Matrix form
----------------------------------------

[x^T A x vs. 5x^2 - 4xy + 8y^2]
  Computed value: 16.01
  Expected value: 16.009999999999998
  check passed

▶ Step 2: Orthogonal diagonalization: P^T A P = diag(4, 9)
----------------------------------------

[Eigenvalues]
  Computed value: [4. 9.]
  Expected value: [4, 9]
  check passed

[P^T P = I]
  Computed value: [[1. 0.]
 [0. 1.]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[det P = 1]
  Computed value: 1.0
  Expected value: 1.0
  check passed

[P^T A P]
  Computed value: [[ 4. -0.]
 [ 0.  9.]]
  Expected value: [[4 0]
 [0 9]]
  check passed

[Rotation angle (degrees)]
  Computed value: 26.56505117707799
  Expected value: 26.56505117707799
  check passed

▶ Step 3: Semi-axis lengths and a check of the endpoints
----------------------------------------

[Semi-major axis, semi-minor axis]
  Computed value: [3. 2.]
  Expected value: [3, 2]
  check passed

[Length of the major axis, length of the minor axis]
  Computed value: [6. 4.]
  Expected value: [6, 4]
  check passed

[Endpoint lies on the curve]
  Computed value: 36.0
  Expected value: 36
  check passed

[Endpoint lies on the curve]
  Computed value: 36.0
  Expected value: 36
  check passed

[Endpoint lies on the curve]
  Computed value: 36.0
  Expected value: 36
  check passed

[Endpoint lies on the curve]
  Computed value: 36.0
  Expected value: 36
  check passed
============================================================
  Exercise 9-65 | Classification of a Quadric Surface
============================================================

▶ Step 1: x^T A x = 2(xy + yz + zx)
----------------------------------------

[x^T A x]
  Computed value: -2.7104428224938504
  Expected value: -2.7104428224938504
  check passed

▶ Step 2: Eigenvalues and type of surface
----------------------------------------

[Eigenvalues (in increasing order)]
  Computed value: [-1. -1.  2.]
  Expected value: [-1, -1, 2]
  check passed

[tr A, det A]
  Computed value: [np.float64(0.0), np.float64(2.0)]
  Expected value: [0, 2]
  check passed

[Numbers of positive and negative eigenvalues of (1/2)A: (1, 2) → hyperboloid of two sheets]
  Computed value: [np.int64(1), np.int64(2)]
  Expected value: [1, 2]
  check passed

[Vertex (1,1,1)/sqrt(3) lies on the surface]
  Computed value: 1.0000000000000002
  Expected value: 1
  check passed

▶ Step 3: Rotation matrix P and the canonical form
----------------------------------------

[p2 = (1,1,-2)/sqrt(6)]
  Computed value: [ 0.4082  0.4082 -0.8165]
  Expected value: [ 0.4082  0.4082 -0.8165]
  check passed

[P^T P = I]
  Computed value: [[ 1. -0. -0.]
 [-0.  1.  0.]
 [-0.  0.  1.]]
  Expected value: [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
  check passed

[det P = 1]
  Computed value: 1.0
  Expected value: 1.0
  check passed

[P^T A P = diag(2, -1, -1)]
  Computed value: [[ 2.  0. -0.]
 [ 0. -1.  0.]
 [ 0.  0. -1.]]
  Expected value: [[ 2  0  0]
 [ 0 -1  0]
 [ 0  0 -1]]
  check passed

[Point constructed from x' satisfies xy+yz+zx = 1]
  Computed value: 1.0000000000000004
  Expected value: 1
  check passed

[Point constructed from x' satisfies xy+yz+zx = 1]
  Computed value: 1.0
  Expected value: 1
  check passed

[Point constructed from x' satisfies xy+yz+zx = 1]
  Computed value: 1.000000000000001
  Expected value: 1
  check passed
============================================================
  Exercise 9-66 | Covariance Matrix and Principal Component Analysis
============================================================

▶ Step 1: Random centered data: Ω = A^T A is symmetric positive semidefinite
----------------------------------------

[Ω = A^T A]
  Computed value: [[0.9292 0.3621]
 [0.3621 1.1168]]
  Expected value: [[0.9292 0.3621]
 [0.3621 1.1168]]
  check passed

[Ω is symmetric]
  Computed value: [[0.9292 0.3621]
 [0.3621 1.1168]]
  Expected value: [[0.9292 0.3621]
 [0.3621 1.1168]]
  check passed

[a, b linearly independent → Ω positive definite (all eigenvalues positive)]
  Computed value: True
  Expected value: True
  check passed

[When b = 2a, det Ω = 0]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[When b = 2a, u = (2,-1)/sqrt(5) gives u^T Ω u = 0]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[Centered data with n = 2: det Ω = 0 (cannot be positive definite)]
  Computed value: -2.223753220512612e-18
  Expected value: 0.0
  check passed

▶ Step 2: Projected data have mean 0 and variance u^T Ω u
----------------------------------------

[Mean]
  Computed value: 2.886579864025407e-17
  Expected value: 0.0
  check passed

[Variance]
  Computed value: 1.2299369638017024
  Expected value: 1.2299369638017024
  check passed

▶ Step 3: Eigenvalues and eigenvectors of Ω = [[4,2],[2,7]]
----------------------------------------

[Eigenvalues (in increasing order)]
  Computed value: [3. 8.]
  Expected value: [3, 8]
  check passed

[W q0 = 8 q0]
  Computed value: [3.5777 7.1554]
  Expected value: [3.5777 7.1554]
  check passed

[W q1 = 3 q1]
  Computed value: [ 2.6833 -1.3416]
  Expected value: [ 2.6833 -1.3416]
  check passed

▶ Step 4: Scan u^T Ω u over the unit circle: maximum 8, minimum 3
----------------------------------------

[Maximum variance]
  Computed value: 7.999999999905701
  Expected value: 8
  check passed

[Minimum variance]
  Computed value: 3.000000000094298
  Expected value: 3
  check passed

[Direction of maximum variance = ±(1,2)/sqrt(5)]
  Computed value: 0.9999999999905701
  Expected value: 1.0
  check passed

[Direction of minimum variance = ±(2,-1)/sqrt(5)]
  Computed value: 0.9999999999905701
  Expected value: 1.0
  check passed

[Total variance = tr Ω]
  Computed value: 11
  Expected value: 11.0
  check passed
============================================================
  Exercise 9-67 | Energy of a Quantum System
============================================================

▶ Step 1: Eigenvalues and eigenvectors
----------------------------------------

[H^* = H]
  Computed value: [[ 5.-0.j -0.+2.j]
 [ 0.-2.j  2.-0.j]]
  Expected value: [[ 5.+0.j  0.+2.j]
 [-0.-2.j  2.+0.j]]
  check passed

[Eigenvalues (in increasing order)]
  Computed value: [1. 6.]
  Expected value: [1, 6]
  check passed

[H u0 = 1·u0]
  Computed value: [0.4472+0.j     0.    +0.8944j]
  Expected value: [0.4472+0.j     0.    +0.8944j]
  check passed

[H u1 = 6·u1]
  Computed value: [0.    +5.3666j 2.6833+0.j    ]
  Expected value: [0.    +5.3666j 2.6833+0.j    ]
  check passed

▶ Step 2: Unitary matrix U and the canonical form
----------------------------------------

[<u0|u1> = 0]
  Computed value: 0j
  Expected value: 0
  check passed

[U^* U = I]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 1.+0.j]]
  Expected value: [[1. 0.]
 [0. 1.]]
  check passed

[U^* H U = diag(1, 6)]
  Computed value: [[1.+0.j 0.+0.j]
 [0.+0.j 6.+0.j]]
  Expected value: [[1 0]
 [0 6]]
  check passed

[H = |u0><u0| + 6|u1><u1|]
  Computed value: [[5.+0.j 0.+2.j]
 [0.-2.j 2.+0.j]]
  Expected value: [[ 5.+0.j  0.+2.j]
 [-0.-2.j  2.+0.j]]
  check passed

▶ Step 3: Random states: h(z) is real and equals |w0|^2 + 6|w1|^2
----------------------------------------

[Imaginary part of h(z)]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[h(z) vs. |w0|^2 + 6|w1|^2]
  Computed value: 1.1195074675943746
  Expected value: 1.1195074675943748
  check passed

[h(z) vs. 5|z0|^2 + 2|z1|^2 - 4 Im(conj(z0) z1)]
  Computed value: 1.1195074675943746
  Expected value: 1.1195074675943748
  check passed

[Energy of a unit state lies in [1, 6]]
  Computed value: True
  Expected value: True
  check passed

[Imaginary part of h(z)]
  Computed value: -8.881784197001252e-16
  Expected value: 0.0
  check passed

[h(z) vs. |w0|^2 + 6|w1|^2]
  Computed value: 11.890370344271124
  Expected value: 11.890370344271124
  check passed

[h(z) vs. 5|z0|^2 + 2|z1|^2 - 4 Im(conj(z0) z1)]
  Computed value: 11.890370344271124
  Expected value: 11.890370344271123
  check passed

[Energy of a unit state lies in [1, 6]]
  Computed value: True
  Expected value: True
  check passed

[Imaginary part of h(z)]
  Computed value: 0.0
  Expected value: 0.0
  check passed

[h(z) vs. |w0|^2 + 6|w1|^2]
  Computed value: 6.673983950416356
  Expected value: 6.673983950416355
  check passed

[h(z) vs. 5|z0|^2 + 2|z1|^2 - 4 Im(conj(z0) z1)]
  Computed value: 6.673983950416356
  Expected value: 6.673983950416355
  check passed

[Energy of a unit state lies in [1, 6]]
  Computed value: True
  Expected value: True
  check passed

The Jacobi Method

The Jacobi method is one of the oldest numerical algorithms for finding the eigenvalues of a real symmetric matrix. It was proposed by Carl Gustav Jacob Jacobi in 1846, more than a century before the modern QR algorithm.

Its central idea comes from a simple observation: the eigenvalues of a diagonal matrix can be read off at a glance—they are the entries on the diagonal. So if a matrix can be “reduced” step by step to diagonal form by a sequence of similarity transformations that leave the eigenvalues unchanged, the problem is solved.

Recall the spectral theorem of Chapter 9: a real symmetric matrix A\mathbf{A} is always orthogonally diagonalizable, that is, there exists an orthogonal matrix Q\mathbf{Q} such that

Q⊤A Q=Λ\mathbf{Q}^\top \mathbf{A}\, \mathbf{Q} = \mathbf{\Lambda}

The Jacobi method approximates this Q\mathbf{Q} constructively—at each step it chooses a plane rotation (a Givens rotation) Gij\mathbf{G}_{ij} that eliminates one off-diagonal entry aija_{ij} exactly, while guaranteeing that the overall “off-diagonal energy”

off(A)2=∑i≠jaij2\text{off}(\mathbf{A})^2 = \sum_{i \neq j} a_{ij}^2

decreases strictly monotonically. Although after one entry is eliminated, other entries in the same row and column may be disturbed and no longer be zero, we will see in the exercise below that each rotation reduces off2\text{off}^2 by exactly 2aij22a_{ij}^2; this “conservation of energy” viewpoint is the key to analyzing the convergence of the algorithm.


✓ All exercises completed.