BubbleProfiler  0.3.0
by Peter Athron, Csaba Balazs, Michael Bardsley, Andrew Fowlie, Dylan Harries & Graham White
eigen_state_utils.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of BubbleProfiler.
3  *
4  * BubbleProfiler is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * BubbleProfiler is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with BubbleProfiler. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BUBBLEPROFILER_EIGEN_STATE_UTILS_HPP_INCLUDED
19 #define BUBBLEPROFILER_EIGEN_STATE_UTILS_HPP_INCLUDED
20 
21 #include <Eigen/Core>
22 
23 #include <boost/version.hpp>
24 
25 #if BOOST_VERSION >= 105600
26 #include <boost/numeric/odeint/algebra/algebra_dispatcher.hpp>
27 #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
28 #else
29 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
30 #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
31 #endif
32 
33 namespace Eigen {
34 
35 template <typename Derived>
36 typename Eigen::MatrixBase<Derived>::PlainObject
37 operator+(const Eigen::MatrixBase<Derived>& m,
38  const typename Eigen::MatrixBase<Derived>::Scalar& s)
39 {
40  return m + s * Eigen::MatrixBase<Derived>::Ones(m.rows(), m.cols());
41 }
42 
43 template <typename Derived>
44 typename Eigen::MatrixBase<Derived>::PlainObject
45 operator+(const typename Eigen::MatrixBase<Derived>::Scalar& s,
46  const Eigen::MatrixBase<Derived>& m)
47 {
48  return m + s * Eigen::MatrixBase<Derived>::Ones(m.rows(), m.cols());
49 }
50 
51 template <typename DerivedA, typename DerivedB>
52 auto operator/(const Eigen::MatrixBase<DerivedA>& x1,
53  const Eigen::MatrixBase<DerivedB>& x2)
54  -> decltype(x1.cwiseQuotient(x2))
55 {
56  return x1.cwiseQuotient(x2);
57 }
58 
59 template <typename Derived>
60 auto abs(const Eigen::MatrixBase<Derived>& m)
61  -> decltype(m.cwiseAbs())
62 {
63  return m.cwiseAbs();
64 }
65 
66 } // namespace Eigen
67 
68 #if BOOST_VERSION >= 105600
69 
70 namespace boost {
71 namespace numeric {
72 namespace odeint {
73 
74 template <typename Scalar, int Rows, int Cols, int Opts,
75  int MaxRows, int MaxCols>
76 struct vector_space_norm_inf<
77  Eigen::Matrix<Scalar,Rows,Cols,Opts,MaxRows,MaxCols> > {
78  using result_type = typename Eigen::Matrix<Scalar,Rows,Cols,Opts,
79  MaxRows,MaxCols>::RealScalar;
80 
81  result_type operator()(const Eigen::Matrix<Scalar,Rows,Cols,
82  Opts,MaxRows,MaxCols>& m) const {
83  return m.template lpNorm<Eigen::Infinity>();
84  }
85 };
86 
87 template< class Derived >
88 struct algebra_dispatcher_sfinae< Derived ,
89  typename boost::enable_if<
90  typename boost::is_base_of<
91  Eigen::MatrixBase< Derived > ,
92  Derived >::type >::type >
93 {
94  typedef vector_space_algebra algebra_type;
95 };
96 
97 
98 template < class Derived >
99 struct algebra_dispatcher_sfinae< Derived ,
100  typename boost::enable_if<
101  typename boost::is_base_of<
102  Eigen::ArrayBase< Derived > ,
103  Derived >::type >::type >
104 {
105  typedef vector_space_algebra algebra_type;
106 };
107 
108 } // namespace odeint
109 } // namespace numeric
110 } // namespace boost
111 
112 namespace BubbleProfiler {
113 
114 template <class State>
115 struct State_algebra_dispatcher {
116  using algebra_type =
117  typename boost::numeric::odeint::algebra_dispatcher<State>::algebra_type;
118 };
119 
120 } // namespace BubbleProfiler
121 
122 #else
123 
124 namespace BubbleProfiler {
125 
126 namespace detail {
127 
128 template <class State, class Enable = void>
130  using algebra_type = boost::numeric::odeint::range_algebra;
131 };
132 
133 template< class Derived >
135  typename boost::enable_if<
136  typename boost::is_base_of<
137  Eigen::MatrixBase< Derived > ,
138  Derived >::type >::type >
139 {
140  using algebra_type = boost::numeric::odeint::vector_space_algebra;
141 };
142 
143 
144 template < class Derived >
146  typename boost::enable_if<
147  typename boost::is_base_of<
148  Eigen::ArrayBase< Derived > ,
149  Derived >::type >::type >
150 {
151  using algebra_type = boost::numeric::odeint::vector_space_algebra;
152 };
153 
154 } // namespace detail
155 
156 template <class State>
159 
160 
161 } // namespace BubbleProfiler
162 
163 #endif
164 
165 #endif
Eigen::MatrixBase< Derived >::PlainObject operator+(const Eigen::MatrixBase< Derived > &m, const typename Eigen::MatrixBase< Derived >::Scalar &s)
auto abs(const Eigen::MatrixBase< Derived > &m) -> decltype(m.cwiseAbs())
auto operator/(const Eigen::MatrixBase< DerivedA > &x1, const Eigen::MatrixBase< DerivedB > &x2) -> decltype(x1.cwiseQuotient(x2))