1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2005 Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
9 #define BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
11 #ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
12 # error Include this file before boost/accumulators/numeric/functional.hpp
17 #include <boost/assert.hpp>
18 #include <boost/mpl/and.hpp>
19 #include <boost/mpl/not.hpp>
20 #include <boost/mpl/assert.hpp>
21 #include <boost/utility/enable_if.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/type_traits/is_scalar.hpp>
24 #include <boost/type_traits/remove_const.hpp>
25 #include <boost/typeof/std/valarray.hpp>
26 #include <boost/accumulators/numeric/functional_fwd.hpp>
28 namespace boost { namespace numeric
34 template<typename Fun>
37 typedef std::valarray<typename Fun::result_type> type;
41 ///////////////////////////////////////////////////////////////////////////////
42 // Handle valarray<Left> / Right where Right is a scalar and Right != Left.
43 template<typename Left, typename Right>
44 typename lazy_enable_if<
45 mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
46 , acc_detail::make_valarray<functional::divides<Left, Right> >
48 operator /(std::valarray<Left> const &left, Right const &right)
50 typedef typename functional::divides<Left, Right>::result_type value_type;
51 std::valarray<value_type> result(left.size());
52 for(std::size_t i = 0, size = result.size(); i != size; ++i)
54 result[i] = numeric::divides(left[i], right);
59 ///////////////////////////////////////////////////////////////////////////////
60 // Handle valarray<Left> * Right where Right is a scalar and Right != Left.
61 template<typename Left, typename Right>
62 typename lazy_enable_if<
63 mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
64 , acc_detail::make_valarray<functional::multiplies<Left, Right> >
66 operator *(std::valarray<Left> const &left, Right const &right)
68 typedef typename functional::multiplies<Left, Right>::result_type value_type;
69 std::valarray<value_type> result(left.size());
70 for(std::size_t i = 0, size = result.size(); i != size; ++i)
72 result[i] = numeric::multiplies(left[i], right);
77 ///////////////////////////////////////////////////////////////////////////////
78 // Handle valarray<Left> + valarray<Right> where Right != Left.
79 template<typename Left, typename Right>
80 typename lazy_disable_if<
82 , acc_detail::make_valarray<functional::plus<Left, Right> >
84 operator +(std::valarray<Left> const &left, std::valarray<Right> const &right)
86 typedef typename functional::plus<Left, Right>::result_type value_type;
87 std::valarray<value_type> result(left.size());
88 for(std::size_t i = 0, size = result.size(); i != size; ++i)
90 result[i] = numeric::plus(left[i], right[i]);
98 struct std_valarray_tag;
101 struct tag<std::valarray<T> >
103 typedef std_valarray_tag type;
107 template<typename T, typename U>
108 struct tag<std::_Expr<T, U> >
110 typedef std_valarray_tag type;
116 // This is necessary because the GCC stdlib uses expression templates, and
117 // typeof(som-valarray-expression) is not an instance of std::valarray
118 #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(Name, Op) \
119 template<typename Left, typename Right> \
120 struct Name<Left, Right, std_valarray_tag, std_valarray_tag> \
122 typedef Left first_argument_type; \
123 typedef Right second_argument_type; \
124 typedef typename Left::value_type left_value_type; \
125 typedef typename Right::value_type right_value_type; \
128 typename Name<left_value_type, right_value_type>::result_type \
132 operator ()(Left &left, Right &right) const \
134 return numeric::promote<std::valarray<left_value_type> >(left) \
135 Op numeric::promote<std::valarray<right_value_type> >(right); \
138 template<typename Left, typename Right> \
139 struct Name<Left, Right, std_valarray_tag, void> \
141 typedef Left first_argument_type; \
142 typedef Right second_argument_type; \
143 typedef typename Left::value_type left_value_type; \
146 typename Name<left_value_type, Right>::result_type \
150 operator ()(Left &left, Right &right) const \
152 return numeric::promote<std::valarray<left_value_type> >(left) Op right;\
155 template<typename Left, typename Right> \
156 struct Name<Left, Right, void, std_valarray_tag> \
158 typedef Left first_argument_type; \
159 typedef Right second_argument_type; \
160 typedef typename Right::value_type right_value_type; \
163 typename Name<Left, right_value_type>::result_type \
167 operator ()(Left &left, Right &right) const \
169 return left Op numeric::promote<std::valarray<right_value_type> >(right);\
173 BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(plus, +)
174 BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(minus, -)
175 BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(multiplies, *)
176 BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(divides, /)
177 BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(modulus, %)
179 #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP
181 ///////////////////////////////////////////////////////////////////////////////
182 // element-wise min of std::valarray
183 template<typename Left, typename Right>
184 struct min_assign<Left, Right, std_valarray_tag, std_valarray_tag>
186 typedef Left first_argument_type;
187 typedef Right second_argument_type;
188 typedef void result_type;
190 void operator ()(Left &left, Right &right) const
192 BOOST_ASSERT(left.size() == right.size());
193 for(std::size_t i = 0, size = left.size(); i != size; ++i)
195 if(numeric::less(right[i], left[i]))
203 ///////////////////////////////////////////////////////////////////////////////
204 // element-wise max of std::valarray
205 template<typename Left, typename Right>
206 struct max_assign<Left, Right, std_valarray_tag, std_valarray_tag>
208 typedef Left first_argument_type;
209 typedef Right second_argument_type;
210 typedef void result_type;
212 void operator ()(Left &left, Right &right) const
214 BOOST_ASSERT(left.size() == right.size());
215 for(std::size_t i = 0, size = left.size(); i != size; ++i)
217 if(numeric::greater(right[i], left[i]))
225 // partial specialization of numeric::fdiv<> for std::valarray.
226 template<typename Left, typename Right, typename RightTag>
227 struct fdiv<Left, Right, std_valarray_tag, RightTag>
229 are_integral<typename Left::value_type, Right>
230 , divides<Left, double const>
231 , divides<Left, Right>
236 template<typename To, typename From>
237 struct promote<To, From, std_valarray_tag, std_valarray_tag>
239 typedef From argument_type;
240 typedef To result_type;
242 To operator ()(From &arr) const
244 typename remove_const<To>::type res(arr.size());
245 for(std::size_t i = 0, size = arr.size(); i != size; ++i)
247 res[i] = numeric::promote<typename To::value_type>(arr[i]);
253 template<typename ToFrom>
254 struct promote<ToFrom, ToFrom, std_valarray_tag, std_valarray_tag>
256 typedef ToFrom argument_type;
257 typedef ToFrom result_type;
259 ToFrom &operator ()(ToFrom &tofrom) const
265 // for "promoting" a std::valarray<bool> to a bool, useful for
266 // comparing 2 valarrays for equality:
267 // if(numeric::promote<bool>(a == b))
268 template<typename From>
269 struct promote<bool, From, void, std_valarray_tag>
271 typedef From argument_type;
272 typedef bool result_type;
274 bool operator ()(From &arr) const
276 BOOST_MPL_ASSERT((is_same<bool, typename From::value_type>));
277 for(std::size_t i = 0, size = arr.size(); i != size; ++i)
288 template<typename From>
289 struct promote<bool const, From, void, std_valarray_tag>
290 : promote<bool, From, void, std_valarray_tag>
293 ///////////////////////////////////////////////////////////////////////////////
294 // functional::as_min
296 struct as_min<T, std_valarray_tag>
298 typedef T argument_type;
299 typedef typename remove_const<T>::type result_type;
301 typename remove_const<T>::type operator ()(T &arr) const
303 return 0 == arr.size()
305 : T(numeric::as_min(arr[0]), arr.size());
309 ///////////////////////////////////////////////////////////////////////////////
310 // functional::as_max
312 struct as_max<T, std_valarray_tag>
314 typedef T argument_type;
315 typedef typename remove_const<T>::type result_type;
317 typename remove_const<T>::type operator ()(T &arr) const
319 return 0 == arr.size()
321 : T(numeric::as_max(arr[0]), arr.size());
325 ///////////////////////////////////////////////////////////////////////////////
326 // functional::as_zero
328 struct as_zero<T, std_valarray_tag>
330 typedef T argument_type;
331 typedef typename remove_const<T>::type result_type;
333 typename remove_const<T>::type operator ()(T &arr) const
335 return 0 == arr.size()
337 : T(numeric::as_zero(arr[0]), arr.size());
341 ///////////////////////////////////////////////////////////////////////////////
342 // functional::as_one
344 struct as_one<T, std_valarray_tag>
346 typedef T argument_type;
347 typedef typename remove_const<T>::type result_type;
349 typename remove_const<T>::type operator ()(T &arr) const
351 return 0 == arr.size()
353 : T(numeric::as_one(arr[0]), arr.size());
357 } // namespace functional
359 }} // namespace boost::numeric