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