Exemple #1
0
        public void Test2()
        {
            // Every transpose(A)*A is SPD if A has full column rank and m > n.
            MatrixD a = new MatrixD(new double[, ] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
                { 10, 11, -12 }
            });

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

            a = a.Transposed * a;

            CholeskyDecompositionD d = new CholeskyDecompositionD(a);

            Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

            MatrixD l = d.L;

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

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

              CholeskyDecompositionD d = new CholeskyDecompositionD(a);

              Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

              MatrixD l = d.L;

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

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

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

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

              Assert.IsTrue(MatrixD.AreNumericallyEqual(x, d.SolveLinearEquations(b)));
        }
Exemple #3
0
        public void Test1()
        {
            MatrixD a = new MatrixD(new double[, ] {
                { 2, -1, 0 },
                { -1, 2, -1 },
                { 0, -1, 2 }
            });

            CholeskyDecompositionD d = new CholeskyDecompositionD(a);

            Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

            MatrixD l = d.L;

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

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

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

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

            Assert.IsTrue(MatrixD.AreNumericallyEqual(x, d.SolveLinearEquations(b)));
        }
        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.
                MatrixD a = new MatrixD(10, 3);
                RandomHelper.Random.NextMatrixD(a, 0, 1);

                QRDecompositionD d = new QRDecompositionD(a);
                MatrixD          b = new MatrixD(10, 1);
                RandomHelper.Random.NextMatrixD(b, 0, 1);
                MatrixD x = d.SolveLinearEquations(b);
                Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R));
                Assert.IsTrue(MatrixD.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).
                    MatrixD spdMatrix         = a.Transposed * a;
                    CholeskyDecompositionD ch = new CholeskyDecompositionD(spdMatrix);
                    MatrixD x2 = ch.SolveLinearEquations(a.Transposed * b);

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

                MatrixD h = d.H; // Just call this one to see if it runs through.
            }
        }
Exemple #5
0
        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++)
            {
                VectorD column1 = new VectorD(4);
                RandomHelper.Random.NextVectorD(column1, 1, 2);
                VectorD column2 = new VectorD(4);
                RandomHelper.Random.NextVectorD(column2, 1, 2);

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

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

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

                MatrixD spdMatrix = a.Transposed * a;

                CholeskyDecompositionD d = new CholeskyDecompositionD(spdMatrix);

                Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

                MatrixD 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(MatrixD.AreNumericallyEqual(spdMatrix, l * l.Transposed));

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

                MatrixD x  = d.SolveLinearEquations(b);
                MatrixD b2 = spdMatrix * x;
                Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f));
            }
        }
        public void SolveLinearEquationsException1()
        {
            // Create A.
              MatrixD a = new MatrixD(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 } });

              CholeskyDecompositionD decomp = new CholeskyDecompositionD(a);
              decomp.SolveLinearEquations(null);
        }
Exemple #7
0
        public void TestEmptyMatrix()
        {
            MatrixD a = new MatrixD(2, 2);

            CholeskyDecompositionD d = new CholeskyDecompositionD(a);

            Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
Exemple #8
0
        public void SolveLinearEquationsException1()
        {
            // Create A.
            MatrixD a = new MatrixD(new double[, ] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 }
            });

            CholeskyDecompositionD decomp = new CholeskyDecompositionD(a);

            decomp.SolveLinearEquations(null);
        }
Exemple #9
0
        public void TestRectangularA()
        {
            MatrixD a = new MatrixD(new double[, ] {
                { 2, -1 },
                { -1, 2 },
                { 0, -1 }
            });

            CholeskyDecompositionD d = new CholeskyDecompositionD(a);

            Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
Exemple #10
0
        public void TestNonSpdA()
        {
            MatrixD a = new MatrixD(new double[, ] {
                { 1, 2, 3 },
                { 2, 5, 6 },
                { 3, 6, 4 }
            });

            CholeskyDecompositionD d = new CholeskyDecompositionD(a);

            Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
Exemple #11
0
        public void TestWithNaNValues()
        {
            MatrixD a = new MatrixD(new[, ] {
                { 0, 1, 2 },
                { 1, 4, 3 },
                { 5, 3, double.NaN }
            });

            // Any result is ok. We must only check for infinite loops!
            var d = new CholeskyDecompositionD(a);

            d = new CholeskyDecompositionD(new MatrixD(4, 4, double.NaN));
        }
        public void TestNonSymmetricA()
        {
            MatrixD a = new MatrixD(new double[,] { { 1, 2, 3},
                                             { 2, 2, 2},
                                             { 0, 0, 0} });

              CholeskyDecompositionD d = new CholeskyDecompositionD(a);
              Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
        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.
            MatrixD a = new MatrixD(10, 3);
            RandomHelper.Random.NextMatrixD(a, 0, 1);

            QRDecompositionD d = new QRDecompositionD(a);
            MatrixD b = new MatrixD(10, 1);
            RandomHelper.Random.NextMatrixD(b, 0, 1);
            MatrixD x = d.SolveLinearEquations(b);
            Assert.IsTrue(MatrixD.AreNumericallyEqual(a, d.Q * d.R));
            Assert.IsTrue(MatrixD.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).
              MatrixD spdMatrix = a.Transposed * a;
              CholeskyDecompositionD ch = new CholeskyDecompositionD(spdMatrix);
              MatrixD x2 = ch.SolveLinearEquations(a.Transposed * b);

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

            MatrixD h = d.H;  // Just call this one to see if it runs through.
              }
        }
        public void Test2()
        {
            // Every transpose(A)*A is SPD if A has full column rank and m > n.
              MatrixD a = new MatrixD(new double[,] { { 1, 2, 3},
                                             { 4, 5, 6},
                                             { 7, 8, 9},
                                             { 10, 11, -12}});
              //MatrixD a = new MatrixD(new double[,] { { 1, 2},
              //                                       { 4, 5},
              //                                       { 7, 8}});

              a = a.Transposed * a;

              CholeskyDecompositionD d = new CholeskyDecompositionD(a);

              Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

              MatrixD l = d.L;

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

              Assert.IsTrue(MatrixD.AreNumericallyEqual(a, l * l.Transposed));
        }
        public void TestEmptyMatrix()
        {
            MatrixD a = new MatrixD(2, 2);

              CholeskyDecompositionD d = new CholeskyDecompositionD(a);

              Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
        public void TestWithNaNValues()
        {
            MatrixD a = new MatrixD(new[,] {{ 0, 1, 2 },
                                      { 1, 4, 3 },
                                      { 5, 3, double.NaN}});

              // Any result is ok. We must only check for infinite loops!
              var d = new CholeskyDecompositionD(a);
              d = new CholeskyDecompositionD(new MatrixD(4, 4, double.NaN));
        }
        public void TestRectangularA()
        {
            MatrixD a = new MatrixD(new double[,] { { 2, -1},
                                             { -1, 2},
                                             { 0, -1} });

              CholeskyDecompositionD d = new CholeskyDecompositionD(a);
              Assert.AreEqual(false, d.IsSymmetricPositiveDefinite);
        }
        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++)
              {
            VectorD column1 = new VectorD(4);
            RandomHelper.Random.NextVectorD(column1, 1, 2);
            VectorD column2 = new VectorD(4);
            RandomHelper.Random.NextVectorD(column2, 1, 2);

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

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

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

            MatrixD spdMatrix = a.Transposed * a;

            CholeskyDecompositionD d = new CholeskyDecompositionD(spdMatrix);

            Assert.AreEqual(true, d.IsSymmetricPositiveDefinite);

            MatrixD 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(MatrixD.AreNumericallyEqual(spdMatrix, l * l.Transposed));

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

            MatrixD x = d.SolveLinearEquations(b);
            MatrixD b2 = spdMatrix * x;
            Assert.IsTrue(MatrixD.AreNumericallyEqual(b, b2, 0.01f));
              }
        }