Accord.Tests.Math.JaggedSingularValueDecompositionTest.JaggedSingularValueDecompositionConstructorTest1 C# (CSharp) Method

JaggedSingularValueDecompositionConstructorTest1() private method

private JaggedSingularValueDecompositionConstructorTest1 ( ) : void
return void
        public void JaggedSingularValueDecompositionConstructorTest1()
        {
            // This test catches the bug in SingularValueDecomposition in the line
            //   for (int j = k + 1; j < nu; j++)
            // where it should be
            //   for (int j = k + 1; j < n; j++)


            // Test for m-x-n matrices where m < n. The available SVD
            // routine was not meant to be used in this case.

            var value = new double[][]
            { 
                 new double[] { 1, 2 },
                 new double[] { 3, 4 },
                 new double[] { 5, 6 },
                 new double[] { 7, 8 }
            }.Transpose(); // value is 2x4, having less rows than columns.

            var target = new JaggedSingularValueDecomposition(value, true, true, false);

            double[][] actual = Matrix.Multiply(Matrix.Multiply(
                target.LeftSingularVectors, target.DiagonalMatrix),
                target.RightSingularVectors.Transpose());

            // Checking the decomposition
            Assert.IsTrue(Matrix.IsEqual(actual, value, 1e-2));
            Assert.IsTrue(Matrix.IsEqual(value, target.Reverse(), 1e-2));

            // Checking values
            var U = new double[][]
            {
                new double[] { -0.641423027995072, -0.767187395072177 },
                new double[] { -0.767187395072177,  0.641423027995072 },
            };

            // U should be equal
            Assert.IsTrue(Matrix.IsEqual(target.LeftSingularVectors, U, 0.001));


            double[][] V = new double[][]// economy svd
            {
                new double[] { -0.152483233310201,  0.822647472225661,  },
                new double[] { -0.349918371807964,  0.421375287684580,  },
                new double[] { -0.547353510305727,  0.0201031031435023, },
                new double[] { -0.744788648803490, -0.381169081397574,  },
            };

            // V can be different, but for the economy SVD it is often equal
            Assert.IsTrue(Matrix.IsEqual(target.RightSingularVectors.Submatrix(0, 3, 0, 1), V, 0.0001));


            double[][] S = 
            {
                new double[] { 14.2690954992615, 0.000000000000000 },
                new double[] {  0.0000000000000,	0.626828232417543 },
            };

            // The diagonal values should be equal
            Assert.IsTrue(Matrix.IsEqual(target.Diagonal.First(2), Matrix.Diagonal(S), 0.001));
        }