ublas_wrapper.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/util/ublas_wrapper.hpp
  4. [begin_description]
  5. Resizing for ublas::vector and ublas::matrix
  6. [end_description]
  7. Copyright 2011-2013 Mario Mulansky
  8. Copyright 2011-2013 Karsten Ahnert
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #ifndef BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED
  14. #define BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED
  15. #include <boost/type_traits/integral_constant.hpp>
  16. #include <boost/numeric/ublas/vector.hpp>
  17. #include <boost/numeric/ublas/matrix.hpp>
  18. #include <boost/numeric/ublas/lu.hpp>
  19. #include <boost/numeric/ublas/vector_expression.hpp>
  20. #include <boost/numeric/ublas/matrix_expression.hpp>
  21. #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
  22. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  23. #include <boost/numeric/odeint/util/is_resizeable.hpp>
  24. #include <boost/numeric/odeint/util/state_wrapper.hpp>
  25. /* extend ublas by a few operations */
  26. /* map norm_inf onto reduce( v , default_operations::maximum ) */
  27. namespace boost { namespace numeric { namespace odeint {
  28. template< typename T , typename A >
  29. struct vector_space_norm_inf< boost::numeric::ublas::vector<T,A> >
  30. {
  31. typedef T result_type;
  32. result_type operator()( const boost::numeric::ublas::vector<T,A> &x ) const
  33. {
  34. return boost::numeric::ublas::norm_inf( x );
  35. }
  36. };
  37. template< class T , class L , class A >
  38. struct vector_space_norm_inf< boost::numeric::ublas::matrix<T,L,A> >
  39. {
  40. typedef T result_type;
  41. result_type operator()( const boost::numeric::ublas::matrix<T,L,A> &x ) const
  42. {
  43. return boost::numeric::ublas::norm_inf( x );
  44. }
  45. };
  46. } } }
  47. /* additional operations:
  48. * abs( v )
  49. * v / w
  50. * a + v
  51. */
  52. namespace boost { namespace numeric { namespace ublas {
  53. // elementwise abs - calculates absolute values of the elements
  54. template<class T>
  55. struct scalar_abs: public scalar_unary_functor<T> {
  56. typedef typename scalar_unary_functor<T>::value_type value_type;
  57. typedef typename scalar_unary_functor<T>::argument_type argument_type;
  58. typedef typename scalar_unary_functor<T>::result_type result_type;
  59. static BOOST_UBLAS_INLINE
  60. result_type apply (argument_type t) {
  61. using std::abs;
  62. return abs (t);
  63. }
  64. };
  65. // (abs v) [i] = abs (v [i])
  66. template<class E>
  67. BOOST_UBLAS_INLINE
  68. typename vector_unary_traits<E, scalar_abs<typename E::value_type> >::result_type
  69. abs (const vector_expression<E> &e) {
  70. typedef typename vector_unary_traits<E, scalar_abs<typename E::value_type> >::expression_type expression_type;
  71. return expression_type (e ());
  72. }
  73. // (abs m) [i] = abs (m [i])
  74. template<class E>
  75. BOOST_UBLAS_INLINE
  76. typename matrix_unary1_traits<E, scalar_abs<typename E::value_type> >::result_type
  77. abs (const matrix_expression<E> &e) {
  78. typedef typename matrix_unary1_traits<E, scalar_abs<typename E::value_type> >::expression_type expression_type;
  79. return expression_type (e ());
  80. }
  81. // elementwise division (v1 / v2) [i] = v1 [i] / v2 [i]
  82. template<class E1, class E2>
  83. BOOST_UBLAS_INLINE
  84. typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  85. typename E2::value_type> >::result_type
  86. operator / (const vector_expression<E1> &e1,
  87. const vector_expression<E2> &e2) {
  88. typedef typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  89. typename E2::value_type> >::expression_type expression_type;
  90. return expression_type (e1 (), e2 ());
  91. }
  92. // elementwise division (m1 / m2) [i] = m1 [i] / m2 [i]
  93. template<class E1, class E2>
  94. BOOST_UBLAS_INLINE
  95. typename matrix_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  96. typename E2::value_type> >::result_type
  97. operator / (const matrix_expression<E1> &e1,
  98. const matrix_expression<E2> &e2) {
  99. typedef typename matrix_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
  100. typename E2::value_type> >::expression_type expression_type;
  101. return expression_type (e1 (), e2 ());
  102. }
  103. // addition with scalar
  104. // (t + v) [i] = t + v [i]
  105. template<class T1, class E2>
  106. BOOST_UBLAS_INLINE
  107. typename enable_if< is_convertible<T1, typename E2::value_type >,
  108. typename vector_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::result_type
  109. >::type
  110. operator + (const T1 &e1,
  111. const vector_expression<E2> &e2) {
  112. typedef typename vector_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::expression_type expression_type;
  113. return expression_type (e1, e2 ());
  114. }
  115. // addition with scalar
  116. // (t + m) [i] = t + m [i]
  117. template<class T1, class E2>
  118. BOOST_UBLAS_INLINE
  119. typename enable_if< is_convertible<T1, typename E2::value_type >,
  120. typename matrix_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::result_type
  121. >::type
  122. operator + (const T1 &e1,
  123. const matrix_expression<E2> &e2) {
  124. typedef typename matrix_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::expression_type expression_type;
  125. return expression_type (e1, e2 ());
  126. }
  127. } } }
  128. /* add resize functionality */
  129. namespace boost {
  130. namespace numeric {
  131. namespace odeint {
  132. /*
  133. * resizeable specialization for boost::numeric::ublas::vector
  134. */
  135. template< class T , class A >
  136. struct is_resizeable< boost::numeric::ublas::vector< T , A > >
  137. {
  138. typedef boost::true_type type;
  139. const static bool value = type::value;
  140. };
  141. /*
  142. * resizeable specialization for boost::numeric::ublas::matrix
  143. */
  144. template< class T , class L , class A >
  145. struct is_resizeable< boost::numeric::ublas::matrix< T , L , A > >
  146. {
  147. typedef boost::true_type type;
  148. const static bool value = type::value;
  149. };
  150. /*
  151. * resizeable specialization for boost::numeric::ublas::permutation_matrix
  152. */
  153. template< class T , class A >
  154. struct is_resizeable< boost::numeric::ublas::permutation_matrix< T , A > >
  155. {
  156. typedef boost::true_type type;
  157. const static bool value = type::value;
  158. };
  159. // specialization for ublas::matrix
  160. // same size and resize specialization for matrix-matrix resizing
  161. template< class T , class L , class A , class T2 , class L2 , class A2 >
  162. struct same_size_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::matrix< T2 , L2 , A2 > >
  163. {
  164. static bool same_size( const boost::numeric::ublas::matrix< T , L , A > &m1 ,
  165. const boost::numeric::ublas::matrix< T2 , L2 , A2 > &m2 )
  166. {
  167. return ( ( m1.size1() == m2.size1() ) && ( m1.size2() == m2.size2() ) );
  168. }
  169. };
  170. template< class T , class L , class A , class T2 , class L2 , class A2 >
  171. struct resize_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::matrix< T2 , L2 , A2 > >
  172. {
  173. static void resize( boost::numeric::ublas::matrix< T , L , A > &m1 ,
  174. const boost::numeric::ublas::matrix< T2 , L2 , A2 > &m2 )
  175. {
  176. m1.resize( m2.size1() , m2.size2() );
  177. }
  178. };
  179. // same size and resize specialization for matrix-vector resizing
  180. template< class T , class L , class A , class T_V , class A_V >
  181. struct same_size_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::vector< T_V , A_V > >
  182. {
  183. static bool same_size( const boost::numeric::ublas::matrix< T , L , A > &m ,
  184. const boost::numeric::ublas::vector< T_V , A_V > &v )
  185. {
  186. return ( ( m.size1() == v.size() ) && ( m.size2() == v.size() ) );
  187. }
  188. };
  189. template< class T , class L , class A , class T_V , class A_V >
  190. struct resize_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::vector< T_V , A_V > >
  191. {
  192. static void resize( boost::numeric::ublas::matrix< T , L , A > &m ,
  193. const boost::numeric::ublas::vector< T_V , A_V > &v )
  194. {
  195. m.resize( v.size() , v.size() );
  196. }
  197. };
  198. // specialization for ublas::permutation_matrix
  199. // same size and resize specialization for matrix-vector resizing
  200. template< class T , class A , class T_V , class A_V >
  201. struct same_size_impl< boost::numeric::ublas::permutation_matrix< T , A > ,
  202. boost::numeric::ublas::vector< T_V , A_V > >
  203. {
  204. static bool same_size( const boost::numeric::ublas::permutation_matrix< T , A > &m ,
  205. const boost::numeric::ublas::vector< T_V , A_V > &v )
  206. {
  207. return ( m.size() == v.size() ); // && ( m.size2() == v.size() ) );
  208. }
  209. };
  210. template< class T , class A , class T_V , class A_V >
  211. struct resize_impl< boost::numeric::ublas::vector< T_V , A_V > ,
  212. boost::numeric::ublas::permutation_matrix< T , A > >
  213. {
  214. static void resize( const boost::numeric::ublas::vector< T_V , A_V > &v,
  215. boost::numeric::ublas::permutation_matrix< T , A > &m )
  216. {
  217. m.resize( v.size() , v.size() );
  218. }
  219. };
  220. template< class T , class A >
  221. struct state_wrapper< boost::numeric::ublas::permutation_matrix< T , A > > // with resizing
  222. {
  223. typedef boost::numeric::ublas::permutation_matrix< T , A > state_type;
  224. typedef state_wrapper< state_type > state_wrapper_type;
  225. state_type m_v;
  226. state_wrapper() : m_v( 1 ) // permutation matrix constructor requires a size, choose 1 as default
  227. { }
  228. };
  229. } } }
  230. #endif // BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED