]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/numeric/ublas/doc/samples/assignment_examples.cpp
2 // Copyright (c) 2010 Athanasios Iliopoulos
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
9 #include <boost/numeric/ublas/assignment.hpp>
10 #include <boost/numeric/ublas/vector.hpp>
11 #include <boost/numeric/ublas/vector_proxy.hpp>
12 #include <boost/numeric/ublas/matrix_proxy.hpp>
13 #include <boost/numeric/ublas/vector_sparse.hpp>
14 #include <boost/numeric/ublas/matrix_sparse.hpp>
15 #include <boost/numeric/ublas/io.hpp>
16 #include <boost/numeric/ublas/matrix.hpp>
18 using namespace boost :: numeric :: ublas
;
24 std :: cout
<< a
<< std :: endl
;
30 std :: cout
<< b
<< std :: endl
;
34 matrix
< double > A ( 3 , 3 );
38 std :: cout
<< A
<< std :: endl
;
47 std :: cout
<< A
<< std :: endl
;
52 // Matrix from vector - column assignment
53 A
<<= move ( 0 , 2 ), traverse_policy :: by_column (),
55 std :: cout
<< A
<< std :: endl
;
60 // Another matrix from vector example (watch the wraping);
61 vector
< double > c ( 9 ); c
<<= 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ;
63 std :: cout
<< A
<< std :: endl
;
68 // If for performance(Benchmarks are not definite about that) or consistency reasons you need to disable wraping:
69 static next_row_manip endr
; //This can be defined globally
70 A
<<= traverse_policy :: by_row_no_wrap (),
77 // If by default you need to disable wraping define
78 // BOOST_UBLAS_DEFAULT_NO_WRAP_POLICY, in the compilation options,
79 // so that you avoid typing the "traverse_policy::by_row_no_wrap()".
81 // Plus and minus assign:
82 A
<<= fill_policy :: index_plus_assign (),
84 std :: cout
<< A
<< std :: endl
;
91 project ( b
, range ( 3 , 6 )),
93 std :: cout
<< A
<< std :: endl
;
99 matrix
< double > B ( 6 , 6 );
102 std :: cout
<< B
<< std :: endl
;
106 // Matrix range (vector is similar)
107 B
= zero_matrix
< double >( 6 , 6 );
108 matrix_range
< matrix
< double > > mrB ( B
, range ( 1 , 4 ), range ( 1 , 4 ));
109 mrB
<<= 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ;
110 std :: cout
<< B
<< std :: endl
;
118 // Horizontal concatenation can be achieved using this trick:
119 matrix
< double > BH ( 3 , 9 );
121 std :: cout
<< BH
<< std :: endl
;
124 // Vertical concatenation can be achieved using this trick:
125 matrix
< double > BV ( 9 , 3 );
129 std :: cout
<< BV
<< std :: endl
;
134 // Watch the difference when assigning matrices for different traverse policies:
135 matrix
< double > BR ( 9 , 9 , 0 );
136 BR
<<= traverse_policy :: by_row (), // This is the default, so this might as well be omitted.
138 std :: cout
<< BR
<< std :: endl
;
143 matrix
< double > BC ( 9 , 9 , 0 );
144 BC
<<= traverse_policy :: by_column (),
146 std :: cout
<< BC
<< std :: endl
;
151 // The following will throw a run-time exception in debug mode (matrix mid-assignment wrap is not allowed) :
152 // matrix<double> C(7,7);
155 // Matrix from matrix with index manipulators
156 matrix
< double > C ( 6 , 6 , 0 );
157 C
<<= A
, move ( 3 , 0 ), A
;
161 // A faster way for to construct this dense matrix.
162 matrix
< double > D ( 6 , 6 );
163 D
<<= A
, zero_matrix
< double >( 3 , 3 ),
164 zero_matrix
< double >( 3 , 3 ), A
;
168 // The next_row and next_column index manipulators:
169 // note: next_row and next_column functions return
170 // a next_row_manip and and next_column_manip object.
171 // This is the manipulator we used earlier when we disabled
173 matrix
< double > E ( 2 , 4 , 0 );
174 E
<<= 1 , 2 , next_row (),
175 3 , 4 , next_column (), 5 ;
176 std :: cout
<< E
<< std :: endl
;
180 // The begin1 (moves to the begining of the column) index manipulator, begin2 does the same for the row:
181 matrix
< double > F ( 2 , 4 , 0 );
182 F
<<= 1 , 2 , next_row (),
184 std :: cout
<< F
<< std :: endl
;
188 // The move (relative) and move_to(absolute) index manipulators (probably the most useful manipulators):
189 matrix
< double > G ( 2 , 4 , 0 );
190 G
<<= 1 , 2 , move ( 0 , 1 ), 3 ,
192 std :: cout
<< G
<< std :: endl
;
196 // Static equivallents (faster) when sizes are known at compile time:
197 matrix
< double > Gs ( 2 , 4 , 0 );
198 Gs
<<= 1 , 2 , move
< 0 , 1 >(), 3 ,
200 std :: cout
<< Gs
<< std :: endl
;
204 // Choice of traverse policy (default is "row by row" traverse):
206 matrix
< double > H ( 2 , 4 , 0 );
209 std :: cout
<< H
<< std :: endl
;
213 H
<<= traverse_policy :: by_column (),
216 std :: cout
<< H
<< std :: endl
;
220 // traverse policy can be changed mid assignment if desired.
221 matrix
< double > H1 ( 4 , 4 , 0 );
222 H1
<<= 1 , 2 , 3 , traverse_policy :: by_column (), 1 , 2 , 3 ;
224 std :: cout
<< H
<< std :: endl
;
230 // note: fill_policy and traverse_policy are namespaces, so you can use them
231 // by a using statement.
233 // For compressed and coordinate matrix types a push_back or insert fill policy can be chosen for faster assginment:
234 compressed_matrix
< double > I ( 2 , 2 );
235 I
<<= fill_policy :: sparse_push_back (),
237 std :: cout
<< I
<< std :: endl
;
241 coordinate_matrix
< double > J ( 2 , 2 );
242 J
<<= fill_policy :: sparse_insert (),
244 std :: cout
<< J
<< std :: endl
;
248 // A sparse matrix from another matrix works as with other types.
249 coordinate_matrix
< double > K ( 3 , 3 );
250 K
<<= fill_policy :: sparse_insert (),
252 std :: cout
<< K
<< std :: endl
;
257 // Be careful this will not work:
258 //compressed_matrix<double> J2(4,4);
259 //J2<<=fill_policy::sparse_push_back(),
261 // That's because the second J2's elements
262 // are attempted to be assigned at positions
263 // that come before the elements already pushed.
264 // Unfortunatelly that's the only thing you can do in this case
265 // (or of course make a custom agorithm):
266 compressed_matrix
< double > J2 ( 4 , 4 );
267 J2
<<= fill_policy :: sparse_push_back (),
268 J
, fill_policy :: sparse_insert (),
271 std :: cout
<< J2
<< std :: endl
;
276 // A different traverse policy doesn't change the result, only they order it is been assigned.
277 coordinate_matrix
< double > L ( 3 , 3 );
278 L
<<= fill_policy :: sparse_insert (), traverse_policy :: by_column (),
280 std :: cout
<< L
<< std :: endl
;
281 // (same as previous)
286 typedef coordinate_matrix
< double >:: size_type cmst
;
287 const cmst size
= 30 ;
288 //typedef fill_policy::sparse_push_back spb;
289 // Although the above could have been used the following is may be faster if
290 // you use the policy often and for relatively small containers.
291 static fill_policy :: sparse_push_back spb
;
293 // A block diagonal sparse using a loop:
294 compressed_matrix
< double > M ( size
, size
, 4 * 15 );
295 for ( cmst i
= 0 ; i
!= size
; i
+= J
. size1 ())
296 M
<<= spb
, move_to ( i
, i
), J
;
299 // If typedef was used above the last expression should start
300 // with M <<= spb()...
302 // Displaying so that blocks can be easily seen:
303 for ( unsigned int i
= 0 ; i
!= M
. size1 (); i
++) {
305 for ( unsigned int j
= 1 ; j
!= M
. size2 (); j
++) std :: cout
<< ", " << M ( i
, j
);
314 // A "repeat" trasverser may by provided so that this becomes faster and an on-liner like:
315 // M <<= spb, repeat(0, size, J.size1(), 0, size, J.size1()), J;
316 // An alternate would be to create a :repeater" matrix and vector expression that can be used in other places as well. The latter is probably better,