2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
15 // Test matrix expression templates
16 template<class M
, std::size_t N
>
17 struct test_my_matrix
{
18 typedef typename
M::value_type value_type
;
21 void test_with (MP
&m1
, MP
&m2
, MP
&m3
) const {
26 default_construct
<MP
>::test ();
29 initialize_matrix (m1
);
30 initialize_matrix (m2
);
32 std::cout
<< "m1 = m2 = " << m1
<< std::endl
;
33 m1
.assign_temporary (m2
);
34 std::cout
<< "m1.assign_temporary (m2) = " << m1
<< std::endl
;
36 std::cout
<< "m1.swap (m2) = " << m1
<< " " << m2
<< std::endl
;
39 m1
= ublas::zero_matrix
<> (m1
.size1 (), m1
.size2 ());
40 std::cout
<< "m1.zero_matrix = " << m1
<< std::endl
;
43 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
44 // Project range and slice
45 initialize_matrix (m1
);
46 initialize_matrix (m2
);
47 project (m1
, ublas::range(0,1),ublas::range(0,1)) = project (m2
, ublas::range(0,1),ublas::range(0,1));
48 project (m1
, ublas::range(0,1),ublas::range(0,1)) = project (m2
, ublas::slice(0,1,1),ublas::slice(0,1,1));
49 project (m1
, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2
, ublas::slice(0,1,2),ublas::slice(0,1,2));
50 project (m1
, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2
, ublas::range(0,2),ublas::range(0,2));
51 std::cout
<< "m1 = range/slice " << m1
<< std::endl
;
54 // Unary matrix operations resulting in a matrix
55 initialize_matrix (m1
);
57 std::cout
<< "- m1 = " << m2
<< std::endl
;
58 m2
= ublas::conj (m1
);
59 std::cout
<< "conj (m1) = " << m2
<< std::endl
;
61 // Binary matrix operations resulting in a matrix
62 initialize_matrix (m1
);
63 initialize_matrix (m2
);
64 initialize_matrix (m3
);
66 std::cout
<< "m1 + m2 = " << m3
<< std::endl
;
68 std::cout
<< "m1 - m2 = " << m3
<< std::endl
;
72 initialize_matrix (m1
);
73 m2
= value_type (1.) * m1
;
74 std::cout
<< "1. * m1 = " << m2
<< std::endl
;
76 std::cout
<< "N * m1 = " << m2
<< std::endl
;
77 initialize_matrix (m1
);
78 m2
= m1
* value_type (1.);
79 std::cout
<< "m1 * 1. = " << m2
<< std::endl
;
81 std::cout
<< "m1 * N = " << m2
<< std::endl
;
84 initialize_matrix (m1
);
85 initialize_matrix (m2
);
87 std::cout
<< "m2 += m1 = " << m2
<< std::endl
;
89 std::cout
<< "m2 -= m1 = " << m2
<< std::endl
;
91 std::cout
<< "m2 = m2 + m1 = " << m2
<< std::endl
;
93 std::cout
<< "m2 = m2 - m1 = " << m2
<< std::endl
;
94 m1
*= value_type (1.);
95 std::cout
<< "m1 *= 1. = " << m1
<< std::endl
;
97 std::cout
<< "m1 *= N = " << m1
<< std::endl
;
100 initialize_matrix (m1
);
101 m2
= ublas::trans (m1
);
102 std::cout
<< "trans (m1) = " << m2
<< std::endl
;
105 initialize_matrix (m1
);
106 m2
= ublas::herm (m1
);
107 std::cout
<< "herm (m1) = " << m2
<< std::endl
;
109 // Matrix multiplication
110 initialize_matrix (m1
);
111 initialize_matrix (m2
);
112 m3
= ublas::prod (m1
, m2
);
113 std::cout
<< "prod (m1, m2) = " << m3
<< std::endl
;
116 void operator () () const {
118 M
m1 (N
, N
, N
* N
), m2 (N
, N
, N
* N
), m3 (N
, N
, N
* N
);
119 test_with (m1
, m2
, m3
);
122 ublas::matrix_range
<M
> mr1 (m1
, ublas::range (0, N
), ublas::range (0, N
)),
123 mr2 (m2
, ublas::range (0, N
), ublas::range (0, N
)),
124 mr3 (m3
, ublas::range (0, N
), ublas::range (0, N
));
125 test_with (mr1
, mr2
, mr3
);
129 ublas::matrix_slice
<M
> ms1 (m1
, ublas::slice (0, 1, N
), ublas::slice (0, 1, N
)),
130 ms2 (m2
, ublas::slice (0, 1, N
), ublas::slice (0, 1, N
)),
131 ms3 (m3
, ublas::slice (0, 1, N
), ublas::slice (0, 1, N
));
132 test_with (ms1
, ms2
, ms3
);
139 void test_matrix () {
140 std::cout
<< "test_matrix" << std::endl
;
142 #ifdef USE_SPARSE_MATRIX
145 std::cout
<< "float, mapped_matrix map_array" << std::endl
;
146 test_my_matrix
<ublas::mapped_matrix
<float, ublas::row_major
, ublas::map_array
<std::size_t, float> >, 3 > () ();
150 std::cout
<< "double, mapped_matrix map_array" << std::endl
;
151 test_my_matrix
<ublas::mapped_matrix
<double, ublas::row_major
, ublas::map_array
<std::size_t, double> >, 3 > () ();
154 #ifdef USE_STD_COMPLEX
156 std::cout
<< "std::complex<float>, mapped_matrix map_array" << std::endl
;
157 test_my_matrix
<ublas::mapped_matrix
<std::complex<float>, ublas::row_major
, ublas::map_array
<std::size_t, std::complex<float> > >, 3 > () ();
161 std::cout
<< "std::complex<double>, mapped_matrix map_array" << std::endl
;
162 test_my_matrix
<ublas::mapped_matrix
<std::complex<double>, ublas::row_major
, ublas::map_array
<std::size_t, std::complex<double> > >, 3 > () ();
169 std::cout
<< "float, mapped_matrix std::map" << std::endl
;
170 test_my_matrix
<ublas::mapped_matrix
<float, ublas::row_major
, std::map
<std::size_t, float> >, 3 > () ();
174 std::cout
<< "double, mapped_matrix std::map" << std::endl
;
175 test_my_matrix
<ublas::mapped_matrix
<double, ublas::row_major
, std::map
<std::size_t, double> >, 3 > () ();
178 #ifdef USE_STD_COMPLEX
180 std::cout
<< "std::complex<float>, mapped_matrix std::map" << std::endl
;
181 test_my_matrix
<ublas::mapped_matrix
<std::complex<float>, ublas::row_major
, std::map
<std::size_t, std::complex<float> > >, 3 > () ();
185 std::cout
<< "std::complex<double>, mapped_matrix std::map" << std::endl
;
186 test_my_matrix
<ublas::mapped_matrix
<std::complex<double>, ublas::row_major
, std::map
<std::size_t, std::complex<double> > >, 3 > () ();
192 #ifdef USE_SPARSE_VECTOR_OF_SPARSE_VECTOR
195 std::cout
<< "float, mapped_vector_of_mapped_vector map_array" << std::endl
;
196 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<float, ublas::row_major
, ublas::map_array
<std::size_t, ublas::map_array
<std::size_t, float> > >, 3 > () ();
200 std::cout
<< "double, mapped_vector_of_mapped_vector map_array" << std::endl
;
201 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<double, ublas::row_major
, ublas::map_array
<std::size_t, ublas::map_array
<std::size_t, double> > >, 3 > () ();
204 #ifdef USE_STD_COMPLEX
206 std::cout
<< "std::complex<float>, mapped_vector_of_mapped_vector map_array" << std::endl
;
207 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<float>, ublas::row_major
, ublas::map_array
<std::size_t, ublas::map_array
<std::size_t, std::complex<float> > > >, 3 > () ();
211 std::cout
<< "std::complex<double>, mapped_vector_of_mapped_vectormap_array" << std::endl
;
212 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<double>, ublas::row_major
, ublas::map_array
<std::size_t, ublas::map_array
<std::size_t, std::complex<double> > > >, 3 > () ();
219 std::cout
<< "float, mapped_vector_of_mapped_vector std::map" << std::endl
;
220 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<float, ublas::row_major
, std::map
<std::size_t, std::map
<std::size_t, float> > >, 3 > () ();
224 std::cout
<< "double, mapped_vector_of_mapped_vector std::map" << std::endl
;
225 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<double, ublas::row_major
, std::map
<std::size_t, std::map
<std::size_t, double> > >, 3 > () ();
228 #ifdef USE_STD_COMPLEX
230 std::cout
<< "std::complex<float>, mapped_vector_of_mapped_vector std::map" << std::endl
;
231 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<float>, ublas::row_major
, std::map
<std::size_t, std::map
<std::size_t, std::complex<float> > > >, 3 > () ();
235 std::cout
<< "std::complex<double>, mapped_vector_of_mapped_vector std::map" << std::endl
;
236 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<double>, ublas::row_major
, std::map
<std::size_t, std::map
<std::size_t, std::complex<double> > > >, 3 > () ();
242 #ifdef USE_GENERALIZED_VECTOR_OF_VECTOR
245 std::cout
<< "float,generalized_vector_of_vector map_array" << std::endl
;
246 test_my_matrix
<ublas::generalized_vector_of_vector
<float, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<float, ublas::map_array
<std::size_t, float> > > >, 3 > () ();
247 test_my_matrix
<ublas::generalized_vector_of_vector
<float, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<float, ublas::map_array
<std::size_t, float> >, ublas::map_array
<std::size_t, ublas::mapped_vector
<float, ublas::map_array
<std::size_t, float> > > > >, 3 > () ();
251 std::cout
<< "double, generalized_vector_of_vector map_array" << std::endl
;
252 test_my_matrix
<ublas::generalized_vector_of_vector
<double, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<double, ublas::map_array
<std::size_t, double> > > >, 3 > () ();
253 test_my_matrix
<ublas::generalized_vector_of_vector
<double, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<double, ublas::map_array
<std::size_t, double> >, ublas::map_array
<std::size_t, ublas::mapped_vector
<double, ublas::map_array
<std::size_t, double> > > > >, 3 > () ();
256 #ifdef USE_STD_COMPLEX
258 std::cout
<< "std::complex<float>, generalized_vector_of_vector map_array" << std::endl
;
259 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<float>, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<std::complex<float>, ublas::map_array
<std::size_t, std::complex<float> > > > >, 3 > () ();
260 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<float>, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<std::complex<float>, ublas::map_array
<std::size_t, std::complex<float> > >, ublas::map_array
<std::size_t, ublas::mapped_vector
<std::complex<float>, ublas::map_array
<std::size_t, std::complex<float> > > > > >, 3 > () ();
264 std::cout
<< "std::complex<double>, generalized_vector_of_vector map_array" << std::endl
;
265 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<double>, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<std::complex<double>, ublas::map_array
<std::size_t, std::complex<double> > > > >, 3 > () ();
266 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<double>, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<std::complex<double>, ublas::map_array
<std::size_t, std::complex<double> > >, ublas::map_array
<std::size_t, ublas::mapped_vector
<std::complex<double>, ublas::map_array
<std::size_t, std::complex<double> > > > > >, 3 > () ();
273 std::cout
<< "float, generalized_vector_of_vector std::map" << std::endl
;
274 test_my_matrix
<ublas::generalized_vector_of_vector
<float, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<float, std::map
<std::size_t, float> > > >, 3 > () ();
275 test_my_matrix
<ublas::generalized_vector_of_vector
<float, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<float, std::map
<std::size_t, float> >, std::map
<std::size_t, ublas::mapped_vector
<float, std::map
<std::size_t, float> > > > >, 3 > () ();
279 std::cout
<< "double, generalized_vector_of_vector std::map" << std::endl
;
280 test_my_matrix
<ublas::generalized_vector_of_vector
<double, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<double, std::map
<std::size_t, double> > > >, 3 > () ();
281 test_my_matrix
<ublas::generalized_vector_of_vector
<double, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<double, std::map
<std::size_t, double> >, std::map
<std::size_t, ublas::mapped_vector
<double, std::map
<std::size_t, double> > > > >, 3 > () ();
284 #ifdef USE_STD_COMPLEX
286 std::cout
<< "std::complex<float>, generalized_vector_of_vector std::map" << std::endl
;
287 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<float>, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<std::complex<float>, std::map
<std::size_t, std::complex<float> > > > >, 3 > () ();
288 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<float>, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<std::complex<float>, std::map
<std::size_t, std::complex<float> > >, std::map
<std::size_t, ublas::mapped_vector
<std::complex<float>, std::map
<std::size_t, std::complex<float> > > > > >, 3 > () ();
292 std::cout
<< "std::complex<double>, generalized_vector_of_vector std::map" << std::endl
;
293 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<double>, ublas::row_major
, ublas::vector
<ublas::mapped_vector
<std::complex<double>, std::map
<std::size_t, std::complex<double> > > > >, 3 > () ();
294 test_my_matrix
<ublas::generalized_vector_of_vector
<std::complex<double>, ublas::row_major
, ublas::mapped_vector
<ublas::mapped_vector
<std::complex<double>, std::map
<std::size_t, std::complex<double> > >, std::map
<std::size_t, ublas::mapped_vector
<std::complex<double>, std::map
<std::size_t, std::complex<double> > > > > >, 3 > () ();
300 #ifdef USE_COMPRESSED_MATRIX
302 std::cout
<< "float compressed_matrix" << std::endl
;
303 test_my_matrix
<ublas::compressed_matrix
<float>, 3 > () ();
307 std::cout
<< "double compressed_matrix" << std::endl
;
308 test_my_matrix
<ublas::compressed_matrix
<double>, 3 > () ();
311 #ifdef USE_STD_COMPLEX
313 std::cout
<< "std::complex<float> compressed_matrix" << std::endl
;
314 test_my_matrix
<ublas::compressed_matrix
<std::complex<float> >, 3 > () ();
318 std::cout
<< "std::complex<double> compressed_matrix" << std::endl
;
319 test_my_matrix
<ublas::compressed_matrix
<std::complex<double> >, 3 > () ();
324 #ifdef USE_COORDINATE_MATRIX
326 std::cout
<< "float coordinate_matrix" << std::endl
;
327 test_my_matrix
<ublas::coordinate_matrix
<float>, 3 > () ();
331 std::cout
<< "double coordinate_matrix" << std::endl
;
332 test_my_matrix
<ublas::coordinate_matrix
<double>, 3 > () ();
335 #ifdef USE_STD_COMPLEX
337 std::cout
<< "std::complex<float> coordinate_matrix" << std::endl
;
338 test_my_matrix
<ublas::coordinate_matrix
<std::complex<float> >, 3 > () ();
342 std::cout
<< "std::complex<double> coordinate_matrix" << std::endl
;
343 test_my_matrix
<ublas::coordinate_matrix
<std::complex<double> >, 3 > () ();
348 #ifdef USE_MAPPED_VECTOR_OF_MAPPED_VECTOR
350 std::cout
<< "float mapped_vector_of_mapped_vector" << std::endl
;
351 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<float>, 3 > () ();
355 std::cout
<< "double mapped_vector_of_mapped_vector" << std::endl
;
356 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<double>, 3 > () ();
359 #ifdef USE_STD_COMPLEX
361 std::cout
<< "std::complex<float> mapped_vector_of_mapped_vector" << std::endl
;
362 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<float> >, 3 > () ();
366 std::cout
<< "std::complex<double> mapped_vector_of_mapped_vector" << std::endl
;
367 test_my_matrix
<ublas::mapped_vector_of_mapped_vector
<std::complex<double> >, 3 > () ();