private static void TestSystemSolution1() { Skip.IfNot(TestSettings.TestSuiteSparse, TestSettings.MessageWhenSkippingSuiteSparse); // Define linear system var rhs = Vector.CreateFromArray(new double[] { 6.0, 14.0, 11.0, 12.0 }); var solutionExpected = Vector.CreateFromArray(new double[] { 1.0, 1.0, 1.0, 1.0 }); var matrixDOK = DokSymmetric.CreateEmpty(4); matrixDOK[0, 0] = 4.0; matrixDOK[0, 2] = 2.0; matrixDOK[1, 1] = 10.0; matrixDOK[1, 2] = 1.0; matrixDOK[1, 3] = 3.0; matrixDOK[2, 2] = 8.0; matrixDOK[3, 3] = 9.0; SymmetricCscMatrix matrixCSC = matrixDOK.BuildSymmetricCscMatrix(true); //const int n = 4; //const int nnz = 7; //int[] colOffsets = new int[n + 1] { 0, 1, 2, 5, nnz }; //int[] rowIndices = new int[nnz] { 0, 1, 0, 1, 2, 1, 3 }; //double[] values = new double[nnz] { 4.0, 10.0, 2.0, 1.0, 8.0, 3.0, 9.0 }; //SymmetricCSC matrixCSC = new SymmetricCSC(values, rowIndices, colOffsets, false); //Solve it using SuiteSparse using (var factor = CholeskySuiteSparse.Factorize(matrixCSC, true)) { Vector solution = factor.SolveLinearSystem(rhs); comparer.AssertEqual(solutionExpected, solution); } }
public static void MemoryConsumptionDebugging() { int order = 100000; int bandwidth = 200; for (int rep = 0; rep < 10; ++rep) { var dok = DokSymmetric.CreateEmpty(order); for (int i = 0; i < order; ++i) { dok[i, i] = 10.0; if (i >= bandwidth) { dok[i - bandwidth, i] = 1.0; } else { dok[0, i] = 1.0; } } dok[0, 0] = 10.0; SymmetricCscMatrix matrix = dok.BuildSymmetricCscMatrix(true); var rhs = Vector.CreateWithValue(order, 2.0); var solution = Vector.CreateZero(order); using (var factorization = CholeskySuiteSparse.Factorize(matrix, true)) { factorization.SolveLinearSystem(rhs, solution); } } }
/// <summary> /// Performs the Cholesky factorization: A = L * L^T of a symmetric positive definite matrix A. /// Only the upper triangle of the original matrix is required and is provided in symmetric CSC format. /// </summary> /// <param name="matrix">The matrix in symmetric (only upper triangle) CSC format.</param> /// <exception cref="IndefiniteMatrixException">Thrown if the original matrix is not positive definite.</exception> public static CholeskyCSparseNet Factorize(SymmetricCscMatrix matrix) => Factorize(matrix.NumColumns, matrix.NumNonZerosUpper, matrix.RawValues, matrix.RawRowIndices, matrix.RawColOffsets);