public void SquareRandomMatrixQRDecomposition() { for (int d = 1; d <= 100; d += 11) { Console.WriteLine("d={0}", d); SquareMatrix M = CreateSquareRandomMatrix(d); // QR decompose the matrix. SquareQRDecomposition QRD = M.QRDecomposition(); // The dimension should be right. Assert.IsTrue(QRD.Dimension == M.Dimension); // Test that the decomposition works. SquareMatrix Q = QRD.QMatrix; SquareMatrix R = QRD.RMatrix; Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * R, M)); // Check that the inverse works. SquareMatrix MI = QRD.Inverse(); Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, UnitMatrix.OfDimension(d))); // Test that a solution works. ColumnVector t = new ColumnVector(d); for (int i = 0; i < d; i++) { t[i] = i; } ColumnVector s = QRD.Solve(t); Assert.IsTrue(TestUtilities.IsNearlyEqual(M * s, t)); } }
public void CirculantEigenvalues() { // See https://en.wikipedia.org/wiki/Circulant_matrix // Definition is C_{ij} = c_{|i - j|} for any n-vector c. // jth eigenvector known to be (1, \omega_j, \omega_j^2, \cdots, \omega_j^{n-1}) where \omega_j = \exp(2 \pi i j / n) are nth roots of unity // jth eigenvalue known to be c_0 + c_{n-1} \omega_j + c_{n-2} \omega_j^2 + \cdots + c_1 \omega_j^{n-1} int n = 12; double[] x = new double[n]; for (int i = 0; i < x.Length; i++) { x[i] = 1.0 / (i + 1); } SquareMatrix A = CreateCirculantMatrix(x); Complex[] u = RootsOfUnity(n); Complex[] v = new Complex[n]; for (int i = 0; i < v.Length; i++) { for (int j = 0; j < n; j++) { v[i] += x[j] * u[(i * (n - j)) % n]; } } Complex[][] w = new Complex[n][]; for (int i = 0; i < n; i++) { w[i] = new Complex[n]; for (int j = 0; j < n; j++) { w[i][j] = u[i * j % n]; } } Complex[] eValues = A.Eigenvalues(); Complex eProduct = 1.0; foreach (Complex eValue in eValues) { eProduct *= eValue; } // v and eValues should be equal. By inspection they are, // but how to verify this given floating point jitter? // Verify that eigenvalue product equals determinant. SquareQRDecomposition QR = A.QRDecomposition(); double det = QR.Determinant(); Assert.IsTrue(TestUtilities.IsNearlyEqual(eProduct, det)); }
public void CirculantEigenvalues() { int n = 12; double[] x = new double[n]; for (int i = 0; i < x.Length; i++) { x[i] = 1.0 / (i + 1); } SquareMatrix A = CreateCirculantMatrix(x); Complex[] u = RootsOfUnity(n); Complex[] v = new Complex[n]; for (int i = 0; i < v.Length; i++) { for (int j = 0; j < n; j++) { v[i] += x[j] * u[(i * (n - j)) % n]; } } Complex[][] w = new Complex[n][]; for (int i = 0; i < n; i++) { w[i] = new Complex[n]; for (int j = 0; j < n; j++) { w[i][j] = u[i * j % n]; } } Complex[] eValues = A.Eigenvalues(); Complex eProduct = 1.0; foreach (Complex eValue in eValues) { eProduct *= eValue; } // v and eValues should be equal. By inspection they are, // but how to verify this given floating point jitter? // Verify that eigenvalue product equals determinant. SquareQRDecomposition QR = A.QRDecomposition(); double det = QR.Determinant(); Assert.IsTrue(TestUtilities.IsNearlyEqual(eProduct, det)); }