示例#1
0
        public static void run()
        {
            int    dim1 = 5;
            Matrix A1   = Matrix.Random(dim1, dim1);

            A1.Add(Matrix.Transpose(A1));

            //MathNet.Numerics.LinearAlgebra.CholeskyDecomposition chol1 = new MathNet.Numerics.LinearAlgebra.CholeskyDecomposition(A1);
            MathNet.Numerics.LinearAlgebra.CholeskyDecomposition chol1 = A1.CholeskyDecomposition;
            Console.WriteLine("symmetric and positive definite matrix = {0}", chol1.IsSPD);
            if (chol1.IsSPD)
            {
                Console.WriteLine("Triangular factor matrix  = {0}", chol1.TriangularFactor.ToString());
            }

            int dim2 = 5;

            double[] data2 = { 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 1, 3, 6, 10, 15, 1, 4, 10, 20, 35, 1, 5, 15, 35, 70 };
            Matrix   A2    = new Matrix(data2, dim2);

            //MathNet.Numerics.LinearAlgebra.CholeskyDecomposition chol2 = new MathNet.Numerics.LinearAlgebra.CholeskyDecomposition(A2);
            MathNet.Numerics.LinearAlgebra.CholeskyDecomposition chol2 = A2.CholeskyDecomposition;
            Console.WriteLine("symmetric and positive definite matrix = {0}", chol2.IsSPD);
            if (chol2.IsSPD)
            {
                Console.WriteLine("Triangular factor matrix  = {0}", chol2.TriangularFactor.ToString());
            }
        }
        public void IRID90_CholeskySolve()
        {
            Matrix i = Matrix.Identity(3, 3);

            double[][] pvals1 = { new double[] { 1.0, 1.0, 1.0 }, new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 3.0, 6.0 } };
            Matrix m1 = new Matrix(pvals1);
            CholeskyDecomposition cd1 = new CholeskyDecomposition(m1);

            Matrix inv1a = cd1.Solve(i);
            Matrix test1a = m1 * inv1a;
            NumericAssert.AreAlmostEqual(i, test1a, "1A");
            Matrix inv1b = m1.Inverse();
            NumericAssert.AreAlmostEqual(inv1a, inv1b, "1B");

            double[][] pvals2 = { new double[] { 25, -5, 10 }, new double[] { -5, 17, 10 }, new double[] { 10, 10, 62 } };
            Matrix m2 = new Matrix(pvals2);
            CholeskyDecomposition cd2 = new CholeskyDecomposition(m2);

            Matrix inv2a = cd2.Solve(i);
            Matrix test2a = m2 * inv2a;
            NumericAssert.AreAlmostEqual(i, test2a, "2A");
            Matrix inv2b = m2.Inverse();
            NumericAssert.AreAlmostEqual(inv2a, inv2b, "2B");
        }