So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
EigenSparseLinearSolvers.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2013 Gilles Daviet <gdaviet@gmail.com>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10 
11 
12 #ifndef BOGUS_EIGEN_SPARSE_LINEAR_SOLVERS_HPP
13 #define BOGUS_EIGEN_SPARSE_LINEAR_SOLVERS_HPP
14 
15 /* Depending on your version of Eigen, this file may make use of LGPL licensed code.
16  See Eigen/src/Sparse/SimplicialCholesky.h for more information
17 */
18 
19 #include "SparseHeader.hpp"
20 
21 #ifdef BOGUS_WITH_EIGEN_STABLE_SPARSE_API
22 
23 #include "EigenLinearSolvers.hpp"
24 #include "../Utils/LinearSolverBase.hpp"
25 #include "../Utils/NaiveSharedPtr.hpp"
26 
27 #include <Eigen/OrderingMethods>
28 
29 
30 #ifndef EIGEN_MPL2_ONLY
31 
32 #include <Eigen/SparseCholesky>
33 #define BOGUS_WITH_EIGEN_SPARSE_LDLT
34 
35 namespace bogus {
36 
37 #if ! EIGEN_VERSION_AT_LEAST(3,2,90)
38 template < typename MatrixType, typename RhsType >
39 struct EigenSolveResult< Eigen::SimplicialLDLT< MatrixType >, RhsType >
40 {
41  typedef Eigen::SimplicialLDLT< MatrixType > FactType ;
42  typedef Eigen::internal::solve_retval< Eigen::SimplicialCholeskyBase< FactType >, RhsType > Type ;
43 };
44 #endif
45 
46 template < typename Derived >
47 struct LinearSolverTraits< LDLT< Eigen::SparseMatrixBase< Derived > > >
48 {
49  typedef typename Derived::PlainObject MatrixType ;
50  typedef Eigen::SimplicialLDLT< MatrixType > FactType ;
51 
52  template < typename RhsT > struct Result {
53  typedef typename EigenSolveResult< FactType, RhsT >::Type Type ;
54  } ;
55  template < typename RhsT >
56  struct Result< Eigen::MatrixBase< RhsT > > {
57  typedef typename Result< RhsT >::Type Type ;
58  } ;
59 } ;
60 
61 
62 template < typename Derived >
63 struct LDLT< Eigen::SparseMatrixBase< Derived > >
64  : public LinearSolverBase< LDLT< Eigen::SparseMatrixBase< Derived > > >
65 {
66  typedef Eigen::SparseMatrixBase< Derived > MatrixType ;
67  typedef LinearSolverTraits< LDLT< MatrixType > > Traits ;
68 
69  LDLT() {}
70  template< typename OtherDerived >
71  explicit LDLT ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
72  : m_fact( new typename Traits::FactType( mat ) )
73  {}
74 
75  template< typename OtherDerived >
76  LDLT& compute ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
77  {
78  m_fact.reset( new typename Traits::FactType( mat ) ) ;
79  return *this ;
80  }
81 
82  template < typename RhsT, typename ResT >
83  void solve( const Eigen::MatrixBase< RhsT >& rhs, ResT& res ) const
84  {
85  assert( m_fact ) ;
86  res = m_fact->solve( rhs ) ;
87  }
88 
89  template < typename RhsT >
90  typename Traits::template Result< Eigen::MatrixBase< RhsT > >::Type
91  solve( const Eigen::MatrixBase< RhsT >& rhs ) const
92  {
93  assert( m_fact ) ;
94  return m_fact->solve( rhs ) ;
95  }
96 
97  const typename Traits::FactType& factorization() const {
98  return *m_fact ;
99  }
100 
101  private:
102  BOGUS_SHARED_PTR( typename Traits::FactType, m_fact ) ;
103 } ;
104 
105 template < typename Scalar, int _Options = 0, typename _Index = int >
106 struct SparseLDLT : public LDLT< Eigen::SparseMatrixBase< Eigen::SparseMatrix< Scalar, _Options, _Index > > >
107 {
108  SparseLDLT() {}
109  template< typename OtherDerived >
110  explicit SparseLDLT ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
111  : LDLT< Eigen::SparseMatrixBase< Eigen::SparseMatrix< Scalar, _Options, _Index > > >( mat )
112  {}
113 } ;
114 
115 } //snamespace bogus
116 
117 #endif // LDLT
118 
119 
120 #if EIGEN_VERSION_AT_LEAST(3,1,92)
121 
122 #include <Eigen/SparseLU>
123 
124 #define BOGUS_WITH_EIGEN_SPARSE_LU
125 
126 namespace bogus {
127 
128 template < typename Derived >
129 struct LinearSolverTraits< LU< Eigen::SparseMatrixBase< Derived > > >
130 {
131  typedef typename Derived::PlainObject MatrixType ;
132  typedef Eigen::SparseLU< MatrixType, Eigen::COLAMDOrdering<int> > FactType ;
133 
134  template < typename RhsT > struct Result {
135  typedef typename EigenSolveResult< FactType, RhsT >::Type Type ;
136  } ;
137  template < typename RhsT >
138  struct Result< Eigen::MatrixBase< RhsT > > {
139  typedef typename Result< RhsT >::Type Type ;
140  } ;
141 } ;
142 
143 
144 template < typename Derived >
145 struct LU< Eigen::SparseMatrixBase< Derived > >
146  : public LinearSolverBase< LU< Eigen::SparseMatrixBase< Derived > > >
147 {
148  typedef Eigen::SparseMatrixBase< Derived > MatrixType ;
149  typedef LinearSolverTraits< LU< MatrixType > > Traits ;
150 
151  LU() {}
152  template< typename OtherDerived >
153  explicit LU ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
154  : m_fact( new typename Traits::FactType( mat ) )
155  {}
156 
157  template< typename OtherDerived >
158  LU& compute ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
159  {
160  m_fact.reset( new typename Traits::FactType( mat ) ) ;
161  return *this ;
162  }
163 
164  template < typename RhsT, typename ResT >
165  void solve( const Eigen::MatrixBase< RhsT >& rhs, ResT& res ) const
166  {
167  assert( m_fact ) ;
168  res = m_fact->solve( rhs ) ;
169  }
170 
171  template < typename RhsT >
172  typename Traits::template Result< Eigen::MatrixBase< RhsT > >::Type
173  solve( const Eigen::MatrixBase< RhsT >& rhs ) const
174  {
175  assert( m_fact ) ;
176  return m_fact->solve( rhs ) ;
177  }
178 
179  const typename Traits::FactType& factorization() const {
180  return *m_fact ;
181  }
182 
183  private:
184  BOGUS_SHARED_PTR( typename Traits::FactType, m_fact ) ;
185 } ;
186 
187 template < typename Scalar, int _Options = 0, typename _Index = int >
188 struct SparseLU : public LU< Eigen::SparseMatrixBase< Eigen::SparseMatrix< Scalar, _Options, _Index > > >
189 {
190  SparseLU() {}
191  template< typename OtherDerived >
192  explicit SparseLU ( const Eigen::SparseMatrixBase< OtherDerived >& mat )
193  : LU< Eigen::SparseMatrixBase< Eigen::SparseMatrix< Scalar, _Options, _Index > > >( mat )
194  {}
195 } ;
196 
197 } //namespace bogus
198 
199 #endif // LU
200 
201 #endif // EIGEN_STABLE_API
202 
203 #endif //HPP
LinearSolverTraits< LDLT< MatrixType > >::template Result< RhsT >::Type solve(const RhsT &rhs) const
Returns the solution x of the linear system M * x = rhs.
Definition: LinearSolverBase.hpp:32