1 | // |
---|
2 | // Copyright (c) 2000-2002 |
---|
3 | // Joerg Walter, Mathias Koch |
---|
4 | // |
---|
5 | // Permission to use, copy, modify, distribute and sell this software |
---|
6 | // and its documentation for any purpose is hereby granted without fee, |
---|
7 | // provided that the above copyright notice appear in all copies and |
---|
8 | // that both that copyright notice and this permission notice appear |
---|
9 | // in supporting documentation. The authors make no representations |
---|
10 | // about the suitability of this software for any purpose. |
---|
11 | // It is provided "as is" without express or implied warranty. |
---|
12 | // |
---|
13 | // The authors gratefully acknowledge the support of |
---|
14 | // GeNeSys mbH & Co. KG in producing this work. |
---|
15 | // |
---|
16 | |
---|
17 | #include "test3.hpp" |
---|
18 | |
---|
19 | // Test matrix expression templates |
---|
20 | template<class M, int N> |
---|
21 | struct test_my_matrix { |
---|
22 | typedef typename M::value_type value_type; |
---|
23 | |
---|
24 | template<class MP> |
---|
25 | void test_with (MP &m1, MP &m2, MP &m3) const { |
---|
26 | { |
---|
27 | value_type t; |
---|
28 | |
---|
29 | // Default Construct |
---|
30 | default_construct<MP>::test (); |
---|
31 | |
---|
32 | // Copy and swap |
---|
33 | initialize_matrix (m1); |
---|
34 | initialize_matrix (m2); |
---|
35 | m1 = m2; |
---|
36 | std::cout << "m1 = m2 = " << m1 << std::endl; |
---|
37 | m1.assign_temporary (m2); |
---|
38 | std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl; |
---|
39 | m1.swap (m2); |
---|
40 | std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl; |
---|
41 | |
---|
42 | // Zero assignment |
---|
43 | m1 = ublas::zero_matrix<> (m1.size1 (), m1.size2 ()); |
---|
44 | std::cout << "m1.zero_matrix = " << m1 << std::endl; |
---|
45 | m1 = m2; |
---|
46 | |
---|
47 | #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
48 | // Project range and slice |
---|
49 | initialize_matrix (m1); |
---|
50 | initialize_matrix (m2); |
---|
51 | project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::range(0,1),ublas::range(0,1)); |
---|
52 | project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::slice(0,1,1),ublas::slice(0,1,1)); |
---|
53 | project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::slice(0,1,2),ublas::slice(0,1,2)); |
---|
54 | project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::range(0,2),ublas::range(0,2)); |
---|
55 | std::cout << "m1 = range/slice " << m1 << std::endl; |
---|
56 | #endif |
---|
57 | |
---|
58 | // Unary matrix operations resulting in a matrix |
---|
59 | initialize_matrix (m1); |
---|
60 | m2 = - m1; |
---|
61 | std::cout << "- m1 = " << m2 << std::endl; |
---|
62 | m2 = ublas::conj (m1); |
---|
63 | std::cout << "conj (m1) = " << m2 << std::endl; |
---|
64 | |
---|
65 | // Binary matrix operations resulting in a matrix |
---|
66 | initialize_matrix (m1); |
---|
67 | initialize_matrix (m2); |
---|
68 | initialize_matrix (m3); |
---|
69 | m3 = m1 + m2; |
---|
70 | std::cout << "m1 + m2 = " << m3 << std::endl; |
---|
71 | m3 = m1 - m2; |
---|
72 | std::cout << "m1 - m2 = " << m3 << std::endl; |
---|
73 | |
---|
74 | // Scaling a matrix |
---|
75 | t = N; |
---|
76 | initialize_matrix (m1); |
---|
77 | m2 = value_type (1.) * m1; |
---|
78 | std::cout << "1. * m1 = " << m2 << std::endl; |
---|
79 | m2 = t * m1; |
---|
80 | std::cout << "N * m1 = " << m2 << std::endl; |
---|
81 | initialize_matrix (m1); |
---|
82 | m2 = m1 * value_type (1.); |
---|
83 | std::cout << "m1 * 1. = " << m2 << std::endl; |
---|
84 | m2 = m1 * t; |
---|
85 | std::cout << "m1 * N = " << m2 << std::endl; |
---|
86 | |
---|
87 | // Some assignments |
---|
88 | initialize_matrix (m1); |
---|
89 | initialize_matrix (m2); |
---|
90 | m2 += m1; |
---|
91 | std::cout << "m2 += m1 = " << m2 << std::endl; |
---|
92 | m2 -= m1; |
---|
93 | std::cout << "m2 -= m1 = " << m2 << std::endl; |
---|
94 | m2 = m2 + m1; |
---|
95 | std::cout << "m2 = m2 + m1 = " << m2 << std::endl; |
---|
96 | m2 = m2 - m1; |
---|
97 | std::cout << "m2 = m2 - m1 = " << m2 << std::endl; |
---|
98 | m1 *= value_type (1.); |
---|
99 | std::cout << "m1 *= 1. = " << m1 << std::endl; |
---|
100 | m1 *= t; |
---|
101 | std::cout << "m1 *= N = " << m1 << std::endl; |
---|
102 | |
---|
103 | // Transpose |
---|
104 | initialize_matrix (m1); |
---|
105 | m2 = ublas::trans (m1); |
---|
106 | std::cout << "trans (m1) = " << m2 << std::endl; |
---|
107 | |
---|
108 | // Hermitean |
---|
109 | initialize_matrix (m1); |
---|
110 | m2 = ublas::herm (m1); |
---|
111 | std::cout << "herm (m1) = " << m2 << std::endl; |
---|
112 | |
---|
113 | // Matrix multiplication |
---|
114 | initialize_matrix (m1); |
---|
115 | initialize_matrix (m2); |
---|
116 | m3 = ublas::prod (m1, m2); |
---|
117 | std::cout << "prod (m1, m2) = " << m3 << std::endl; |
---|
118 | } |
---|
119 | } |
---|
120 | void operator () () const { |
---|
121 | { |
---|
122 | M m1 (N, N, N * N), m2 (N, N, N * N), m3 (N, N, N * N); |
---|
123 | test_with (m1, m2, m3); |
---|
124 | |
---|
125 | #ifdef USE_RANGE |
---|
126 | ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)), |
---|
127 | mr2 (m2, ublas::range (0, N), ublas::range (0, N)), |
---|
128 | mr3 (m3, ublas::range (0, N), ublas::range (0, N)); |
---|
129 | test_with (mr1, mr2, mr3); |
---|
130 | #endif |
---|
131 | |
---|
132 | #ifdef USE_SLICE |
---|
133 | ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
134 | ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
135 | ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
136 | test_with (ms1, ms2, ms3); |
---|
137 | #endif |
---|
138 | } |
---|
139 | } |
---|
140 | }; |
---|
141 | |
---|
142 | // Test matrix |
---|
143 | void test_matrix () { |
---|
144 | std::cout << "test_matrix" << std::endl; |
---|
145 | |
---|
146 | #ifdef USE_SPARSE_MATRIX |
---|
147 | #ifdef USE_MAP_ARRAY |
---|
148 | #ifdef USE_FLOAT |
---|
149 | std::cout << "float, mapped_matrix map_array" << std::endl; |
---|
150 | test_my_matrix<ublas::mapped_matrix<float, ublas::row_major, ublas::map_array<std::size_t, float> >, 3 > () (); |
---|
151 | #endif |
---|
152 | |
---|
153 | #ifdef USE_DOUBLE |
---|
154 | std::cout << "double, mapped_matrix map_array" << std::endl; |
---|
155 | test_my_matrix<ublas::mapped_matrix<double, ublas::row_major, ublas::map_array<std::size_t, double> >, 3 > () (); |
---|
156 | #endif |
---|
157 | |
---|
158 | #ifdef USE_STD_COMPLEX |
---|
159 | #ifdef USE_FLOAT |
---|
160 | std::cout << "std::complex<float>, mapped_matrix map_array" << std::endl; |
---|
161 | test_my_matrix<ublas::mapped_matrix<std::complex<float>, ublas::row_major, ublas::map_array<std::size_t, std::complex<float> > >, 3 > () (); |
---|
162 | #endif |
---|
163 | |
---|
164 | #ifdef USE_DOUBLE |
---|
165 | std::cout << "std::complex<double>, mapped_matrix map_array" << std::endl; |
---|
166 | test_my_matrix<ublas::mapped_matrix<std::complex<double>, ublas::row_major, ublas::map_array<std::size_t, std::complex<double> > >, 3 > () (); |
---|
167 | #endif |
---|
168 | #endif |
---|
169 | #endif |
---|
170 | |
---|
171 | #ifdef USE_STD_MAP |
---|
172 | #ifdef USE_FLOAT |
---|
173 | std::cout << "float, mapped_matrix std::map" << std::endl; |
---|
174 | test_my_matrix<ublas::mapped_matrix<float, ublas::row_major, std::map<std::size_t, float> >, 3 > () (); |
---|
175 | #endif |
---|
176 | |
---|
177 | #ifdef USE_DOUBLE |
---|
178 | std::cout << "double, mapped_matrix std::map" << std::endl; |
---|
179 | test_my_matrix<ublas::mapped_matrix<double, ublas::row_major, std::map<std::size_t, double> >, 3 > () (); |
---|
180 | #endif |
---|
181 | |
---|
182 | #ifdef USE_STD_COMPLEX |
---|
183 | #ifdef USE_FLOAT |
---|
184 | std::cout << "std::complex<float>, mapped_matrix std::map" << std::endl; |
---|
185 | test_my_matrix<ublas::mapped_matrix<std::complex<float>, ublas::row_major, std::map<std::size_t, std::complex<float> > >, 3 > () (); |
---|
186 | #endif |
---|
187 | |
---|
188 | #ifdef USE_DOUBLE |
---|
189 | std::cout << "std::complex<double>, mapped_matrix std::map" << std::endl; |
---|
190 | test_my_matrix<ublas::mapped_matrix<std::complex<double>, ublas::row_major, std::map<std::size_t, std::complex<double> > >, 3 > () (); |
---|
191 | #endif |
---|
192 | #endif |
---|
193 | #endif |
---|
194 | #endif |
---|
195 | |
---|
196 | #ifdef USE_SPARSE_VECTOR_OF_SPARSE_VECTOR |
---|
197 | #ifdef USE_MAP_ARRAY |
---|
198 | #ifdef USE_FLOAT |
---|
199 | std::cout << "float, mapped_vector_of_mapped_vector map_array" << std::endl; |
---|
200 | 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 > () (); |
---|
201 | #endif |
---|
202 | |
---|
203 | #ifdef USE_DOUBLE |
---|
204 | std::cout << "double, mapped_vector_of_mapped_vector map_array" << std::endl; |
---|
205 | 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 > () (); |
---|
206 | #endif |
---|
207 | |
---|
208 | #ifdef USE_STD_COMPLEX |
---|
209 | #ifdef USE_FLOAT |
---|
210 | std::cout << "std::complex<float>, mapped_vector_of_mapped_vector map_array" << std::endl; |
---|
211 | 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 > () (); |
---|
212 | #endif |
---|
213 | |
---|
214 | #ifdef USE_DOUBLE |
---|
215 | std::cout << "std::complex<double>, mapped_vector_of_mapped_vectormap_array" << std::endl; |
---|
216 | 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 > () (); |
---|
217 | #endif |
---|
218 | #endif |
---|
219 | #endif |
---|
220 | |
---|
221 | #ifdef USE_STD_MAP |
---|
222 | #ifdef USE_FLOAT |
---|
223 | std::cout << "float, mapped_vector_of_mapped_vector std::map" << std::endl; |
---|
224 | 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 > () (); |
---|
225 | #endif |
---|
226 | |
---|
227 | #ifdef USE_DOUBLE |
---|
228 | std::cout << "double, mapped_vector_of_mapped_vector std::map" << std::endl; |
---|
229 | 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 > () (); |
---|
230 | #endif |
---|
231 | |
---|
232 | #ifdef USE_STD_COMPLEX |
---|
233 | #ifdef USE_FLOAT |
---|
234 | std::cout << "std::complex<float>, mapped_vector_of_mapped_vector std::map" << std::endl; |
---|
235 | 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 > () (); |
---|
236 | #endif |
---|
237 | |
---|
238 | #ifdef USE_DOUBLE |
---|
239 | std::cout << "std::complex<double>, mapped_vector_of_mapped_vector std::map" << std::endl; |
---|
240 | 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 > () (); |
---|
241 | #endif |
---|
242 | #endif |
---|
243 | #endif |
---|
244 | #endif |
---|
245 | |
---|
246 | #ifdef USE_GENERALIZED_VECTOR_OF_VECTOR |
---|
247 | #ifdef USE_MAP_ARRAY |
---|
248 | #ifdef USE_FLOAT |
---|
249 | std::cout << "float,generalized_vector_of_vector map_array" << std::endl; |
---|
250 | 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 > () (); |
---|
251 | 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 > () (); |
---|
252 | #endif |
---|
253 | |
---|
254 | #ifdef USE_DOUBLE |
---|
255 | std::cout << "double, generalized_vector_of_vector map_array" << std::endl; |
---|
256 | 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 > () (); |
---|
257 | 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 > () (); |
---|
258 | #endif |
---|
259 | |
---|
260 | #ifdef USE_STD_COMPLEX |
---|
261 | #ifdef USE_FLOAT |
---|
262 | std::cout << "std::complex<float>, generalized_vector_of_vector map_array" << std::endl; |
---|
263 | 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 > () (); |
---|
264 | 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 > () (); |
---|
265 | #endif |
---|
266 | |
---|
267 | #ifdef USE_DOUBLE |
---|
268 | std::cout << "std::complex<double>, generalized_vector_of_vector map_array" << std::endl; |
---|
269 | 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 > () (); |
---|
270 | 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 > () (); |
---|
271 | #endif |
---|
272 | #endif |
---|
273 | #endif |
---|
274 | |
---|
275 | #ifdef USE_STD_MAP |
---|
276 | #ifdef USE_FLOAT |
---|
277 | std::cout << "float, generalized_vector_of_vector std::map" << std::endl; |
---|
278 | 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 > () (); |
---|
279 | 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 > () (); |
---|
280 | #endif |
---|
281 | |
---|
282 | #ifdef USE_DOUBLE |
---|
283 | std::cout << "double, generalized_vector_of_vector std::map" << std::endl; |
---|
284 | 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 > () (); |
---|
285 | 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 > () (); |
---|
286 | #endif |
---|
287 | |
---|
288 | #ifdef USE_STD_COMPLEX |
---|
289 | #ifdef USE_FLOAT |
---|
290 | std::cout << "std::complex<float>, generalized_vector_of_vector std::map" << std::endl; |
---|
291 | 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 > () (); |
---|
292 | 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 > () (); |
---|
293 | #endif |
---|
294 | |
---|
295 | #ifdef USE_DOUBLE |
---|
296 | std::cout << "std::complex<double>, generalized_vector_of_vector std::map" << std::endl; |
---|
297 | 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 > () (); |
---|
298 | 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 > () (); |
---|
299 | #endif |
---|
300 | #endif |
---|
301 | #endif |
---|
302 | #endif |
---|
303 | |
---|
304 | #ifdef USE_COMPRESSED_MATRIX |
---|
305 | #ifdef USE_FLOAT |
---|
306 | std::cout << "float compressed_matrix" << std::endl; |
---|
307 | test_my_matrix<ublas::compressed_matrix<float>, 3 > () (); |
---|
308 | #endif |
---|
309 | |
---|
310 | #ifdef USE_DOUBLE |
---|
311 | std::cout << "double compressed_matrix" << std::endl; |
---|
312 | test_my_matrix<ublas::compressed_matrix<double>, 3 > () (); |
---|
313 | #endif |
---|
314 | |
---|
315 | #ifdef USE_STD_COMPLEX |
---|
316 | #ifdef USE_FLOAT |
---|
317 | std::cout << "std::complex<float> compressed_matrix" << std::endl; |
---|
318 | test_my_matrix<ublas::compressed_matrix<std::complex<float> >, 3 > () (); |
---|
319 | #endif |
---|
320 | |
---|
321 | #ifdef USE_DOUBLE |
---|
322 | std::cout << "std::complex<double> compressed_matrix" << std::endl; |
---|
323 | test_my_matrix<ublas::compressed_matrix<std::complex<double> >, 3 > () (); |
---|
324 | #endif |
---|
325 | #endif |
---|
326 | #endif |
---|
327 | |
---|
328 | #ifdef USE_COORDINATE_MATRIX |
---|
329 | #ifdef USE_FLOAT |
---|
330 | std::cout << "float coordinate_matrix" << std::endl; |
---|
331 | test_my_matrix<ublas::coordinate_matrix<float>, 3 > () (); |
---|
332 | #endif |
---|
333 | |
---|
334 | #ifdef USE_DOUBLE |
---|
335 | std::cout << "double coordinate_matrix" << std::endl; |
---|
336 | test_my_matrix<ublas::coordinate_matrix<double>, 3 > () (); |
---|
337 | #endif |
---|
338 | |
---|
339 | #ifdef USE_STD_COMPLEX |
---|
340 | #ifdef USE_FLOAT |
---|
341 | std::cout << "std::complex<float> coordinate_matrix" << std::endl; |
---|
342 | test_my_matrix<ublas::coordinate_matrix<std::complex<float> >, 3 > () (); |
---|
343 | #endif |
---|
344 | |
---|
345 | #ifdef USE_DOUBLE |
---|
346 | std::cout << "std::complex<double> coordinate_matrix" << std::endl; |
---|
347 | test_my_matrix<ublas::coordinate_matrix<std::complex<double> >, 3 > () (); |
---|
348 | #endif |
---|
349 | #endif |
---|
350 | #endif |
---|
351 | } |
---|