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 "test4.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 | // Unary matrix operations resulting in a matrix |
---|
48 | initialize_matrix (m1); |
---|
49 | m2 = - m1; |
---|
50 | std::cout << "- m1 = " << m2 << std::endl; |
---|
51 | m2 = ublas::conj (m1); |
---|
52 | std::cout << "conj (m1) = " << m2 << std::endl; |
---|
53 | |
---|
54 | // Binary matrix operations resulting in a matrix |
---|
55 | initialize_matrix (m1); |
---|
56 | initialize_matrix (m2); |
---|
57 | m3 = m1 + m2; |
---|
58 | std::cout << "m1 + m2 = " << m3 << std::endl; |
---|
59 | m3 = m1 - m2; |
---|
60 | std::cout << "m1 - m2 = " << m3 << std::endl; |
---|
61 | |
---|
62 | // Scaling a matrix |
---|
63 | t = N; |
---|
64 | initialize_matrix (m1); |
---|
65 | m2 = value_type (1.) * m1; |
---|
66 | std::cout << "1. * m1 = " << m2 << std::endl; |
---|
67 | m2 = t * m1; |
---|
68 | std::cout << "N * m1 = " << m2 << std::endl; |
---|
69 | initialize_matrix (m1); |
---|
70 | m2 = m1 * value_type (1.); |
---|
71 | std::cout << "m1 * 1. = " << m2 << std::endl; |
---|
72 | m2 = m1 * t; |
---|
73 | std::cout << "m1 * N = " << m2 << std::endl; |
---|
74 | |
---|
75 | // Some assignments |
---|
76 | initialize_matrix (m1); |
---|
77 | initialize_matrix (m2); |
---|
78 | m2 += m1; |
---|
79 | std::cout << "m2 += m1 = " << m2 << std::endl; |
---|
80 | m2 -= m1; |
---|
81 | std::cout << "m2 -= m1 = " << m2 << std::endl; |
---|
82 | m2 = m2 + m1; |
---|
83 | std::cout << "m2 = m2 + m1 = " << m2 << std::endl; |
---|
84 | m2 = m2 - m1; |
---|
85 | std::cout << "m2 = m2 - m1 = " << m2 << std::endl; |
---|
86 | m1 *= value_type (1.); |
---|
87 | std::cout << "m1 *= 1. = " << m1 << std::endl; |
---|
88 | m1 *= t; |
---|
89 | std::cout << "m1 *= N = " << m1 << std::endl; |
---|
90 | |
---|
91 | // Transpose |
---|
92 | initialize_matrix (m1); |
---|
93 | m2 = ublas::trans (m1); |
---|
94 | std::cout << "trans (m1) = " << m2 << std::endl; |
---|
95 | |
---|
96 | // Hermitean |
---|
97 | initialize_matrix (m1); |
---|
98 | m2 = ublas::herm (m1); |
---|
99 | std::cout << "herm (m1) = " << m2 << std::endl; |
---|
100 | |
---|
101 | // Matrix multiplication |
---|
102 | initialize_matrix (m1); |
---|
103 | initialize_matrix (m2); |
---|
104 | // Banded times banded isn't banded |
---|
105 | std::cout << "prod (m1, m2) = " << ublas::prod (m1, m2) << std::endl; |
---|
106 | } |
---|
107 | } |
---|
108 | void operator () () const { |
---|
109 | { |
---|
110 | #ifdef USE_BANDED |
---|
111 | M m1 (N, N, 1, 1), m2 (N, N, 1, 1), m3 (N, N, 1, 1); |
---|
112 | #endif |
---|
113 | #ifdef USE_DIAGONAL |
---|
114 | M m1 (N, N), m2 (N, N), m3 (N, N); |
---|
115 | #endif |
---|
116 | test_with (m1, m2, m3); |
---|
117 | |
---|
118 | #ifdef USE_RANGE |
---|
119 | ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)), |
---|
120 | mr2 (m2, ublas::range (0, N), ublas::range (0, N)), |
---|
121 | mr3 (m3, ublas::range (0, N), ublas::range (0, N)); |
---|
122 | test_with (mr1, mr2, mr3); |
---|
123 | #endif |
---|
124 | |
---|
125 | #ifdef USE_SLICE |
---|
126 | ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
127 | ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
128 | ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
129 | test_with (ms1, ms2, ms3); |
---|
130 | #endif |
---|
131 | } |
---|
132 | |
---|
133 | #ifdef USE_ADAPTOR |
---|
134 | { |
---|
135 | #ifdef USE_BANDED |
---|
136 | M m1 (N, N, 1, 1), m2 (N, N, 1, 1), m3 (N, N, 1, 1); |
---|
137 | ublas::banded_adaptor<M> bam1 (m1, 1, 1), bam2 (m2, 1, 1), bam3 (m3, 1, 1); |
---|
138 | test_with (bam1, bam2, bam3); |
---|
139 | |
---|
140 | #ifdef USE_RANGE |
---|
141 | ublas::matrix_range<ublas::banded_adaptor<M> > mr1 (bam1, ublas::range (0, N), ublas::range (0, N)), |
---|
142 | mr2 (bam2, ublas::range (0, N), ublas::range (0, N)), |
---|
143 | mr3 (bam3, ublas::range (0, N), ublas::range (0, N)); |
---|
144 | test_with (mr1, mr2, mr3); |
---|
145 | #endif |
---|
146 | |
---|
147 | #ifdef USE_SLICE |
---|
148 | ublas::matrix_slice<ublas::banded_adaptor<M> > ms1 (bam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
149 | ms2 (bam2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
150 | ms3 (bam3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
151 | test_with (ms1, ms2, ms3); |
---|
152 | #endif |
---|
153 | #endif |
---|
154 | #ifdef USE_DIAGONAL |
---|
155 | M m1 (N, N), m2 (N, N), m3 (N, N); |
---|
156 | ublas::diagonal_adaptor<M> dam1 (m1), dam2 (m2), dam3 (m3); |
---|
157 | test_with (dam1, dam2, dam3); |
---|
158 | |
---|
159 | #ifdef USE_RANGE |
---|
160 | ublas::matrix_range<ublas::diagonal_adaptor<M> > mr1 (dam1, ublas::range (0, N), ublas::range (0, N)), |
---|
161 | mr2 (dam2, ublas::range (0, N), ublas::range (0, N)), |
---|
162 | mr3 (dam3, ublas::range (0, N), ublas::range (0, N)); |
---|
163 | test_with (mr1, mr2, mr3); |
---|
164 | #endif |
---|
165 | |
---|
166 | #ifdef USE_SLICE |
---|
167 | ublas::matrix_slice<ublas::diagonal_adaptor<M> > ms1 (dam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
168 | ms2 (dam2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
169 | ms3 (dam3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
170 | test_with (ms1, ms2, ms3); |
---|
171 | #endif |
---|
172 | #endif |
---|
173 | } |
---|
174 | #endif |
---|
175 | |
---|
176 | } |
---|
177 | }; |
---|
178 | |
---|
179 | // Test matrix |
---|
180 | void test_matrix () { |
---|
181 | std::cout << "test_matrix" << std::endl; |
---|
182 | |
---|
183 | #ifdef USE_BANDED |
---|
184 | #ifdef USE_BOUNDED_ARRAY |
---|
185 | #ifdef USE_FLOAT |
---|
186 | std::cout << "float, bounded_array" << std::endl; |
---|
187 | test_my_matrix<ublas::banded_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3 > () (); |
---|
188 | #endif |
---|
189 | |
---|
190 | #ifdef USE_DOUBLE |
---|
191 | std::cout << "double, bounded_array" << std::endl; |
---|
192 | test_my_matrix<ublas::banded_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3 > () (); |
---|
193 | #endif |
---|
194 | |
---|
195 | #ifdef USE_STD_COMPLEX |
---|
196 | #ifdef USE_FLOAT |
---|
197 | std::cout << "std::complex<float>, bounded_array" << std::endl; |
---|
198 | test_my_matrix<ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3 > () (); |
---|
199 | #endif |
---|
200 | |
---|
201 | #ifdef USE_DOUBLE |
---|
202 | std::cout << "std::complex<double>, bounded_array" << std::endl; |
---|
203 | test_my_matrix<ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3 > () (); |
---|
204 | #endif |
---|
205 | #endif |
---|
206 | #endif |
---|
207 | |
---|
208 | #ifdef USE_UNBOUNDED_ARRAY |
---|
209 | #ifdef USE_FLOAT |
---|
210 | std::cout << "float, unbounded_array" << std::endl; |
---|
211 | test_my_matrix<ublas::banded_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3 > () (); |
---|
212 | #endif |
---|
213 | |
---|
214 | #ifdef USE_DOUBLE |
---|
215 | std::cout << "double, unbounded_array" << std::endl; |
---|
216 | test_my_matrix<ublas::banded_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3 > () (); |
---|
217 | #endif |
---|
218 | |
---|
219 | #ifdef USE_STD_COMPLEX |
---|
220 | #ifdef USE_FLOAT |
---|
221 | std::cout << "std::complex<float>, unbounded_array" << std::endl; |
---|
222 | test_my_matrix<ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3 > () (); |
---|
223 | #endif |
---|
224 | |
---|
225 | #ifdef USE_DOUBLE |
---|
226 | std::cout << "std::complex<double>, unbounded_array" << std::endl; |
---|
227 | test_my_matrix<ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3 > () (); |
---|
228 | #endif |
---|
229 | #endif |
---|
230 | #endif |
---|
231 | |
---|
232 | #ifdef USE_STD_VECTOR |
---|
233 | #ifdef USE_FLOAT |
---|
234 | std::cout << "float, std::vector" << std::endl; |
---|
235 | test_my_matrix<ublas::banded_matrix<float, ublas::row_major, std::vector<float> >, 3 > () (); |
---|
236 | #endif |
---|
237 | |
---|
238 | #ifdef USE_DOUBLE |
---|
239 | std::cout << "double, std::vector" << std::endl; |
---|
240 | test_my_matrix<ublas::banded_matrix<double, ublas::row_major, std::vector<double> >, 3 > () (); |
---|
241 | #endif |
---|
242 | |
---|
243 | #ifdef USE_STD_COMPLEX |
---|
244 | #ifdef USE_FLOAT |
---|
245 | std::cout << "std::complex<float>, std::vector" << std::endl; |
---|
246 | test_my_matrix<ublas::banded_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3 > () (); |
---|
247 | #endif |
---|
248 | |
---|
249 | #ifdef USE_DOUBLE |
---|
250 | std::cout << "std::complex<double>, std::vector" << std::endl; |
---|
251 | test_my_matrix<ublas::banded_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3 > () (); |
---|
252 | #endif |
---|
253 | #endif |
---|
254 | #endif |
---|
255 | #endif |
---|
256 | |
---|
257 | #ifdef USE_DIAGONAL |
---|
258 | #ifdef USE_BOUNDED_ARRAY |
---|
259 | #ifdef USE_FLOAT |
---|
260 | std::cout << "float, bounded_array" << std::endl; |
---|
261 | test_my_matrix<ublas::diagonal_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3 > () (); |
---|
262 | #endif |
---|
263 | |
---|
264 | #ifdef USE_DOUBLE |
---|
265 | std::cout << "double, bounded_array" << std::endl; |
---|
266 | test_my_matrix<ublas::diagonal_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3 > () (); |
---|
267 | #endif |
---|
268 | |
---|
269 | #ifdef USE_STD_COMPLEX |
---|
270 | #ifdef USE_FLOAT |
---|
271 | std::cout << "std::complex<float>, bounded_array" << std::endl; |
---|
272 | test_my_matrix<ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3 > () (); |
---|
273 | #endif |
---|
274 | |
---|
275 | #ifdef USE_DOUBLE |
---|
276 | std::cout << "std::complex<double>, bounded_array" << std::endl; |
---|
277 | test_my_matrix<ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3 > () (); |
---|
278 | #endif |
---|
279 | #endif |
---|
280 | #endif |
---|
281 | |
---|
282 | #ifdef USE_UNBOUNDED_ARRAY |
---|
283 | #ifdef USE_FLOAT |
---|
284 | std::cout << "float, unbounded_array" << std::endl; |
---|
285 | test_my_matrix<ublas::diagonal_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3 > () (); |
---|
286 | #endif |
---|
287 | |
---|
288 | #ifdef USE_DOUBLE |
---|
289 | std::cout << "double, unbounded_array" << std::endl; |
---|
290 | test_my_matrix<ublas::diagonal_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3 > () (); |
---|
291 | #endif |
---|
292 | |
---|
293 | #ifdef USE_STD_COMPLEX |
---|
294 | #ifdef USE_FLOAT |
---|
295 | std::cout << "std::complex<float>, unbounded_array" << std::endl; |
---|
296 | test_my_matrix<ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3 > () (); |
---|
297 | #endif |
---|
298 | |
---|
299 | #ifdef USE_DOUBLE |
---|
300 | std::cout << "std::complex<double>, unbounded_array" << std::endl; |
---|
301 | test_my_matrix<ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3 > () (); |
---|
302 | #endif |
---|
303 | #endif |
---|
304 | #endif |
---|
305 | |
---|
306 | #ifdef USE_STD_VECTOR |
---|
307 | #ifdef USE_FLOAT |
---|
308 | std::cout << "float, std::vector" << std::endl; |
---|
309 | test_my_matrix<ublas::diagonal_matrix<float, ublas::row_major, std::vector<float> >, 3 > () (); |
---|
310 | #endif |
---|
311 | |
---|
312 | #ifdef USE_DOUBLE |
---|
313 | std::cout << "double, std::vector" << std::endl; |
---|
314 | test_my_matrix<ublas::diagonal_matrix<double, ublas::row_major, std::vector<double> >, 3 > () (); |
---|
315 | #endif |
---|
316 | |
---|
317 | #ifdef USE_STD_COMPLEX |
---|
318 | #ifdef USE_FLOAT |
---|
319 | std::cout << "std::complex<float>, std::vector" << std::endl; |
---|
320 | test_my_matrix<ublas::diagonal_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3 > () (); |
---|
321 | #endif |
---|
322 | |
---|
323 | #ifdef USE_DOUBLE |
---|
324 | std::cout << "std::complex<double>, std::vector" << std::endl; |
---|
325 | test_my_matrix<ublas::diagonal_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3 > () (); |
---|
326 | #endif |
---|
327 | #endif |
---|
328 | #endif |
---|
329 | #endif |
---|
330 | } |
---|