valarray.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file valarray.hpp
  3. ///
  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)
  7. #ifndef BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
  8. #define BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005
  9. #ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
  10. # error Include this file before boost/accumulators/numeric/functional.hpp
  11. #endif
  12. #include <valarray>
  13. #include <functional>
  14. #include <boost/assert.hpp>
  15. #include <boost/mpl/and.hpp>
  16. #include <boost/mpl/not.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/type_traits/is_scalar.hpp>
  21. #include <boost/type_traits/remove_const.hpp>
  22. #include <boost/typeof/std/valarray.hpp>
  23. #include <boost/accumulators/numeric/functional_fwd.hpp>
  24. namespace boost { namespace numeric
  25. {
  26. namespace operators
  27. {
  28. namespace acc_detail
  29. {
  30. template<typename Fun>
  31. struct make_valarray
  32. {
  33. typedef std::valarray<typename Fun::result_type> type;
  34. };
  35. }
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // Handle valarray<Left> / Right where Right is a scalar and Right != Left.
  38. template<typename Left, typename Right>
  39. typename lazy_enable_if<
  40. mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
  41. , acc_detail::make_valarray<functional::divides<Left, Right> >
  42. >::type
  43. operator /(std::valarray<Left> const &left, Right const &right)
  44. {
  45. typedef typename functional::divides<Left, Right>::result_type value_type;
  46. std::valarray<value_type> result(left.size());
  47. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  48. {
  49. result[i] = numeric::divides(left[i], right);
  50. }
  51. return result;
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////
  54. // Handle valarray<Left> * Right where Right is a scalar and Right != Left.
  55. template<typename Left, typename Right>
  56. typename lazy_enable_if<
  57. mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >
  58. , acc_detail::make_valarray<functional::multiplies<Left, Right> >
  59. >::type
  60. operator *(std::valarray<Left> const &left, Right const &right)
  61. {
  62. typedef typename functional::multiplies<Left, Right>::result_type value_type;
  63. std::valarray<value_type> result(left.size());
  64. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  65. {
  66. result[i] = numeric::multiplies(left[i], right);
  67. }
  68. return result;
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Handle valarray<Left> + valarray<Right> where Right != Left.
  72. template<typename Left, typename Right>
  73. typename lazy_disable_if<
  74. is_same<Left, Right>
  75. , acc_detail::make_valarray<functional::plus<Left, Right> >
  76. >::type
  77. operator +(std::valarray<Left> const &left, std::valarray<Right> const &right)
  78. {
  79. typedef typename functional::plus<Left, Right>::result_type value_type;
  80. std::valarray<value_type> result(left.size());
  81. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  82. {
  83. result[i] = numeric::plus(left[i], right[i]);
  84. }
  85. return result;
  86. }
  87. }
  88. namespace functional
  89. {
  90. struct std_valarray_tag;
  91. template<typename T>
  92. struct tag<std::valarray<T> >
  93. {
  94. typedef std_valarray_tag type;
  95. };
  96. #ifdef __GLIBCXX__
  97. template<typename T, typename U>
  98. struct tag<std::_Expr<T, U> >
  99. {
  100. typedef std_valarray_tag type;
  101. };
  102. #endif
  103. /// INTERNAL ONLY
  104. ///
  105. // This is necessary because the GCC stdlib uses expression templates, and
  106. // typeof(som-valarray-expression) is not an instance of std::valarray
  107. #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(Name, Op) \
  108. template<typename Left, typename Right> \
  109. struct Name<Left, Right, std_valarray_tag, std_valarray_tag> \
  110. { \
  111. typedef Left first_argument_type; \
  112. typedef Right second_argument_type; \
  113. typedef typename Left::value_type left_value_type; \
  114. typedef typename Right::value_type right_value_type; \
  115. typedef \
  116. std::valarray< \
  117. typename Name<left_value_type, right_value_type>::result_type \
  118. > \
  119. result_type; \
  120. result_type \
  121. operator ()(Left &left, Right &right) const \
  122. { \
  123. return numeric::promote<std::valarray<left_value_type> >(left) \
  124. Op numeric::promote<std::valarray<right_value_type> >(right); \
  125. } \
  126. }; \
  127. template<typename Left, typename Right> \
  128. struct Name<Left, Right, std_valarray_tag, void> \
  129. { \
  130. typedef Left first_argument_type; \
  131. typedef Right second_argument_type; \
  132. typedef typename Left::value_type left_value_type; \
  133. typedef \
  134. std::valarray< \
  135. typename Name<left_value_type, Right>::result_type \
  136. > \
  137. result_type; \
  138. result_type \
  139. operator ()(Left &left, Right &right) const \
  140. { \
  141. return numeric::promote<std::valarray<left_value_type> >(left) Op right;\
  142. } \
  143. }; \
  144. template<typename Left, typename Right> \
  145. struct Name<Left, Right, void, std_valarray_tag> \
  146. { \
  147. typedef Left first_argument_type; \
  148. typedef Right second_argument_type; \
  149. typedef typename Right::value_type right_value_type; \
  150. typedef \
  151. std::valarray< \
  152. typename Name<Left, right_value_type>::result_type \
  153. > \
  154. result_type; \
  155. result_type \
  156. operator ()(Left &left, Right &right) const \
  157. { \
  158. return left Op numeric::promote<std::valarray<right_value_type> >(right);\
  159. } \
  160. };
  161. BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(plus, +)
  162. BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(minus, -)
  163. BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(multiplies, *)
  164. BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(divides, /)
  165. BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(modulus, %)
  166. #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP
  167. ///////////////////////////////////////////////////////////////////////////////
  168. // element-wise min of std::valarray
  169. template<typename Left, typename Right>
  170. struct min_assign<Left, Right, std_valarray_tag, std_valarray_tag>
  171. {
  172. typedef Left first_argument_type;
  173. typedef Right second_argument_type;
  174. typedef void result_type;
  175. void operator ()(Left &left, Right &right) const
  176. {
  177. BOOST_ASSERT(left.size() == right.size());
  178. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  179. {
  180. if(numeric::less(right[i], left[i]))
  181. {
  182. left[i] = right[i];
  183. }
  184. }
  185. }
  186. };
  187. ///////////////////////////////////////////////////////////////////////////////
  188. // element-wise max of std::valarray
  189. template<typename Left, typename Right>
  190. struct max_assign<Left, Right, std_valarray_tag, std_valarray_tag>
  191. {
  192. typedef Left first_argument_type;
  193. typedef Right second_argument_type;
  194. typedef void result_type;
  195. void operator ()(Left &left, Right &right) const
  196. {
  197. BOOST_ASSERT(left.size() == right.size());
  198. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  199. {
  200. if(numeric::greater(right[i], left[i]))
  201. {
  202. left[i] = right[i];
  203. }
  204. }
  205. }
  206. };
  207. // partial specialization of numeric::fdiv<> for std::valarray.
  208. template<typename Left, typename Right, typename RightTag>
  209. struct fdiv<Left, Right, std_valarray_tag, RightTag>
  210. : mpl::if_<
  211. are_integral<typename Left::value_type, Right>
  212. , divides<Left, double const>
  213. , divides<Left, Right>
  214. >::type
  215. {};
  216. // promote
  217. template<typename To, typename From>
  218. struct promote<To, From, std_valarray_tag, std_valarray_tag>
  219. {
  220. typedef From argument_type;
  221. typedef To result_type;
  222. To operator ()(From &arr) const
  223. {
  224. typename remove_const<To>::type res(arr.size());
  225. for(std::size_t i = 0, size = arr.size(); i != size; ++i)
  226. {
  227. res[i] = numeric::promote<typename To::value_type>(arr[i]);
  228. }
  229. return res;
  230. }
  231. };
  232. template<typename ToFrom>
  233. struct promote<ToFrom, ToFrom, std_valarray_tag, std_valarray_tag>
  234. {
  235. typedef ToFrom argument_type;
  236. typedef ToFrom result_type;
  237. ToFrom &operator ()(ToFrom &tofrom) const
  238. {
  239. return tofrom;
  240. }
  241. };
  242. // for "promoting" a std::valarray<bool> to a bool, useful for
  243. // comparing 2 valarrays for equality:
  244. // if(numeric::promote<bool>(a == b))
  245. template<typename From>
  246. struct promote<bool, From, void, std_valarray_tag>
  247. {
  248. typedef From argument_type;
  249. typedef bool result_type;
  250. bool operator ()(From &arr) const
  251. {
  252. BOOST_MPL_ASSERT((is_same<bool, typename From::value_type>));
  253. for(std::size_t i = 0, size = arr.size(); i != size; ++i)
  254. {
  255. if(!arr[i])
  256. {
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. };
  263. template<typename From>
  264. struct promote<bool const, From, void, std_valarray_tag>
  265. : promote<bool, From, void, std_valarray_tag>
  266. {};
  267. ///////////////////////////////////////////////////////////////////////////////
  268. // functional::as_min
  269. template<typename T>
  270. struct as_min<T, std_valarray_tag>
  271. {
  272. typedef T argument_type;
  273. typedef typename remove_const<T>::type result_type;
  274. typename remove_const<T>::type operator ()(T &arr) const
  275. {
  276. return 0 == arr.size()
  277. ? T()
  278. : T(numeric::as_min(arr[0]), arr.size());
  279. }
  280. };
  281. ///////////////////////////////////////////////////////////////////////////////
  282. // functional::as_max
  283. template<typename T>
  284. struct as_max<T, std_valarray_tag>
  285. {
  286. typedef T argument_type;
  287. typedef typename remove_const<T>::type result_type;
  288. typename remove_const<T>::type operator ()(T &arr) const
  289. {
  290. return 0 == arr.size()
  291. ? T()
  292. : T(numeric::as_max(arr[0]), arr.size());
  293. }
  294. };
  295. ///////////////////////////////////////////////////////////////////////////////
  296. // functional::as_zero
  297. template<typename T>
  298. struct as_zero<T, std_valarray_tag>
  299. {
  300. typedef T argument_type;
  301. typedef typename remove_const<T>::type result_type;
  302. typename remove_const<T>::type operator ()(T &arr) const
  303. {
  304. return 0 == arr.size()
  305. ? T()
  306. : T(numeric::as_zero(arr[0]), arr.size());
  307. }
  308. };
  309. ///////////////////////////////////////////////////////////////////////////////
  310. // functional::as_one
  311. template<typename T>
  312. struct as_one<T, std_valarray_tag>
  313. {
  314. typedef T argument_type;
  315. typedef typename remove_const<T>::type result_type;
  316. typename remove_const<T>::type operator ()(T &arr) const
  317. {
  318. return 0 == arr.size()
  319. ? T()
  320. : T(numeric::as_one(arr[0]), arr.size());
  321. }
  322. };
  323. } // namespace functional
  324. }} // namespace boost::numeric
  325. #endif