public void TestRandomA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                // Create A.
                MatrixF a = new MatrixF(3, 3);
                RandomHelper.Random.NextMatrixF(a, 0, 1);

                LUDecompositionF d = new LUDecompositionF(a);

                if (d.IsNumericallySingular == false)
                {
                    // Check solving of linear equations.
                    MatrixF b = new MatrixF(3, 2);
                    RandomHelper.Random.NextMatrixF(b, 0, 1);

                    MatrixF x  = d.SolveLinearEquations(b);
                    MatrixF b2 = a * x;
                    Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f));

                    MatrixF aPermuted = d.L * d.U;
                    Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
                }
            }
        }
        public void TestMatricesWithoutFullRank()
        {
            MatrixF          a  = new MatrixF(3, 3);
            QRDecompositionF qr = new QRDecompositionF(a);

            Assert.AreEqual(false, qr.HasNumericallyFullRank);

            a = new MatrixF(new float[, ] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 4, 5, 6 }
            });
            qr = new QRDecompositionF(a);
            Assert.AreEqual(false, qr.HasNumericallyFullRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, qr.Q * qr.R));
            qr = new QRDecompositionF(a.Transposed);
            Assert.AreEqual(false, qr.HasNumericallyFullRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a.Transposed, qr.Q * qr.R));

            a = new MatrixF(new float[, ] {
                { 1, 2 }, { 1, 2 }, { 1, 2 }
            });
            qr = new QRDecompositionF(a);
            Assert.AreEqual(false, qr.HasNumericallyFullRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, qr.Q * qr.R));

            MatrixF h = qr.H; // Just call this one to see if it runs through.
        }
        public void TestRandomSquareA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                // Create A.
                MatrixF a = new MatrixF(3, 3);
                RandomHelper.Random.NextMatrixF(a, 0, 1);

                QRDecompositionF d = new QRDecompositionF(a);
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R));
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R)); // Second time with the cached values.

                // Check solving of linear equations.
                if (d.HasNumericallyFullRank)
                {
                    MatrixF b = new MatrixF(3, 2);
                    RandomHelper.Random.NextMatrixF(b, 0, 1);
                    MatrixF x  = d.SolveLinearEquations(b);
                    MatrixF b2 = a * x;
                    Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f));
                }

                MatrixF h = d.H; // Just call this one to see if it runs through.
                h = d.H;         // Call it secont time to cover code with internal caching.
            }
        }
        public void TestRandomRectangularA()
        {
            RandomHelper.Random = new Random(1);

            // Every transpose(A) * A is SPD if A has full column rank and m>n.
            for (int i = 0; i < 100; i++)
            {
                // Create A.
                MatrixF a = new MatrixF(10, 3);
                RandomHelper.Random.NextMatrixF(a, 0, 1);

                QRDecompositionF d = new QRDecompositionF(a);
                MatrixF          b = new MatrixF(10, 1);
                RandomHelper.Random.NextMatrixF(b, 0, 1);
                MatrixF x = d.SolveLinearEquations(b);
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R));
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R)); // Second time with the cached values.


                // Check solving of linear equations.
                if (d.HasNumericallyFullRank)
                {
                    // Compare with Least squares solution (Gauss-Transformation and Cholesky).
                    MatrixF spdMatrix         = a.Transposed * a;
                    CholeskyDecompositionF ch = new CholeskyDecompositionF(spdMatrix);
                    MatrixF x2 = ch.SolveLinearEquations(a.Transposed * b);

                    Assert.IsTrue(MatrixF.AreNumericallyEqual(x, x2, 0.0001f));
                }

                MatrixF h = d.H; // Just call this one to see if it runs through.
            }
        }
Example #5
0
        public void TestMatricesWithFullRank()
        {
            MatrixF a = new MatrixF(new float[, ] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 }
            });
            SingularValueDecompositionF svd = new SingularValueDecompositionF(a);

            Assert.AreEqual(3, svd.NumericalRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            float condNumber = svd.ConditionNumber;

            svd = new SingularValueDecompositionF(a.Transposed);
            Assert.AreEqual(3, svd.NumericalRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a.Transposed, svd.U * svd.S * svd.V.Transposed));
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            condNumber = svd.ConditionNumber;

            a = new MatrixF(new float[, ] {
                { 1, 2 }, { 4, 5 }, { 4, 5 }
            });
            svd = new SingularValueDecompositionF(a);
            Assert.AreEqual(2, svd.NumericalRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            condNumber = svd.ConditionNumber;
        }
Example #6
0
        public void TestRandomRegularA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                VectorF column1 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column1, 1, 2);
                VectorF column2 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column2, 1, 2);

                // Make linearly independent.
                if (column1 / column1[0] == column2 / column2[0])
                {
                    column2[0]++;
                }

                // Create linearly independent third column.
                VectorF column3 = column1 + column2;
                column3[1]++;

                // Create A.
                MatrixF a = new MatrixF(3, 3);
                a.SetColumn(0, column1);
                a.SetColumn(1, column2);
                a.SetColumn(2, column3);

                SingularValueDecompositionF svd = new SingularValueDecompositionF(a);
                Assert.AreEqual(3, svd.NumericalRank);
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
                Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
                float condNumber = svd.ConditionNumber;
            }
        }
Example #7
0
        public void TestRandomRectangularA()
        {
            RandomHelper.Random = new Random(1);

            // Every transpose(A) * A is SPD if A has full column rank and m>n.
            for (int i = 0; i < 100; i++)
            {
                // Create A.
                MatrixF a = new MatrixF(4, 3);
                RandomHelper.Random.NextMatrixF(a, 0, 1);

                // Check for full rank with QRD.
                QRDecompositionF d = new QRDecompositionF(a);

                SingularValueDecompositionF svd = new SingularValueDecompositionF(a);
                if (d.HasNumericallyFullRank)
                {
                    // Rank should be full.
                    Assert.AreEqual(3, svd.NumericalRank);
                    Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
                }
                else
                {
                    // Not full rank - we dont know much, just see if it runs through
                    Assert.Greater(3, svd.NumericalRank);
                    Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
                }
                Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
                float condNumber = svd.ConditionNumber;
            }
        }
Example #8
0
        public void TestMatricesWithoutFullRank()
        {
            MatrixF a = new MatrixF(3, 3);
            SingularValueDecompositionF svd = new SingularValueDecompositionF(a);

            Assert.AreEqual(0, svd.NumericalRank);
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            float condNumber = svd.ConditionNumber;

            a = new MatrixF(new float[, ] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 4, 5, 6 }
            });
            svd = new SingularValueDecompositionF(a);
            Assert.AreEqual(2, svd.NumericalRank);
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
            svd = new SingularValueDecompositionF(a.Transposed);
            Assert.AreEqual(2, svd.NumericalRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a.Transposed, svd.U * svd.S * svd.V.Transposed));
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a.Transposed, svd.U * svd.S * svd.V.Transposed)); // Repeat to test with cached values.
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            condNumber = svd.ConditionNumber;

            a = new MatrixF(new float[, ] {
                { 1, 2 }, { 1, 2 }, { 1, 2 }
            });
            svd = new SingularValueDecompositionF(a);
            Assert.AreEqual(1, svd.NumericalRank);
            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, svd.U * svd.S * svd.V.Transposed));
            Assert.AreEqual(svd.SingularValues[0], svd.Norm2);
            condNumber = svd.ConditionNumber;
        }
        public void Test2()
        {
            // Every transpose(A)*A is SPD if A has full column rank and m > n.
            MatrixF a = new MatrixF(new float[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
                { 10, 11, -12 }
            });

            //MatrixF a = new MatrixF(new float[,] { { 1, 2},
            //                                       { 4, 5},
            //                                       { 7, 8}});

            a = a.Transposed * a;

            CholeskyDecompositionF d = new CholeskyDecompositionF(a);

            Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

            MatrixF l = d.L;

            Assert.AreEqual(0, l[0, 1]);
            Assert.AreEqual(0, l[0, 2]);
            Assert.AreEqual(0, l[1, 2]);

            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, l * l.Transposed));
        }
        public void Test1()
        {
            MatrixF a = new MatrixF(new float[, ] {
                { 2, -1, 0 },
                { -1, 2, -1 },
                { 0, -1, 2 }
            });

            CholeskyDecompositionF d = new CholeskyDecompositionF(a);

            Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

            MatrixF l = d.L;

            Assert.AreEqual(0, l[0, 1]);
            Assert.AreEqual(0, l[0, 2]);
            Assert.AreEqual(0, l[1, 2]);

            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, l * l.Transposed));

            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, l * l.Transposed));

            // Check solving of linear equations.
            MatrixF x = new MatrixF(new float[, ] {
                { 1, 2 },
                { 3, 4 },
                { 5, 6 }
            });
            MatrixF b = a * x;

            Assert.IsTrue(MatrixF.AreNumericallyEqual(x, d.SolveLinearEquations(b)));
        }
        public void TestRandomSpdA()
        {
            RandomHelper.Random = new Random(1);

            // Every transpose(A) * A is SPD if A has full column rank and m>n.
            for (int i = 0; i < 100; i++)
            {
                VectorF column1 = new VectorF(4);
                RandomHelper.Random.NextVectorF(column1, 1, 2);
                VectorF column2 = new VectorF(4);
                RandomHelper.Random.NextVectorF(column2, 1, 2);

                // Make linearly independent.
                if (column1 / column1[0] == column2 / column2[0])
                {
                    column2[0]++;
                }

                // Create linearly independent third column.
                VectorF column3 = column1 + column2;
                column3[1]++;

                // Create A.
                MatrixF a = new MatrixF(4, 3);
                a.SetColumn(0, column1);
                a.SetColumn(1, column2);
                a.SetColumn(2, column3);

                MatrixF spdMatrix = a.Transposed * a;

                CholeskyDecompositionF d = new CholeskyDecompositionF(spdMatrix);

                Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

                MatrixF l = d.L;

                // Test if L is a lower triangular matrix.
                for (int j = 0; j < l.NumberOfRows; j++)
                {
                    for (int k = 0; k < l.NumberOfColumns; k++)
                    {
                        if (j < k)
                        {
                            Assert.AreEqual(0, l[j, k]);
                        }
                    }
                }

                Assert.IsTrue(MatrixF.AreNumericallyEqual(spdMatrix, l * l.Transposed));

                // Check solving of linear equations.
                MatrixF b = new MatrixF(3, 2);
                RandomHelper.Random.NextMatrixF(b, 0, 1);

                MatrixF x  = d.SolveLinearEquations(b);
                MatrixF b2 = spdMatrix * x;
                Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f));
            }
        }
        public void Test2()
        {
            MatrixF a = new MatrixF(new float[, ] {
                { 0, 1, 2 },
                { 1, 4, 3 },
                { 2, 3, 5 }
            });
            EigenvalueDecompositionF d = new EigenvalueDecompositionF(a);

            Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.V * d.D * d.V.Transposed));
        }
        public void Test1()
        {
            MatrixF a = new MatrixF(new float[, ] {
                { 1, -1, 4 },
                { 3, 2, -1 },
                { 2, 1, -1 }
            });
            EigenvalueDecompositionF d = new EigenvalueDecompositionF(a);

            Assert.IsTrue(MatrixF.AreNumericallyEqual(a * d.V, d.V * d.D));
        }
        public void Determinant()
        {
            MatrixF a = new MatrixF(new float[, ] {
                { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 0, 1, 2, 0 }, { 1, 0, 1, 0 }
            });

            LUDecompositionF d = new LUDecompositionF(a);

            Assert.AreEqual(false, d.IsNumericallySingular);
            Assert.IsTrue(Numeric.AreEqual(-24, d.Determinant()));

            MatrixF aPermuted = d.L * d.U;

            Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 3)));
        }
        public void TestRandomRegularA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                VectorF column1 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column1, 1, 2);

                VectorF column2 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column2, 1, 2);

                // Make linearly independent.
                if (column1 / column1[0] == column2 / column2[0])
                {
                    column2[0]++;
                }

                // Create linearly independent third column.
                VectorF column3 = column1 + column2;
                column3[1]++;

                // Create A.
                MatrixF a = new MatrixF(3, 3);
                a.SetColumn(0, column1);
                a.SetColumn(1, column2);
                a.SetColumn(2, column3);

                LUDecompositionF d = new LUDecompositionF(a);

                MatrixF aPermuted = d.L * d.U;
                Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));
                aPermuted = d.L * d.U; // Repeat with to test cached values.
                Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2)));

                Assert.AreEqual(false, d.IsNumericallySingular);

                // Check solving of linear equations.
                MatrixF b = new MatrixF(3, 2);
                RandomHelper.Random.NextMatrixF(b, 0, 1);

                MatrixF x  = d.SolveLinearEquations(b);
                MatrixF b2 = a * x;
                Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f));
            }
        }
        public void TestRandomRegularA()
        {
            RandomHelper.Random = new Random(1);

            for (int i = 0; i < 100; i++)
            {
                VectorF column1 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column1, 1, 2);
                VectorF column2 = new VectorF(3);
                RandomHelper.Random.NextVectorF(column2, 1, 2);

                // Make linearly independent.
                if (column1 / column1[0] == column2 / column2[0])
                {
                    column2[0]++;
                }

                // Create linearly independent third column.
                VectorF column3 = column1 + column2;
                column3[1]++;

                // Create A.
                MatrixF a = new MatrixF(3, 3);
                a.SetColumn(0, column1);
                a.SetColumn(1, column2);
                a.SetColumn(2, column3);

                QRDecompositionF d = new QRDecompositionF(a);
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R));
                Assert.IsTrue(MatrixF.AreNumericallyEqual(a, d.Q * d.R)); // Second time with the cached values.
                Assert.AreEqual(true, d.HasNumericallyFullRank);

                // Check solving of linear equations.
                MatrixF b = new MatrixF(3, 2);
                RandomHelper.Random.NextMatrixF(b, 0, 1);

                MatrixF x  = d.SolveLinearEquations(b);
                MatrixF b2 = a * x;
                Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f));

                MatrixF h = d.H; // Just call this one to see if it runs through.
                h = d.H;         // Call it secont time to cover code with internal caching.
            }
        }