So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
EigenLinearSolvers.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_LINEAR_SOLVERS
13 #define BOGUS_EIGEN_LINEAR_SOLVERS
14 
15 #include "../Utils/LinearSolverBase.hpp"
16 
17 #include <Eigen/LU>
18 #include <Eigen/Cholesky>
19 
20 namespace bogus {
21 
22 template<typename Decomposition, typename RhsType>
24 {
25 #if EIGEN_VERSION_AT_LEAST(3,2,90)
26  typedef const Eigen::Solve< Decomposition, RhsType > Type ;
27 #else
28  typedef Eigen::internal::solve_retval< Decomposition, RhsType > Type ;
29 #endif
30 };
31 
32 template < typename Derived >
33 struct LinearSolverTraits< LU< Eigen::MatrixBase< Derived > > >
34 {
35  typedef typename Derived::PlainObject MatrixType ;
36  typedef Eigen::FullPivLU< MatrixType > FactType ;
37 
38  template < typename RhsT > struct Result {
39  typedef typename EigenSolveResult< FactType, RhsT >::Type Type ;
40  } ;
41  template < typename RhsT >
42  struct Result< Eigen::MatrixBase< RhsT > > {
43  typedef typename Result< RhsT >::Type Type ;
44  } ;
45 } ;
46 
47 
48 template < typename Derived >
49 struct LU< Eigen::MatrixBase< Derived > >
50  : public LinearSolverBase< LU< Eigen::MatrixBase< Derived > > >
51 {
52  typedef Eigen::MatrixBase< Derived > MatrixType ;
54 
55  LU() {}
56  template< typename OtherDerived >
57  explicit LU ( const Eigen::MatrixBase< OtherDerived >& mat )
58  : m_fact( mat )
59  {}
60 
61  template< typename OtherDerived >
62  LU< Eigen::MatrixBase< Derived > >& compute ( const Eigen::MatrixBase< OtherDerived >& mat )
63  {
64  m_fact.compute( mat ) ;
65  return *this ;
66  }
67 
68  template < typename RhsT, typename ResT >
69  void solve( const Eigen::MatrixBase< RhsT >& rhs, ResT& res ) const
70  {
71  res = m_fact.solve( rhs ) ;
72  }
73 
74  template < typename RhsT >
75  typename Traits::template Result< Eigen::MatrixBase< RhsT > >::Type
76  solve( const Eigen::MatrixBase< RhsT >& rhs ) const
77  {
78  return m_fact.solve( rhs ) ;
79  }
80 
81  private:
82  typename Traits::FactType m_fact ;
83 } ;
84 
85 template < typename Scalar, int Rows, int Cols = Rows, int Options = 0 >
86 struct DenseLU : public LU< Eigen::MatrixBase< Eigen::Matrix< Scalar, Rows, Cols, Options > > >
87 {
88  DenseLU() {}
89  template< typename OtherDerived >
90  explicit DenseLU ( const Eigen::MatrixBase< OtherDerived >& mat )
92  {}
93 } ;
94 
95 template < typename Derived >
96 struct LinearSolverTraits< LDLT< Eigen::MatrixBase< Derived > > >
97 {
98  typedef typename Derived::PlainObject MatrixType ;
99  typedef Eigen::LDLT< MatrixType > FactType ;
100 
101  template < typename RhsT > struct Result {
102  typedef typename EigenSolveResult< FactType, RhsT >::Type Type ;
103  } ;
104  template < typename RhsT >
105  struct Result< Eigen::MatrixBase< RhsT > > {
106  typedef typename Result< RhsT >::Type Type ;
107  } ;
108 } ;
109 
110 
111 template < typename Derived >
112 struct LDLT< Eigen::MatrixBase< Derived > >
113  : public LinearSolverBase< LDLT< Eigen::MatrixBase< Derived > > >
114 {
115  typedef Eigen::MatrixBase< Derived > MatrixType ;
117 
118  LDLT() {}
119  template< typename OtherDerived >
120  explicit LDLT ( const Eigen::MatrixBase< OtherDerived >& mat )
121  : m_fact( mat )
122  {}
123 
124  template< typename OtherDerived >
125  LDLT< Eigen::MatrixBase< Derived > >& compute ( const Eigen::MatrixBase< OtherDerived >& mat )
126  {
127  m_fact.compute( mat ) ;
128  return *this ;
129  }
130 
131  template < typename RhsT, typename ResT >
132  void solve( const Eigen::MatrixBase< RhsT >& rhs, ResT& res ) const
133  {
134  res = m_fact.solve( rhs ) ;
135  }
136 
137  template < typename RhsT >
138  typename Traits::template Result< Eigen::MatrixBase< RhsT > >::Type
139  solve( const Eigen::MatrixBase< RhsT >& rhs ) const
140  {
141  return m_fact.solve( rhs ) ;
142  }
143 
144  private:
145  typename Traits::FactType m_fact ;
146 } ;
147 
148 template < typename Scalar, int Rows, int Options = 0 >
149 struct DenseLDLT : public LDLT< Eigen::MatrixBase< Eigen::Matrix< Scalar, Rows, Rows, Options > > >
150 {
151  DenseLDLT() {}
152  template< typename OtherDerived >
153  explicit DenseLDLT ( const Eigen::MatrixBase< OtherDerived >& mat )
155  {}
156 } ;
157 
158 template < typename Derived, typename RhsT >
159 typename LinearSolverTraits< Derived >::template Result< Eigen::MatrixBase< RhsT > >::Type operator*
160  ( const LinearSolverBase< Derived >& solver, const Eigen::MatrixBase< RhsT >& rhs )
161 {
162  return solver.solve( rhs ) ;
163 }
164 
165 
166 } //namespace bogus
167 
168 
169 #endif
Definition: EigenLinearSolvers.hpp:23
Definition: EigenLinearSolvers.hpp:86
Definition: LinearSolverBase.hpp:22
Base class for linear solvers on base ( i.e. non-block ) matrices.
Definition: LinearSolverBase.hpp:26
Definition: EigenLinearSolvers.hpp:149
Base class for LDLT factorizations.
Definition: LinearSolverBase.hpp:72
Base class for LU factorizations.
Definition: LinearSolverBase.hpp:66
LinearSolverTraits< LU< 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