private static void CheckSystemSolution2()
        {
            Skip.IfNot(TestSettings.TestSuiteSparse, TestSettings.MessageWhenSkippingSuiteSparse);

            int order = SparsePosDef10by10.Order;

            // Build the matrices and right hand sides
            var dense = Matrix.CreateFromArray(SparsePosDef10by10.Matrix);
            //var skyline = SkylineMatrix.CreateFromArrays(order, SparsePositiveDefinite.skylineValues,
            //    SparsePositiveDefinite.skylineDiagOffsets, false);
            //var dok = DOKSymmetricColMajor.CreateFromSparseMatrix(skyline);
            var dok = DokSymmetric.CreateEmpty(order);

            for (int j = 0; j < order; ++j)
            {
                for (int i = 0; i <= j; ++i)
                {
                    if (dense[i, j] != 0)
                    {
                        dok[i, j] = dense[i, j];
                    }
                }
            }
            Vector b = Vector.CreateFromArray(SparsePosDef10by10.Rhs);
            Matrix B = Matrix.CreateFromArray(SquareInvertible10by10.Matrix);

            // Solve using dense algebra
            CholeskyFull chol           = dense.FactorCholesky(false);
            Matrix       U              = chol.GetFactorU();
            Matrix       L              = U.Transpose();
            Vector       xSolveExpect   = chol.SolveLinearSystem(b);
            Matrix       XSolveExpect   = dense.Invert() * B;
            Vector       xBackExpect    = U.Invert() * b;
            Matrix       XBackExpect    = U.Invert() * B;
            Vector       xForwardExpect = L.Invert() * b;
            Matrix       XForwardExpect = L.Invert() * B;

            // Solve using SuiteSparse
            var(values, rowIndices, colOffsets) = dok.BuildSymmetricCscArrays(true);
            CholeskySuiteSparse factor = CholeskySuiteSparse.Factorize(order, values.Length, values, rowIndices, colOffsets,
                                                                       true);
            Vector xSolveComput   = factor.SolveLinearSystem(b);
            Matrix XSolveComput   = factor.SolveLinearSystems(B);
            Vector xBackComput    = factor.BackSubstitution(b);
            Matrix XBackComput    = factor.BackSubstitutions(B);
            Vector xForwardComput = factor.ForwardSubstitution(b);
            Matrix XForwardComput = factor.ForwardSubstitutions(B);
            Vector xSolveComput2  = factor.BackSubstitution(factor.ForwardSubstitution(b));

            // Check results
            comparer.AssertEqual(xSolveExpect, xSolveComput);
            comparer.AssertEqual(XSolveExpect, XSolveComput);
            comparer.AssertEqual(xBackExpect, xBackComput);
            comparer.AssertEqual(XBackExpect, XBackComput);
            comparer.AssertEqual(xForwardExpect, xForwardComput);
            comparer.AssertEqual(XForwardExpect, XForwardComput);
        }
예제 #2
0
        private static void TestFactorization(LinearAlgebraProviderChoice providers)
        {
            TestSettings.RunMultiproviderTest(providers, delegate()
            {
                // positive definite
                var A1         = Matrix.CreateFromArray(SymmPosDef10by10.Matrix);
                var expectedU1 = Matrix.CreateFromArray(SymmPosDef10by10.FactorU);
                CholeskyFull factorization1 = A1.FactorCholesky(true);
                Matrix computedU1           = factorization1.GetFactorU();
                comparer.AssertEqual(expectedU1, computedU1);

                // singular
                var A2 = Matrix.CreateFromArray(SquareSingular10by10.Matrix);
                Assert.Throws <IndefiniteMatrixException>(() => A2.FactorCholesky(true));
            });
        }