#define EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS #include "sparse_solver.h" #if defined(DEBUG) #undef DEBUG #endif #include template int generate_sparse_rectangular_problem(MatrixType& A, DenseMat& dA, int maxRows = 300, int maxCols = 300) { typedef typename MatrixType::Scalar Scalar; int rows = internal::random(1, maxRows); int cols = internal::random(1, maxCols); double density = (std::max)(8.0 / (rows * cols), 0.01); A.resize(rows, cols); dA.resize(rows, cols); initSparse(density, dA, A, ForceNonZeroDiag); A.makeCompressed(); return rows; } template int generate_sparse_square_symmetric_problem(MatrixType& A, DenseMat& dA, int maxSize = 300) { typedef typename MatrixType::Scalar Scalar; int rows = internal::random(1, maxSize); int cols = rows; double density = (std::max)(8.0 / (rows * cols), 0.01); A.resize(rows, cols); dA.resize(rows, cols); initSparse(density, dA, A, ForceNonZeroDiag); dA = dA * dA.transpose(); A = A * A.transpose(); A.makeCompressed(); return rows; } template void test_accelerate_ldlt() { typedef SparseMatrix MatrixType; typedef Matrix DenseVector; MatrixType A; Matrix dA; generate_sparse_square_symmetric_problem(A, dA); DenseVector b = DenseVector::Random(A.rows()); Solver solver; solver.compute(A); if (solver.info() != Success) { std::cerr << "sparse LDLT factorization failed\n"; exit(0); return; } DenseVector x = solver.solve(b); if (solver.info() != Success) { std::cerr << "sparse LDLT factorization failed\n"; exit(0); return; } // Compare with a dense solver DenseVector refX = dA.ldlt().solve(b); VERIFY((A * x).isApprox(A * refX, test_precision())); } template void test_accelerate_llt() { typedef SparseMatrix MatrixType; typedef Matrix DenseVector; MatrixType A; Matrix dA; generate_sparse_square_symmetric_problem(A, dA); DenseVector b = DenseVector::Random(A.rows()); Solver solver; solver.compute(A); if (solver.info() != Success) { std::cerr << "sparse LLT factorization failed\n"; exit(0); return; } DenseVector x = solver.solve(b); if (solver.info() != Success) { std::cerr << "sparse LLT factorization failed\n"; exit(0); return; } // Compare with a dense solver DenseVector refX = dA.llt().solve(b); VERIFY((A * x).isApprox(A * refX, test_precision())); } template void test_accelerate_qr() { typedef SparseMatrix MatrixType; typedef Matrix DenseVector; MatrixType A; Matrix dA; generate_sparse_rectangular_problem(A, dA); DenseVector b = DenseVector::Random(A.rows()); Solver solver; solver.compute(A); if (solver.info() != Success) { std::cerr << "sparse QR factorization failed\n"; exit(0); return; } DenseVector x = solver.solve(b); if (solver.info() != Success) { std::cerr << "sparse QR factorization failed\n"; exit(0); return; } // Compare with a dense solver DenseVector refX = dA.colPivHouseholderQr().solve(b); VERIFY((A * x).isApprox(A * refX, test_precision())); } template void run_tests() { typedef SparseMatrix MatrixType; test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_ldlt >(); test_accelerate_llt >(); test_accelerate_llt >(); test_accelerate_qr >(); } EIGEN_DECLARE_TEST(accelerate_support) { CALL_SUBTEST_1(run_tests()); CALL_SUBTEST_2(run_tests()); }