antioch-0.4.0
valarray_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_VALARRAY_UTILS_H
28 #define ANTIOCH_VALARRAY_UTILS_H
29 
30 #ifdef ANTIOCH_METAPROGRAMMING_H
31 # ifndef ANTIOCH_VALARRAY_UTILS_DECL_H
32 # error valarray_utils_decl.h must be included before metaprogramming.h
33 # endif
34 #endif
35 
36 // Antioch
38 
39 // C++
40 #include <cmath>
41 #include <iostream>
42 #include <valarray>
43 
44 // Add some overloads that are blatantly missing from the std:: namespace
45 
46 namespace std
47 {
48 
49 template <typename T>
50 inline
51 typename Antioch::enable_if_c<
53  std::ostream&>::type
54 operator<< (std::ostream& output, const T& a)
55 {
56  output << '{';
57  const std::size_t size = a.size();
58  if (size)
59  output << a[0];
60  for (std::size_t i=1; i<size; ++i)
61  output << ',' << a[i];
62  output << '}';
63  return output;
64 }
65 
66 template <typename T, typename T2>
67 inline
68 typename Antioch::enable_if_c<
69  Antioch::is_valarray<T>::value,
70  typename Antioch::state_type<T>::type>::type
71 pow (const T& in, const T2& n)
72 {
73  typename Antioch::state_type<T>::type out=in;
74  const size_t size = in.size();
75  for (size_t i=0; i != size; ++i)
76  out[i] = pow(in[i], n);
77  return out;
78 }
79 
80 template <typename T>
81 inline
82 std::valarray<T>
83 max (const std::valarray<T>& a, const std::valarray<T>& b)
84 {
85  using std::max;
86 
87  const size_t size = a.size();
88  std::valarray<T> out(size);
89  for (size_t i=0; i != size; ++i)
90  out[i] = max(a[i], b[i]);
91  return out;
92 }
93 
94 
95 template <typename T>
96 inline
97 std::valarray<T>
98 min (const std::valarray<T>& a, const std::valarray<T>& b)
99 {
100  using std::min;
101 
102  const size_t size = a.size();
103  std::valarray<T> out(size);
104  for (size_t i=0; i != size; ++i)
105  out[i] = min(a[i], b[i]);
106  return out;
107 }
108 
109 
110 } // end namespace std
111 
112 
113 namespace Antioch
114 {
115 
116 template <typename T>
117 inline
118 T
119 max (const std::valarray<T>& in)
120 {
121  return in.max();
122 }
123 
124 template <typename T>
125 inline
126 T
127 min (const std::valarray<T>& in)
128 {
129  return in.min();
130 }
131 
132 template <typename T>
133 struct return_auto<T, typename Antioch::enable_if_c<is_valarray<T>::value,void>::type>
134 {
135  static const bool value = false;
136 };
137 
138 template <typename T>
139 struct has_size<std::valarray<T> >
140 {
141  static const bool value = true;
142 };
143 
144 template <typename T>
145 struct size_type<std::valarray<T> >
146 {
147  typedef std::size_t type;
148 };
149 
150 template <typename T>
151 struct value_type<std::valarray<T> >
152 {
153  typedef T type;
154 };
155 
156 template <typename T>
157 struct raw_value_type<std::valarray<T> >
158 {
159  typedef typename raw_value_type<T>::type type;
160 };
161 
162 template <typename T>
163 inline
164 std::valarray<T>
165 zero_clone(const std::valarray<T>& example)
166 {
167  if (example.size())
168  return std::valarray<T>(zero_clone(example[0]),example.size());
169  else
170  return std::valarray<T>();
171 }
172 
173 template <typename T1, typename T2>
174 inline
175 void
176 zero_clone(std::valarray<T1>& output, const std::valarray<T2>& example)
177 {
178  const std::size_t sz = example.size();
179  output.resize(sz);
180  for (std::size_t i=0; i != sz; ++i)
181  Antioch::zero_clone(output[i], example[i]);
182 }
183 
184 template <typename T, typename Scalar>
185 inline
186 std::valarray<T>
187 constant_clone(const std::valarray<T>& example, const Scalar& value)
188 {
189  if (example.size())
190  return std::valarray<T>(value,example.size());
191  else
192  return std::valarray<T>();
193 }
194 
195 
196 
197 template <typename T>
198 inline
199 void
200 init_clone(std::valarray<T>& output, const std::valarray<T>& example)
201 {
202  const std::size_t sz = example.size();
203  output.resize(sz);
204  for (std::size_t i=0; i != sz; ++i)
205  init_clone(output[i], example[i]);
206 }
207 
208 /*
209 template <typename T, typename VectorScalar>
210 inline
211 std::valarray<T> custom_clone(const std::valarray<T>& example, const VectorScalar& values, const std::valarray<unsigned int> & indexes)
212 {
213  std::valarray<T> returnval;
214  returnval.resize(indexes.size());
215  for(std::size_t i = 0; i < indexes.size(); i++)
216  {
217  returnval[i] = values[indexes[i]];
218  }
219  return returnval;
220 }
221 */
222 
223 template <typename T>
224 inline
225 std::valarray<T>
226 if_else(const std::valarray<bool>& condition,
227  const std::valarray<T>& if_true,
228  const std::valarray<T>& if_false)
229 {
230  antioch_assert_equal_to(condition.size(), if_true.size());
231  antioch_assert_equal_to(condition.size(), if_false.size());
232 
233  const std::size_t size = condition.size();
234  std::valarray<T> returnval(size);
235 
236  for (std::size_t i=0; i != size; ++i)
237  returnval[i] = condition[i] ? if_true[i] : if_false[i];
238 
239  return returnval;
240 }
241 
242 
243 template <typename VectorT>
244 inline
245 typename Antioch::enable_if_c<
247  typename value_type<VectorT>::type
248 >::type
249 eval_index(const VectorT& vec, const std::valarray<unsigned int>& index)
250 {
251  typename value_type<VectorT>::type returnval;
252  std::size_t sz = index.size();
253  returnval.resize(sz);
254  for (std::size_t i=0; i != sz; ++i)
255  returnval[i] = vec[index[i]][i];
256  return returnval;
257 }
258 
259 
260 } // end namespace Antioch
261 
262 
263 #endif //ANTIOCH_VALARRAY_UTILS_H
Antioch::enable_if_c< is_eigen< T >::value, typename value_type< T >::type >::type max(const T &in)
Definition: eigen_utils.h:88
#define antioch_assert_equal_to(expr1, expr2)
Antioch::enable_if_c< Antioch::is_valarray< T >::value, std::ostream & >::type operator<<(std::ostream &output, const T &a)
Antioch::enable_if_c< is_eigen< T >::value, typename value_type< T >::type >::type min(const T &in)
Definition: eigen_utils.h:98
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
void init_clone(T &output, const T &example)
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
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 >
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > zero_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex)
Definition: eigen_utils.h:145

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