// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2012 Désiré Nuentsa-Wakam // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // SPDX-License-Identifier: MPL-2.0 // SparseLU solve does not accept column major matrices for the destination. // However, as expected, the generic check_sparse_square_solving routines produces row-major // rhs and destination matrices when compiled with EIGEN_DEFAULT_TO_ROW_MAJOR #ifdef EIGEN_DEFAULT_TO_ROW_MAJOR #undef EIGEN_DEFAULT_TO_ROW_MAJOR #endif #include "sparse_solver.h" #include template void test_sparselu_T() { SparseLU /*, COLAMDOrdering*/> sparselu_colamd; // COLAMDOrdering is the default SparseLU, AMDOrdering > sparselu_amd; SparseLU, NaturalOrdering > sparselu_natural; check_sparse_square_solving(sparselu_colamd, 300, 100000, true); check_sparse_square_solving(sparselu_amd, 300, 10000, true); check_sparse_square_solving(sparselu_natural, 300, 2000, true); check_sparse_square_abs_determinant(sparselu_colamd); check_sparse_square_abs_determinant(sparselu_amd); check_sparse_square_determinant(sparselu_colamd); check_sparse_square_determinant(sparselu_amd); } template void test_sparselu_rowmajor_compressed_input() { typedef SparseMatrix RowMajorSparseMatrix; typedef Matrix Vector; Vector b(2); b << T(1.1), T(3.14); Vector expected(2); expected << T(1.1 - 0.0001 * 3.14), T(3.14); RowMajorSparseMatrix compressed(2, 2); compressed.insert(0, 0) = T(1.0); compressed.insert(0, 1) = T(0.0001); compressed.insert(1, 1) = T(1.0); compressed.makeCompressed(); RowMajorSparseMatrix uncompressed(2, 2); uncompressed.insert(0, 0) = T(1.0); uncompressed.insert(0, 1) = T(0.0001); uncompressed.insert(1, 1) = T(1.0); SparseLU compressed_solver; compressed_solver.compute(compressed); VERIFY_IS_EQUAL(compressed_solver.info(), Success); VERIFY_IS_APPROX(compressed_solver.solve(b), expected); SparseLU two_step_solver; two_step_solver.analyzePattern(compressed); two_step_solver.factorize(compressed); VERIFY_IS_EQUAL(two_step_solver.info(), Success); VERIFY_IS_APPROX(two_step_solver.solve(b), expected); SparseLU uncompressed_solver; uncompressed_solver.compute(uncompressed); VERIFY_IS_EQUAL(uncompressed_solver.info(), Success); VERIFY_IS_APPROX(uncompressed_solver.solve(b), expected); } template void test_sparselu_colmajor_uncompressed_input() { typedef SparseMatrix ColMajorSparseMatrix; typedef Matrix Vector; Vector b(2); b << T(1.1), T(3.14); Vector expected(2); expected << T(1.1 - 0.0001 * 3.14), T(3.14); ColMajorSparseMatrix uncompressed(2, 2); uncompressed.insert(0, 0) = T(1.0); uncompressed.insert(0, 1) = T(0.0001); uncompressed.insert(1, 1) = T(1.0); SparseLU uncompressed_solver; uncompressed_solver.compute(uncompressed); VERIFY_IS_EQUAL(uncompressed_solver.info(), Success); VERIFY_IS_APPROX(uncompressed_solver.solve(b), expected); } // Regression for issue #1908: lastErrorMessage() must not carry over from a // previous failed factorize() once a subsequent factorize() succeeds. template void test_sparselu_clear_error_state() { typedef SparseMatrix ColMajorSparseMatrix; ColMajorSparseMatrix singular(3, 3); // structurally singular: no nonzeros ColMajorSparseMatrix non_singular(3, 3); non_singular.insert(0, 0) = T(1); non_singular.insert(1, 1) = T(1); non_singular.insert(2, 2) = T(1); non_singular.makeCompressed(); SparseLU solver; solver.compute(singular); VERIFY(solver.info() != Success); VERIFY(!solver.lastErrorMessage().empty()); solver.compute(non_singular); VERIFY_IS_EQUAL(solver.info(), Success); VERIFY(solver.lastErrorMessage().empty()); } EIGEN_DECLARE_TEST(sparselu) { CALL_SUBTEST_1(test_sparselu_T()); CALL_SUBTEST_2(test_sparselu_T()); CALL_SUBTEST_3(test_sparselu_T >()); CALL_SUBTEST_4(test_sparselu_T >()); CALL_SUBTEST_5(test_sparselu_rowmajor_compressed_input()); CALL_SUBTEST_6(test_sparselu_rowmajor_compressed_input()); CALL_SUBTEST_7(test_sparselu_colmajor_uncompressed_input()); CALL_SUBTEST_8(test_sparselu_colmajor_uncompressed_input()); CALL_SUBTEST_9(test_sparselu_clear_error_state()); CALL_SUBTEST_10(test_sparselu_clear_error_state()); }