xref: /aosp_15_r20/external/eigen/test/incomplete_cholesky.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015-2016 Gael Guennebaud <[email protected]>
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 
test_incomplete_cholesky_T()15 template<typename T, typename I_> void test_incomplete_cholesky_T()
16 {
17   typedef SparseMatrix<T,0,I_> SparseMatrixType;
18   ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > >        cg_illt_lower_amd;
19   ConjugateGradient<SparseMatrixType, Lower, IncompleteCholesky<T, Lower, NaturalOrdering<I_> > >    cg_illt_lower_nat;
20   ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, AMDOrdering<I_> > >        cg_illt_upper_amd;
21   ConjugateGradient<SparseMatrixType, Upper, IncompleteCholesky<T, Upper, NaturalOrdering<I_> > >    cg_illt_upper_nat;
22   ConjugateGradient<SparseMatrixType, Upper|Lower, IncompleteCholesky<T, Lower, AMDOrdering<I_> > >  cg_illt_uplo_amd;
23 
24 
25   CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_amd) );
26   CALL_SUBTEST( check_sparse_spd_solving(cg_illt_lower_nat) );
27   CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_amd) );
28   CALL_SUBTEST( check_sparse_spd_solving(cg_illt_upper_nat) );
29   CALL_SUBTEST( check_sparse_spd_solving(cg_illt_uplo_amd) );
30 }
31 
32 template<int>
bug1150()33 void bug1150()
34 {
35   // regression for bug 1150
36   for(int N = 1; N<20; ++N)
37   {
38     Eigen::MatrixXd b( N, N );
39     b.setOnes();
40 
41     Eigen::SparseMatrix<double> m( N, N );
42     m.reserve(Eigen::VectorXi::Constant(N,4));
43     for( int i = 0; i < N; ++i )
44     {
45         m.insert( i, i ) = 1;
46         m.coeffRef( i, i / 2 ) = 2;
47         m.coeffRef( i, i / 3 ) = 2;
48         m.coeffRef( i, i / 4 ) = 2;
49     }
50 
51     Eigen::SparseMatrix<double> A;
52     A = m * m.transpose();
53 
54     Eigen::ConjugateGradient<Eigen::SparseMatrix<double>,
55         Eigen::Lower | Eigen::Upper,
56         Eigen::IncompleteCholesky<double> > solver( A );
57     VERIFY(solver.preconditioner().info() == Eigen::Success);
58     VERIFY(solver.info() == Eigen::Success);
59   }
60 }
61 
EIGEN_DECLARE_TEST(incomplete_cholesky)62 EIGEN_DECLARE_TEST(incomplete_cholesky)
63 {
64   CALL_SUBTEST_1(( test_incomplete_cholesky_T<double,int>() ));
65   CALL_SUBTEST_2(( test_incomplete_cholesky_T<std::complex<double>, int>() ));
66   CALL_SUBTEST_3(( test_incomplete_cholesky_T<double,long int>() ));
67 
68   CALL_SUBTEST_1(( bug1150<0>() ));
69 }
70