1//
2// Copyright (c) 2000-2002
3// Joerg Walter, Mathias Koch
4//
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)
8//
9// The authors gratefully acknowledge the support of
10// GeNeSys mbH & Co. KG in producing this work.
11//
12
13#include "test3.hpp"
14
15// Test matrix expression templates
16template<class M, std::size_t N>
17struct test_my_matrix {
18 typedef typename M::value_type value_type;
19
20 template<class MP>
21 void test_with (MP &m1, MP &m2, MP &m3) const {
22 {
23 value_type t;
24
25 // Default Construct
26 default_construct<MP>::test ();
27
28 // Copy and swap
29 initialize_matrix (m1);
30 initialize_matrix (m2);
31 m1 = m2;
32 std::cout << "m1 = m2 = " << m1 << std::endl;
33 m1.assign_temporary (m2);
34 std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl;
35 m1.swap (m2);
36 std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl;
37
38 // Zero assignment
39 m1 = ublas::zero_matrix<short> (m1.size1 (), m1.size2 ());
40 std::cout << "m1.zero_matrix = " << m1 << std::endl;
41 m1 = m2;
42
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;
52#endif
53
54 // Unary matrix operations resulting in a matrix
55 initialize_matrix (m1);
56 m2 = - m1;
57 std::cout << "- m1 = " << m2 << std::endl;
58 m2 = ublas::conj (m1);
59 std::cout << "conj (m1) = " << m2 << std::endl;
60
61 // Binary matrix operations resulting in a matrix
62 initialize_matrix (m1);
63 initialize_matrix (m2);
64 initialize_matrix (m3);
65 m3 = m1 + m2;
66 std::cout << "m1 + m2 = " << m3 << std::endl;
67 m3 = m1 - m2;
68 std::cout << "m1 - m2 = " << m3 << std::endl;
69
70 // Scaling a matrix
71 t = N;
72 initialize_matrix (m1);
73 m2 = value_type (1.) * m1;
74 std::cout << "1. * m1 = " << m2 << std::endl;
75 m2 = t * m1;
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;
80 m2 = m1 * t;
81 std::cout << "m1 * N = " << m2 << std::endl;
82
83 // Some assignments
84 initialize_matrix (m1);
85 initialize_matrix (m2);
86 m2 += m1;
87 std::cout << "m2 += m1 = " << m2 << std::endl;
88 m2 -= m1;
89 std::cout << "m2 -= m1 = " << m2 << std::endl;
90 m2 = m2 + m1;
91 std::cout << "m2 = m2 + m1 = " << m2 << std::endl;
92 m2 = m2 - m1;
93 std::cout << "m2 = m2 - m1 = " << m2 << std::endl;
94 m1 *= value_type (1.);
95 std::cout << "m1 *= 1. = " << m1 << std::endl;
96 m1 *= t;
97 std::cout << "m1 *= N = " << m1 << std::endl;
98
99 // Transpose
100 initialize_matrix (m1);
101 m2 = ublas::trans (m1);
102 std::cout << "trans (m1) = " << m2 << std::endl;
103
104 // Hermitean
105 initialize_matrix (m1);
106 m2 = ublas::herm (m1);
107 std::cout << "herm (m1) = " << m2 << std::endl;
108
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;
114 }
115 }
116 void operator () () const {
117 {
118 M m1 (N, N, N * N), m2 (N, N, N * N), m3 (N, N, N * N);
119 test_with (m1, m2, m3);
120
121#ifdef USE_RANGE
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);
126#endif
127
128#ifdef USE_SLICE
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);
133#endif
134 }
135 }
136};
137
138// Test matrix
139void test_matrix () {
140 std::cout << "test_matrix" << std::endl;
141
142#ifdef USE_SPARSE_MATRIX
143#ifdef USE_MAP_ARRAY
144#ifdef USE_FLOAT
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 > () ();
147#endif
148
149#ifdef USE_DOUBLE
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 > () ();
152#endif
153
154#ifdef USE_STD_COMPLEX
155#ifdef USE_FLOAT
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 > () ();
158#endif
159
160#ifdef USE_DOUBLE
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 > () ();
163#endif
164#endif
165#endif
166
167#ifdef USE_STD_MAP
168#ifdef USE_FLOAT
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 > () ();
171#endif
172
173#ifdef USE_DOUBLE
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 > () ();
176#endif
177
178#ifdef USE_STD_COMPLEX
179#ifdef USE_FLOAT
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 > () ();
182#endif
183
184#ifdef USE_DOUBLE
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 > () ();
187#endif
188#endif
189#endif
190#endif
191
192#ifdef USE_SPARSE_VECTOR_OF_SPARSE_VECTOR
193#ifdef USE_MAP_ARRAY
194#ifdef USE_FLOAT
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 > () ();
197#endif
198
199#ifdef USE_DOUBLE
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 > () ();
202#endif
203
204#ifdef USE_STD_COMPLEX
205#ifdef USE_FLOAT
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 > () ();
208#endif
209
210#ifdef USE_DOUBLE
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 > () ();
213#endif
214#endif
215#endif
216
217#ifdef USE_STD_MAP
218#ifdef USE_FLOAT
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 > () ();
221#endif
222
223#ifdef USE_DOUBLE
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 > () ();
226#endif
227
228#ifdef USE_STD_COMPLEX
229#ifdef USE_FLOAT
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 > () ();
232#endif
233
234#ifdef USE_DOUBLE
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 > () ();
237#endif
238#endif
239#endif
240#endif
241
242#ifdef USE_GENERALIZED_VECTOR_OF_VECTOR
243#ifdef USE_MAP_ARRAY
244#ifdef USE_FLOAT
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 > () ();
248#endif
249
250#ifdef USE_DOUBLE
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 > () ();
254#endif
255
256#ifdef USE_STD_COMPLEX
257#ifdef USE_FLOAT
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 > () ();
261#endif
262
263#ifdef USE_DOUBLE
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 > () ();
267#endif
268#endif
269#endif
270
271#ifdef USE_STD_MAP
272#ifdef USE_FLOAT
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 > () ();
276#endif
277
278#ifdef USE_DOUBLE
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 > () ();
282#endif
283
284#ifdef USE_STD_COMPLEX
285#ifdef USE_FLOAT
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 > () ();
289#endif
290
291#ifdef USE_DOUBLE
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 > () ();
295#endif
296#endif
297#endif
298#endif
299
300#ifdef USE_COMPRESSED_MATRIX
301#ifdef USE_FLOAT
302 std::cout << "float compressed_matrix" << std::endl;
303 test_my_matrix<ublas::compressed_matrix<float>, 3 > () ();
304#endif
305
306#ifdef USE_DOUBLE
307 std::cout << "double compressed_matrix" << std::endl;
308 test_my_matrix<ublas::compressed_matrix<double>, 3 > () ();
309#endif
310
311#ifdef USE_STD_COMPLEX
312#ifdef USE_FLOAT
313 std::cout << "std::complex<float> compressed_matrix" << std::endl;
314 test_my_matrix<ublas::compressed_matrix<std::complex<float> >, 3 > () ();
315#endif
316
317#ifdef USE_DOUBLE
318 std::cout << "std::complex<double> compressed_matrix" << std::endl;
319 test_my_matrix<ublas::compressed_matrix<std::complex<double> >, 3 > () ();
320#endif
321#endif
322#endif
323
324#ifdef USE_COORDINATE_MATRIX
325#ifdef USE_FLOAT
326 std::cout << "float coordinate_matrix" << std::endl;
327 test_my_matrix<ublas::coordinate_matrix<float>, 3 > () ();
328#endif
329
330#ifdef USE_DOUBLE
331 std::cout << "double coordinate_matrix" << std::endl;
332 test_my_matrix<ublas::coordinate_matrix<double>, 3 > () ();
333#endif
334
335#ifdef USE_STD_COMPLEX
336#ifdef USE_FLOAT
337 std::cout << "std::complex<float> coordinate_matrix" << std::endl;
338 test_my_matrix<ublas::coordinate_matrix<std::complex<float> >, 3 > () ();
339#endif
340
341#ifdef USE_DOUBLE
342 std::cout << "std::complex<double> coordinate_matrix" << std::endl;
343 test_my_matrix<ublas::coordinate_matrix<std::complex<double> >, 3 > () ();
344#endif
345#endif
346#endif
347
348#ifdef USE_MAPPED_VECTOR_OF_MAPPED_VECTOR
349#ifdef USE_FLOAT
350 std::cout << "float mapped_vector_of_mapped_vector" << std::endl;
351 test_my_matrix<ublas::mapped_vector_of_mapped_vector<float>, 3 > () ();
352#endif
353
354#ifdef USE_DOUBLE
355 std::cout << "double mapped_vector_of_mapped_vector" << std::endl;
356 test_my_matrix<ublas::mapped_vector_of_mapped_vector<double>, 3 > () ();
357#endif
358
359#ifdef USE_STD_COMPLEX
360#ifdef USE_FLOAT
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 > () ();
363#endif
364
365#ifdef USE_DOUBLE
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 > () ();
368#endif
369#endif
370#endif
371}
372

source code of boost/libs/numeric/ublas/test/test33.cpp