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 "test1.hpp"
14
15// Test matrix expression templates
16template<class M, int N>
17struct test_my_matrix {
18 typedef typename M::value_type value_type;
19
20 template<class VP>
21 void test_container_with (VP &v1) const {
22 // Container type tests in addition to expression types
23 // Insert and erase
24 v1.insert_element (0,0, 55);
25 v1.erase_element (1,1);
26 v1.clear ();
27 }
28
29 template<class MP>
30 void test_expression_with (MP &m1, MP &m2, MP &m3) const {
31 value_type t;
32
33 // Default Construct
34 default_construct<MP>::test ();
35
36 // Copy and swap
37 initialize_matrix (m1);
38 initialize_matrix (m2);
39 m1 = m2;
40 std::cout << "m1 = m2 = " << m1 << std::endl;
41 m1.assign_temporary (m2);
42 std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl;
43 m1.swap (m2);
44 std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl;
45
46 // Zero assignment
47 m1 = ublas::zero_matrix<> (m1.size1 (), m1.size2 ());
48 std::cout << "m1.zero_matrix = " << m1 << std::endl;
49 m1 = m2;
50
51#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
52 // Project range and slice
53 initialize_matrix (m1);
54 initialize_matrix (m2);
55 project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::range(0,1),ublas::range(0,1));
56 project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::slice(0,1,1),ublas::slice(0,1,1));
57 project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::slice(0,1,2),ublas::slice(0,1,2));
58 project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::range(0,2),ublas::range(0,2));
59 std::cout << "m1 = range/slice " << m1 << std::endl;
60#endif
61
62 // Unary matrix operations resulting in a matrix
63 initialize_matrix (m1);
64 m2 = - m1;
65 std::cout << "- m1 = " << m2 << std::endl;
66 m2 = ublas::conj (m1);
67 std::cout << "conj (m1) = " << m2 << std::endl;
68
69 // Binary matrix operations resulting in a matrix
70 initialize_matrix (m1);
71 initialize_matrix (m2);
72 m3 = m1 + m2;
73 std::cout << "m1 + m2 = " << m3 << std::endl;
74 m3 = m1 - m2;
75 std::cout << "m1 - m2 = " << m3 << std::endl;
76 m3 = ublas::element_prod (m1, m2);
77 std::cout << "element_prod (m1, m2) = " << m3 << std::endl;
78
79 // Scaling a matrix
80 t = N;
81 initialize_matrix (m1);
82 m2 = value_type (1.) * m1;
83 std::cout << "1. * m1 = " << m2 << std::endl;
84 m2 = t * m1;
85 std::cout << "N * m1 = " << m2 << std::endl;
86 initialize_matrix (m1);
87 m2 = m1 * value_type (1.);
88 std::cout << "m1 * 1. = " << m2 << std::endl;
89 m2 = m1 * t;
90 std::cout << "m1 * N = " << m2 << std::endl;
91 m2 = m1 / value_type (2.);
92 std::cout << "m1 / 2. = " << m2 << std::endl;
93 m2 = m1 / t;
94 std::cout << "m1 / N = " << m2 << std::endl;
95
96 // Some assignments
97 initialize_matrix (m1);
98 initialize_matrix (m2);
99 m2 += m1;
100 std::cout << "m2 += m1 = " << m2 << std::endl;
101 m2 -= m1;
102 std::cout << "m2 -= m1 = " << m2 << std::endl;
103 m2 = m2 + m1;
104 std::cout << "m2 = m2 + m1 = " << m2 << std::endl;
105 m2 = m2 - m1;
106 std::cout << "m2 = m2 - m1 = " << m2 << std::endl;
107 m1 *= value_type (1.);
108 std::cout << "m1 *= 1. = " << m1 << std::endl;
109 m1 *= t;
110 std::cout << "m1 *= N = " << m1 << std::endl;
111
112 // Transpose
113 initialize_matrix (m1);
114 m2 = ublas::trans (m1);
115 std::cout << "trans (m1) = " << m2 << std::endl;
116
117 // Hermitean
118 initialize_matrix (m1);
119 m2 = ublas::herm (m1);
120 std::cout << "herm (m1) = " << m2 << std::endl;
121
122 // Matrix multiplication
123 initialize_matrix (m1);
124 initialize_matrix (m2);
125 m3 = ublas::prod (m1, m2);
126 std::cout << "prod (m1, m2) = " << m3 << std::endl;
127 }
128
129 void operator () () const {
130 M m1 (N, N), m2 (N, N), m3 (N, N);
131 test_expression_with (m1, m2, m3);
132 test_container_with (m1);
133
134#ifdef USE_RANGE
135 ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
136 mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
137 mr3 (m3, ublas::range (0, N), ublas::range (0, N));
138 test_expression_with (mr1, mr2, mr3);
139#endif
140
141#ifdef USE_SLICE
142 ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
143 ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
144 ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
145 test_expression_with (ms1, ms2, ms3);
146#endif
147 }
148};
149
150// Test matrix
151void test_matrix () {
152 std::cout << "test_matrix" << std::endl;
153
154#ifdef USE_MATRIX
155#ifdef USE_BOUNDED_ARRAY
156#ifdef USE_FLOAT
157 std::cout << "float, bounded_array" << std::endl;
158 test_my_matrix<ublas::matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () ();
159#endif
160
161#ifdef USE_DOUBLE
162 std::cout << "double, bounded_array" << std::endl;
163 test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () ();
164#endif
165
166#ifdef USE_STD_COMPLEX
167#ifdef USE_FLOAT
168 std::cout << "std::complex<float>, bounded_array" << std::endl;
169 test_my_matrix<ublas::matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () ();
170#endif
171
172#ifdef USE_DOUBLE
173 std::cout << "std::complex<double>, bounded_array" << std::endl;
174 test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () ();
175#endif
176#endif
177#endif
178
179#ifdef USE_UNBOUNDED_ARRAY
180#ifdef USE_FLOAT
181 std::cout << "float, unbounded_array" << std::endl;
182 test_my_matrix<ublas::matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3> () ();
183#endif
184
185#ifdef USE_DOUBLE
186 std::cout << "double, unbounded_array" << std::endl;
187 test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3> () ();
188#endif
189
190#ifdef USE_STD_COMPLEX
191#ifdef USE_FLOAT
192 std::cout << "std::complex<float>, unbounded_array" << std::endl;
193 test_my_matrix<ublas::matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () ();
194#endif
195
196#ifdef USE_DOUBLE
197 std::cout << "std::complex<double>, unbounded_array" << std::endl;
198 test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () ();
199#endif
200#endif
201#endif
202
203#ifdef USE_STD_VECTOR
204#ifdef USE_FLOAT
205 std::cout << "float, std::vector" << std::endl;
206 test_my_matrix<ublas::matrix<float, ublas::row_major, std::vector<float> >, 3> () ();
207#endif
208
209#ifdef USE_DOUBLE
210 std::cout << "double, std::vector" << std::endl;
211 test_my_matrix<ublas::matrix<double, ublas::row_major, std::vector<double> >, 3> () ();
212#endif
213
214#ifdef USE_STD_COMPLEX
215#ifdef USE_FLOAT
216 std::cout << "std::complex<float>, std::vector" << std::endl;
217 test_my_matrix<ublas::matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3> () ();
218#endif
219
220#ifdef USE_DOUBLE
221 std::cout << "std::complex<double>, std::vector" << std::endl;
222 test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3> () ();
223#endif
224#endif
225#endif
226#endif
227
228#ifdef USE_BOUNDED_MATRIX
229#ifdef USE_FLOAT
230 std::cout << "float, bounded" << std::endl;
231 test_my_matrix<ublas::bounded_matrix<float, 3, 3>, 3> () ();
232#endif
233
234#ifdef USE_DOUBLE
235 std::cout << "double, bounded" << std::endl;
236 test_my_matrix<ublas::bounded_matrix<double, 3, 3>, 3> () ();
237#endif
238
239#ifdef USE_STD_COMPLEX
240#ifdef USE_FLOAT
241 std::cout << "std::complex<float>, bounded" << std::endl;
242 test_my_matrix<ublas::bounded_matrix<std::complex<float>, 3, 3>, 3> () ();
243#endif
244
245#ifdef USE_DOUBLE
246 std::cout << "std::complex<double>, bounded" << std::endl;
247 test_my_matrix<ublas::bounded_matrix<std::complex<double>, 3, 3>, 3> () ();
248#endif
249#endif
250#endif
251
252#ifdef USE_VECTOR_OF_VECTOR
253#ifdef USE_BOUNDED_ARRAY
254#ifdef USE_FLOAT
255 std::cout << "float, bounded_array" << std::endl;
256 test_my_matrix<ublas::vector_of_vector<float, ublas::row_major, ublas::bounded_array<ublas::bounded_array<float, 3>, 3 + 1> >, 3> () ();
257#endif
258
259#ifdef USE_DOUBLE
260 std::cout << "double, bounded_array" << std::endl;
261 test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::bounded_array<ublas::bounded_array<double, 3>, 3 + 1> >, 3> () ();
262#endif
263
264#ifdef USE_STD_COMPLEX
265#ifdef USE_FLOAT
266 std::cout << "std::complex<float>, bounded_array" << std::endl;
267 test_my_matrix<ublas::vector_of_vector<std::complex<float>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<float>, 3>, 3 + 1> >, 3> () ();
268#endif
269
270#ifdef USE_DOUBLE
271 std::cout << "std::complex<double>, bounded_array" << std::endl;
272 test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<double>, 3>, 3 + 1> >, 3> () ();
273#endif
274#endif
275#endif
276
277#ifdef USE_UNBOUNDED_ARRAY
278#ifdef USE_FLOAT
279 std::cout << "float, unbounded_array" << std::endl;
280 test_my_matrix<ublas::vector_of_vector<float, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<float> > >, 3> () ();
281#endif
282
283#ifdef USE_DOUBLE
284 std::cout << "double, unbounded_array" << std::endl;
285 test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<double> > >, 3> () ();
286#endif
287
288#ifdef USE_STD_COMPLEX
289#ifdef USE_FLOAT
290 std::cout << "std::complex<float>, unbounded_array" << std::endl;
291 test_my_matrix<ublas::vector_of_vector<std::complex<float>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<float> > > >, 3> () ();
292#endif
293
294#ifdef USE_DOUBLE
295 std::cout << "std::complex<double>, unbounded_array" << std::endl;
296 test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<double> > > >, 3> () ();
297#endif
298#endif
299#endif
300
301#ifdef USE_STD_VECTOR
302#ifdef USE_FLOAT
303 std::cout << "float, std::vector" << std::endl;
304 test_my_matrix<ublas::vector_of_vector<float, ublas::row_major, std::vector<std::vector<float > > >, 3> () ();
305#endif
306
307#ifdef USE_DOUBLE
308 std::cout << "double, std::vector" << std::endl;
309 test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, std::vector<std::vector<double> > >, 3> () ();
310#endif
311
312#ifdef USE_STD_COMPLEX
313#ifdef USE_FLOAT
314 std::cout << "std::complex<float>, std::vector" << std::endl;
315 test_my_matrix<ublas::vector_of_vector<std::complex<float>, ublas::row_major, std::vector<std::vector<std::complex<float> > > >, 3> () ();
316#endif
317
318#ifdef USE_DOUBLE
319 std::cout << "std::complex<double>, std::vector" << std::endl;
320 test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, std::vector<std::vector<std::complex<double> > > >, 3> () ();
321#endif
322#endif
323#endif
324#endif
325}
326

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