incomplete_cholesky.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2015-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. // #define EIGEN_DONT_VECTORIZE
  10. // #define EIGEN_MAX_ALIGN_BYTES 0
  11. #include "sparse_solver.h"
  12. #include <Eigen/IterativeLinearSolvers>
  13. #include <unsupported/Eigen/IterativeSolvers>
  14. template<typename T, typename I_> void test_incomplete_cholesky_T()
  15. {
  16. typedef SparseMatrix<T,0,I_> SparseMatrixType;
  17. ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > > cg_illt_lower_amd;
  18. ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, NaturalOrdering<I_> > > cg_illt_lower_nat;
  19. ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, AMDOrdering<I_> > > cg_illt_upper_amd;
  20. ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, NaturalOrdering<I_> > > cg_illt_upper_nat;
  21. ConjugateGradient<SparseMatrixType, Upper|Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > > cg_illt_uplo_amd;
  22. CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_amd) );
  23. CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_nat) );
  24. CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_amd) );
  25. CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_nat) );
  26. CALL_SUBTEST( check_sparse_spd_solving(cg_illt_uplo_amd) );
  27. }
  28. template<int>
  29. void bug1150()
  30. {
  31. // regression for bug 1150
  32. for(int N = 1; N<20; ++N)
  33. {
  34. Eigen::MatrixXd b( N, N );
  35. b.setOnes();
  36. Eigen::SparseMatrix<double> m( N, N );
  37. m.reserve(Eigen::VectorXi::Constant(N,4));
  38. for( int i = 0; i < N; ++i )
  39. {
  40. m.insert( i, i ) = 1;
  41. m.coeffRef( i, i / 2 ) = 2;
  42. m.coeffRef( i, i / 3 ) = 2;
  43. m.coeffRef( i, i / 4 ) = 2;
  44. }
  45. Eigen::SparseMatrix<double> A;
  46. A = m * m.transpose();
  47. Eigen::ConjugateGradient<Eigen::SparseMatrix<double>,
  48. Eigen::Lower | Eigen::Upper,
  49. Eigen::IncompleteCholesky<double> > solver( A );
  50. VERIFY(solver.preconditioner().info() == Eigen::Success);
  51. VERIFY(solver.info() == Eigen::Success);
  52. }
  53. }
  54. EIGEN_DECLARE_TEST(incomplete_cholesky)
  55. {
  56. CALL_SUBTEST_1(( test_incomplete_cholesky_T<double,int>() ));
  57. CALL_SUBTEST_2(( test_incomplete_cholesky_T<std::complex<double>, int>() ));
  58. CALL_SUBTEST_3(( test_incomplete_cholesky_T<double,long int>() ));
  59. CALL_SUBTEST_1(( bug1150<0>() ));
  60. }