antioch-0.4.0
eigen_utils.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_EIGEN_UTILS_H
28 #define ANTIOCH_EIGEN_UTILS_H
29 
30 #ifdef ANTIOCH_METAPROGRAMMING_H
31 # ifndef ANTIOCH_EIGEN_UTILS_DECL_H
32 # error eigen_utils_decl.h must be included before metaprogramming.h
33 # endif
34 #endif
35 
36 #include "antioch_config.h"
37 
38 #ifdef ANTIOCH_HAVE_EIGEN
39 // Though the following implementations are all valid without <Eigen/Dense>,
40 // successfully using them with Eigen types requires Eigen be included first.
41 // Configure-time Eigen support enforces this constraint but header-only Eigen
42 // may be mixed with header-only Antioch without configure-time flags.
43 #include <Eigen/Dense>
44 #endif
45 
47 
48 // Notice _Matrix template templates might be Eigen::Matrix or Eigen::Array.
49 // Therefore, always use .array()- or .matrix()-like operations for robustness.
50 // Otherwise Eigen will complain with YOU_CANNOT_MIX_ARRAYS_AND_MATRICES.
51 
52 namespace std
53 {
54 
55 template <
56  template <typename, int, int, int, int, int> class _Matrix,
57  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
58 >
59 inline
60 ANTIOCH_AUTO(_Matrix<_Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols>)
61 max(const _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & a,
62  const _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & b)
63 ANTIOCH_AUTOFUNC(_Matrix<_Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols>,
64 a.array().max(b.array()))
65 
66 template <
67  template <typename, int, int, int, int, int> class _Matrix,
68  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
69 >
70 inline
71 ANTIOCH_AUTO(_Matrix<_Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols>)
72 min(const _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & a,
73  const _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & b)
74 ANTIOCH_AUTOFUNC(_Matrix<_Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols>,
75 a.array().min(b.array()))
76 
77 } // end namespace std
78 
79 
80 namespace Antioch
81 {
82 
83 template <typename T>
84 inline
86  typename value_type<T>::type
87  >::type
88 max(const T& in)
89 {
90  return in.maxCoeff();
91 }
92 
93 template <typename T>
94 inline
96  typename value_type<T>::type
97  >::type
98 min(const T& in)
99 {
100  return in.minCoeff();
101 }
102 
103 template <typename T>
104 struct has_size<T, typename Antioch::enable_if_c<is_eigen<T>::value,void>::type>
105 {
106  const static bool value = true;
107 };
108 
109 template <typename T>
110 struct return_auto<T, typename Antioch::enable_if_c<is_eigen<T>::value,void>::type>
111 {
112  const static bool value = true;
113 };
114 
115 template <typename T>
116 struct size_type<T, typename Antioch::enable_if_c<is_eigen<T>::value,void>::type>
117 {
118  typedef typename T::Index type;
119 };
120 
121 template <
122  template <typename, int, int, int, int, int> class _Matrix,
123  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
124 >
125 struct value_type<_Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
126 {
127  typedef _Scalar type;
128 };
129 
130 template <
131  template <typename, int, int, int, int, int> class _Matrix,
132  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
133 >
134 struct raw_value_type<_Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
135 {
137 };
138 
139 template <
140  template <typename, int, int, int, int, int> class _Matrix,
141  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
142 >
143 inline
144 _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
145 zero_clone(const _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& ex)
146 {
147  // We can't just use setZero here with arbitrary _Scalar types
148  if (ex.size())
149  return
150  _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
151  (ex.rows(), ex.cols()).setConstant(zero_clone(ex[0]));
152 
153  return
154  _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
155  (ex.rows(), ex.cols());
156 }
157 
158 template <
159  template <typename, int, int, int, int, int> class _Matrix,
160  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols,
161  typename Scalar2
162 >
163 inline
164 void
165 zero_clone(_Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& output,
166  const _Matrix<Scalar2, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& ex)
167 {
168  // We can't just use setZero here with arbitrary _Scalar types
169  output.resize(ex.rows(), ex.cols());
170  output.setConstant(zero_clone(ex[0]));
171 }
172 
173 
174 template <
175  template <typename, int, int, int, int, int> class _Matrix,
176  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols,
177  typename Scalar
178 >
179 inline
180 _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
181 constant_clone(const _Matrix<_Scalar, _Rows, _Cols, _Options,
182  _MaxRows, _MaxCols>& ex,
183  const Scalar& value)
184 {
185  // We can't just use setZero here with arbitrary _Scalar types
186  if (ex.size())
187  return
188  _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
189  (ex.rows(), ex.cols()).setConstant(value);
190 
191  return
192  _Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
193  (ex.rows(), ex.cols());
194 }
195 
196 
197 template <
198  template <typename, int, int, int, int, int> class _Matrix,
199  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols,
200  typename Scalar
201 >
202 inline
203 void
204 constant_fill(_Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows,
205  _MaxCols>& output,
206  const Scalar& value)
207 {
208  output.fill(value);
209 }
210 
211 template <
212  template <typename, int, int, int, int, int> class _Matrix,
213  typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
214 >
215 inline
216 void
217 set_zero(_Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& a)
218 {
219  // We can't just use setZero here with arbitrary value_types
220  if (a.size())
221  a.setConstant (zero_clone(a[0]));
222 }
223 
224 /*
225 template <template <typename, int, int, int, int, int> class _Matrix,
226  typename T, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols,
227  typename VectorScalar
228 >
229 inline
230 _Matrix<T,_Rows,_Cols,_Options,_MaxRows,_MaxCols>
231  custom_clone(const _Matrix<T,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & /example/, const VectorScalar& values, const _Matrix<unsigned int,_Rows,_Cols,_Options,_MaxRows,_MaxCols> & indexes)
232 {
233  _Matrix<T,_Rows,_Cols,_Options,_MaxRows,_MaxCols> returnval;
234  returnval.resize(indexes.size());
235  for(std::size_t i = 0; i < indexes.size(); i++)
236  {
237  returnval[i] = values[indexes[i]];
238  }
239  return returnval;
240 }
241 */
242 
243 template <typename Condition, typename T1, typename T2>
244 inline
245 typename enable_if_c<
246  is_eigen<T1>::value &&
247  is_eigen<T2>::value,
248  typename state_type<T1>::type
249 >::type
251 const Condition& condition,
252 const T1& if_true,
253 const T2& if_false)
254 {
255  return condition.select(if_true, if_false);
256 }
257 
258 
259 template <typename VectorT,
260  template <typename , int, int, int, int, int> class _Matrix,
261  typename _UIntT, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols
262 >
263 inline
264 typename enable_if_c<
265  is_eigen<typename value_type<VectorT>::type>::value,
266  typename value_type<VectorT>::type
267 >::type
268 eval_index(const VectorT& vec, const _Matrix<_UIntT, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& index)
269 {
270  typename value_type<VectorT>::type returnval = vec[0];
271  for (unsigned int i=0; i != index.size(); ++i)
272  returnval[i] = vec[index[i]][i];
273  return returnval;
274 }
275 
276 template <typename T>
277 inline
279 {
280  return vec.all();
281 }
282 
283 template <typename T>
284 inline
286 {
287  return vec.any();
288 }
289 
290 
291 } // end namespace Antioch
292 
293 #endif //ANTIOCH_EIGEN_UTILS_H
bool conjunction_root(const T &vec, eigen_library_tag)
Definition: eigen_utils.h:278
bool disjunction_root(const T &vec, eigen_library_tag)
Definition: eigen_utils.h:285
enable_if_c< is_eigen< typename value_type< VectorT >::type >::value, typename value_type< VectorT >::type >::type eval_index(const VectorT &vec, const _Matrix< _UIntT, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &index)
Definition: eigen_utils.h:268
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > constant_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex, const Scalar &value)
Definition: eigen_utils.h:181
max(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
enable_if_c< is_eigen< T1 >::value &&is_eigen< T2 >::value, typename state_type< T1 >::type >::type if_else(const Condition &condition, const T1 &if_true, const T2 &if_false)
Definition: eigen_utils.h:250
void set_zero(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a)
Definition: eigen_utils.h:217
#define ANTIOCH_AUTOFUNC(Type, Expr)
void constant_fill(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &output, const Scalar &value)
Definition: eigen_utils.h:204
The parameters are reduced parameters.
typename _Scalar int _Rows int _Cols int _Options int _MaxRows int _MaxCols inline min(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
#define ANTIOCH_COMMA
void zero_clone(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &output, const _Matrix< Scalar2, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex)
Definition: eigen_utils.h:165
#define ANTIOCH_AUTO(Type)
a array().max(b.array())) template< template< typename

Generated on Thu Jul 7 2016 11:09:46 for antioch-0.4.0 by  doxygen 1.8.8