public void SquareMatrixSVD() { for (int d = 4; d < 50; d += 7) { SquareMatrix A = CreateSquareRandomMatrix(d, d); SingularValueDecomposition SVD = A.SingularValueDecomposition(); Assert.IsTrue(SVD.Dimension == A.Dimension); ColumnVector x = new ColumnVector(d); for (int i = 0; i < d; i++) { x[i] = i; } ColumnVector b = A * x; ColumnVector b1 = SVD.LeftTransformMatrix().Transpose() * b; for (int i = 0; i < SVD.Rank; i++) { b1[i] = b1[i] / SVD.SingularValue(i); } ColumnVector b2 = SVD.RightTransformMatrix() * b1; ColumnVector y = SVD.Solve(b); Assert.IsTrue(TestUtilities.IsNearlyEqual(x, y)); } }
public void HilbertMatrixSVD() { int n = 4; SquareMatrix H = new SquareMatrix(n); for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { H[r, c] = 1.0 / (r + c + 1); } } SingularValueDecomposition SVD = H.SingularValueDecomposition(); for (int i = 0; i < n; i++) { Console.WriteLine(SVD.SingularValue(i)); } SquareMatrix S = SVD.LeftTransformMatrix().Transpose() * H * SVD.RightTransformMatrix(); for (int i = 0; i < SVD.Dimension; i++) { Console.WriteLine(S[i, i]); Assert.IsTrue(TestUtilities.IsNearlyEqual(S[i, i], SVD.SingularValue(i))); } }
public void SmallSVD() { SquareMatrix A0 = new SquareMatrix(1); A0[0, 0] = 0.0; SingularValueDecomposition SVD0 = A0.SingularValueDecomposition(); Console.WriteLine(SVD0.SingularValue(0)); Assert.IsTrue(SVD0.SingularValue(0) == 0.0); SquareMatrix A1 = new SquareMatrix(1); A1[0, 0] = 1.0; SingularValueDecomposition SVD1 = A1.SingularValueDecomposition(); Console.WriteLine(SVD1.SingularValue(0)); //Assert.IsTrue(SVD1.SingularValue(0) == 1.0); SquareMatrix A2 = new SquareMatrix(2); A2[0, 0] = 0.0; A2[0, 1] = 1.0; A2[1, 0] = 0.0; A2[1, 1] = 1.0; // Singular values Sqrt(2), 0 SingularValueDecomposition SVD2 = A2.SingularValueDecomposition(); SquareMatrix S2 = SVD2.LeftTransformMatrix().Transpose() * A2 * SVD2.RightTransformMatrix(); for (int i = 0; i < SVD2.Dimension; i++) { Console.WriteLine("{0} {1}", S2[i, i], SVD2.SingularValue(i)); Assert.IsTrue(TestUtilities.IsNearlyEqual(S2[i, i], SVD2.SingularValue(i))); } }
public void RandomRectangularSVD() { for (int c = 1; c < 64; c += 11) { Console.WriteLine(c); RectangularMatrix R = GenerateRandomMatrix(64, c); SingularValueDecomposition SVD = R.SingularValueDecomposition(); Assert.IsTrue(SVD.RowCount == R.RowCount); Assert.IsTrue(SVD.ColumnCount == SVD.ColumnCount); Assert.IsTrue(SVD.Dimension == SVD.ColumnCount); SquareMatrix U = SVD.LeftTransformMatrix(); Assert.IsTrue(U.Dimension == R.RowCount); Assert.IsTrue(TestUtilities.IsNearlyEqual(U.Transpose() * U, TestUtilities.CreateSquareUnitMatrix(U.Dimension))); SquareMatrix V = SVD.RightTransformMatrix(); Assert.IsTrue(V.Dimension == R.ColumnCount); Assert.IsTrue(TestUtilities.IsNearlyEqual(V.Transpose() * V, TestUtilities.CreateSquareUnitMatrix(V.Dimension))); RectangularMatrix S = U.Transpose() * R * V; for (int i = 0; i < SVD.Dimension; i++) { double w = SVD.SingularValue(i); Console.WriteLine(" {0} {1}", w, S[i, i]); Assert.IsTrue(w >= 0.0); Assert.IsTrue(TestUtilities.IsNearlyEqual(S[i, i], w)); Assert.IsTrue(TestUtilities.IsNearlyEqual(R * SVD.RightSingularVector(i), w * SVD.LeftSingularVector(i))); } } }
public void PC() { Random rng = new Random(1); double s = 1.0 / Math.Sqrt(2.0); MultivariateSample MS = new MultivariateSample(2); RectangularMatrix R = new RectangularMatrix(1000, 2); for (int i = 0; i < 1000; i++) { double r1 = 2.0 * rng.NextDouble() - 1.0; double r2 = 2.0 * rng.NextDouble() - 1.0; double x = r1 * 4.0 * s - r2 * 9.0 * s; double y = r1 * 4.0 * s + r2 * 9.0 * s; R[i, 0] = x; R[i, 1] = y; MS.Add(x, y); } Console.WriteLine("x {0} {1}", MS.Column(0).Mean, MS.Column(0).Variance); Console.WriteLine("y {0} {1}", MS.Column(1).Mean, MS.Column(1).Variance); Console.WriteLine("SVD"); SingularValueDecomposition SVD = R.SingularValueDecomposition(); for (int i = 0; i < SVD.Dimension; i++) { Console.WriteLine("{0} {1}", i, SVD.SingularValue(i)); ColumnVector v = SVD.RightSingularVector(i); Console.WriteLine(" {0} {1}", v[0], v[1]); } Console.WriteLine("PCA"); PrincipalComponentAnalysis PCA = MS.PrincipalComponentAnalysis(); Console.WriteLine("Dimension = {0} Count = {1}", PCA.Dimension, PCA.Count); for (int i = 0; i < PCA.Dimension; i++) { PrincipalComponent PC = PCA.Component(i); Console.WriteLine(" {0} {1} {2} {3}", PC.Index, PC.Weight, PC.VarianceFraction, PC.CumulativeVarianceFraction); RowVector v = PC.NormalizedVector(); Console.WriteLine(" {0} {1}", v[0], v[1]); } // reconstruct SquareMatrix U = SVD.LeftTransformMatrix(); SquareMatrix V = SVD.RightTransformMatrix(); double x1 = U[0, 0] * SVD.SingularValue(0) * V[0, 0] + U[0, 1] * SVD.SingularValue(1) * V[0, 1]; Console.WriteLine("x1 = {0} {1}", x1, R[0, 0]); double y1 = U[0, 0] * SVD.SingularValue(0) * V[1, 0] + U[0, 1] * SVD.SingularValue(1) * V[1, 1]; Console.WriteLine("y1 = {0} {1}", y1, R[0, 1]); double x100 = U[100, 0] * SVD.SingularValue(0) * V[0, 0] + U[100, 1] * SVD.SingularValue(1) * V[0, 1]; Console.WriteLine("x100 = {0} {1}", x100, R[100, 0]); double y100 = U[100, 0] * SVD.SingularValue(0) * V[1, 0] + U[100, 1] * SVD.SingularValue(1) * V[1, 1]; Console.WriteLine("y100 = {0} {1}", y100, R[100, 1]); ColumnVector d1 = U[0, 0] * SVD.SingularValue(0) * SVD.RightSingularVector(0) + U[0, 1] * SVD.SingularValue(1) * SVD.RightSingularVector(1); Console.WriteLine("d1 = ({0} {1})", d1[0], d1[1]); ColumnVector d100 = U[100, 0] * SVD.SingularValue(0) * SVD.RightSingularVector(0) + U[100, 1] * SVD.SingularValue(1) * SVD.RightSingularVector(1); Console.WriteLine("d100 = ({0} {1})", d100[0], d100[1]); Console.WriteLine("compare"); MultivariateSample RS = PCA.TransformedSample(); IEnumerator <double[]> RSE = RS.GetEnumerator(); RSE.MoveNext(); double[] dv1 = RSE.Current; Console.WriteLine("{0} {1}", dv1[0], dv1[1]); Console.WriteLine("{0} {1}", U[0, 0], U[0, 1]); RSE.Dispose(); }