Пример #1
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(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.
            }
        }
        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 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 SolveLinearEquationsException1()
        {
            // Create A.
              MatrixF a = new MatrixF(new float[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 } });

              CholeskyDecompositionF decomp = new CholeskyDecompositionF(a);
              decomp.SolveLinearEquations(null);
        }
        public void SolveLinearEquationsException1()
        {
            // Create A.
            MatrixF a = new MatrixF(new float[, ] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, -9 }
            });

            CholeskyDecompositionF decomp = new CholeskyDecompositionF(a);

            decomp.SolveLinearEquations(null);
        }
        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));
              }
        }
Пример #8
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(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.
              }
        }