3 boost/numeric/odeint/util/ublas_wrapper.hpp
6 Resizing for ublas::vector and ublas::matrix
9 Copyright 2011-2013 Mario Mulansky
10 Copyright 2011-2013 Karsten Ahnert
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
18 #ifndef BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED
19 #define BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED
22 #include <boost/type_traits/integral_constant.hpp>
23 #include <boost/numeric/ublas/vector.hpp>
24 #include <boost/numeric/ublas/matrix.hpp>
25 #include <boost/numeric/ublas/lu.hpp>
26 #include <boost/numeric/ublas/vector_expression.hpp>
27 #include <boost/numeric/ublas/matrix_expression.hpp>
29 #include <boost/numeric/odeint/algebra/vector_space_algebra.hpp>
30 #include <boost/numeric/odeint/algebra/default_operations.hpp>
32 #include <boost/numeric/odeint/util/is_resizeable.hpp>
33 #include <boost/numeric/odeint/util/state_wrapper.hpp>
36 /* extend ublas by a few operations */
38 /* map norm_inf onto reduce( v , default_operations::maximum ) */
39 namespace boost { namespace numeric { namespace odeint {
41 template< typename T , typename A >
42 struct vector_space_norm_inf< boost::numeric::ublas::vector<T,A> >
44 typedef T result_type;
46 result_type operator()( const boost::numeric::ublas::vector<T,A> &x ) const
48 return boost::numeric::ublas::norm_inf( x );
53 template< class T , class L , class A >
54 struct vector_space_norm_inf< boost::numeric::ublas::matrix<T,L,A> >
56 typedef T result_type;
58 result_type operator()( const boost::numeric::ublas::matrix<T,L,A> &x ) const
60 return boost::numeric::ublas::norm_inf( x );
65 /* additional operations:
70 namespace boost { namespace numeric { namespace ublas {
73 // elementwise abs - calculates absolute values of the elements
75 struct scalar_abs: public scalar_unary_functor<T> {
76 typedef typename scalar_unary_functor<T>::value_type value_type;
77 typedef typename scalar_unary_functor<T>::argument_type argument_type;
78 typedef typename scalar_unary_functor<T>::result_type result_type;
80 static BOOST_UBLAS_INLINE
81 result_type apply (argument_type t) {
88 // (abs v) [i] = abs (v [i])
91 typename vector_unary_traits<E, scalar_abs<typename E::value_type> >::result_type
92 abs (const vector_expression<E> &e) {
93 typedef typename vector_unary_traits<E, scalar_abs<typename E::value_type> >::expression_type expression_type;
94 return expression_type (e ());
97 // (abs m) [i] = abs (m [i])
100 typename matrix_unary1_traits<E, scalar_abs<typename E::value_type> >::result_type
101 abs (const matrix_expression<E> &e) {
102 typedef typename matrix_unary1_traits<E, scalar_abs<typename E::value_type> >::expression_type expression_type;
103 return expression_type (e ());
107 // elementwise division (v1 / v2) [i] = v1 [i] / v2 [i]
108 template<class E1, class E2>
110 typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
111 typename E2::value_type> >::result_type
112 operator / (const vector_expression<E1> &e1,
113 const vector_expression<E2> &e2) {
114 typedef typename vector_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
115 typename E2::value_type> >::expression_type expression_type;
116 return expression_type (e1 (), e2 ());
120 // elementwise division (m1 / m2) [i] = m1 [i] / m2 [i]
121 template<class E1, class E2>
123 typename matrix_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
124 typename E2::value_type> >::result_type
125 operator / (const matrix_expression<E1> &e1,
126 const matrix_expression<E2> &e2) {
127 typedef typename matrix_binary_traits<E1, E2, scalar_divides<typename E1::value_type,
128 typename E2::value_type> >::expression_type expression_type;
129 return expression_type (e1 (), e2 ());
132 // addition with scalar
133 // (t + v) [i] = t + v [i]
134 template<class T1, class E2>
136 typename enable_if< is_convertible<T1, typename E2::value_type >,
137 typename vector_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::result_type
139 operator + (const T1 &e1,
140 const vector_expression<E2> &e2) {
141 typedef typename vector_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::expression_type expression_type;
142 return expression_type (e1, e2 ());
145 // addition with scalar
146 // (t + m) [i] = t + m [i]
147 template<class T1, class E2>
149 typename enable_if< is_convertible<T1, typename E2::value_type >,
150 typename matrix_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::result_type
152 operator + (const T1 &e1,
153 const matrix_expression<E2> &e2) {
154 typedef typename matrix_binary_scalar1_traits<const T1, E2, scalar_plus<T1, typename E2::value_type> >::expression_type expression_type;
155 return expression_type (e1, e2 ());
163 /* add resize functionality */
169 * resizeable specialization for boost::numeric::ublas::vector
171 template< class T , class A >
172 struct is_resizeable< boost::numeric::ublas::vector< T , A > >
174 typedef boost::true_type type;
175 const static bool value = type::value;
180 * resizeable specialization for boost::numeric::ublas::matrix
182 template< class T , class L , class A >
183 struct is_resizeable< boost::numeric::ublas::matrix< T , L , A > >
185 typedef boost::true_type type;
186 const static bool value = type::value;
191 * resizeable specialization for boost::numeric::ublas::permutation_matrix
193 template< class T , class A >
194 struct is_resizeable< boost::numeric::ublas::permutation_matrix< T , A > >
196 typedef boost::true_type type;
197 const static bool value = type::value;
201 // specialization for ublas::matrix
202 // same size and resize specialization for matrix-matrix resizing
203 template< class T , class L , class A , class T2 , class L2 , class A2 >
204 struct same_size_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::matrix< T2 , L2 , A2 > >
206 static bool same_size( const boost::numeric::ublas::matrix< T , L , A > &m1 ,
207 const boost::numeric::ublas::matrix< T2 , L2 , A2 > &m2 )
209 return ( ( m1.size1() == m2.size1() ) && ( m1.size2() == m2.size2() ) );
213 template< class T , class L , class A , class T2 , class L2 , class A2 >
214 struct resize_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::matrix< T2 , L2 , A2 > >
216 static void resize( boost::numeric::ublas::matrix< T , L , A > &m1 ,
217 const boost::numeric::ublas::matrix< T2 , L2 , A2 > &m2 )
219 m1.resize( m2.size1() , m2.size2() );
225 // same size and resize specialization for matrix-vector resizing
226 template< class T , class L , class A , class T_V , class A_V >
227 struct same_size_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::vector< T_V , A_V > >
229 static bool same_size( const boost::numeric::ublas::matrix< T , L , A > &m ,
230 const boost::numeric::ublas::vector< T_V , A_V > &v )
232 return ( ( m.size1() == v.size() ) && ( m.size2() == v.size() ) );
236 template< class T , class L , class A , class T_V , class A_V >
237 struct resize_impl< boost::numeric::ublas::matrix< T , L , A > , boost::numeric::ublas::vector< T_V , A_V > >
239 static void resize( boost::numeric::ublas::matrix< T , L , A > &m ,
240 const boost::numeric::ublas::vector< T_V , A_V > &v )
242 m.resize( v.size() , v.size() );
248 // specialization for ublas::permutation_matrix
249 // same size and resize specialization for matrix-vector resizing
250 template< class T , class A , class T_V , class A_V >
251 struct same_size_impl< boost::numeric::ublas::permutation_matrix< T , A > ,
252 boost::numeric::ublas::vector< T_V , A_V > >
254 static bool same_size( const boost::numeric::ublas::permutation_matrix< T , A > &m ,
255 const boost::numeric::ublas::vector< T_V , A_V > &v )
257 return ( m.size() == v.size() ); // && ( m.size2() == v.size() ) );
261 template< class T , class A , class T_V , class A_V >
262 struct resize_impl< boost::numeric::ublas::vector< T_V , A_V > ,
263 boost::numeric::ublas::permutation_matrix< T , A > >
265 static void resize( const boost::numeric::ublas::vector< T_V , A_V > &v,
266 boost::numeric::ublas::permutation_matrix< T , A > &m )
268 m.resize( v.size() , v.size() );
278 template< class T , class A >
279 struct state_wrapper< boost::numeric::ublas::permutation_matrix< T , A > > // with resizing
281 typedef boost::numeric::ublas::permutation_matrix< T , A > state_type;
282 typedef state_wrapper< state_type > state_wrapper_type;
286 state_wrapper() : m_v( 1 ) // permutation matrix constructor requires a size, choose 1 as default
297 #endif // BOOST_NUMERIC_ODEINT_UTIL_UBLAS_WRAPPER_HPP_INCLUDED