Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/numeric/ublas/test/test13.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 11.5 KB
Line 
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 "test1.hpp"
18
19// Test matrix expression templates
20template<class M, int N>
21struct test_my_matrix {
22    typedef typename M::value_type value_type;
23
24    template<class VP>
25    void test_container_with (VP &v1) const {
26        // Container type tests in addition to expression types
27        // Insert and erase
28        v1.insert_element (0,0, 55);
29        v1.erase_element (1,1);
30        v1.clear ();
31    }
32   
33    template<class MP>
34    void test_expression_with (MP &m1, MP &m2, MP &m3) const {
35        value_type t;
36
37        // Default Construct
38        default_construct<MP>::test ();
39       
40        // Copy and swap
41        initialize_matrix (m1);
42        initialize_matrix (m2);
43        m1 = m2;
44        std::cout << "m1 = m2 = " << m1 << std::endl;
45        m1.assign_temporary (m2);
46        std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl;
47        m1.swap (m2);
48        std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl;
49
50        // Zero assignment
51        m1 = ublas::zero_matrix<> (m1.size1 (), m1.size2 ());
52        std::cout << "m1.zero_matrix = " << m1 << std::endl;
53        m1 = m2;
54
55#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
56            // Project range and slice
57        initialize_matrix (m1);
58        initialize_matrix (m2);
59        project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::range(0,1),ublas::range(0,1));
60        project (m1, ublas::range(0,1),ublas::range(0,1)) = project (m2, ublas::slice(0,1,1),ublas::slice(0,1,1));
61        project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::slice(0,1,2),ublas::slice(0,1,2));
62        project (m1, ublas::slice(2,-1,2),ublas::slice(2,-1,2)) = project (m2, ublas::range(0,2),ublas::range(0,2));
63        std::cout << "m1 = range/slice " << m1 << std::endl;
64#endif
65
66            // Unary matrix operations resulting in a matrix
67        initialize_matrix (m1);
68        m2 = - m1;
69        std::cout << "- m1 = " << m2 << std::endl;
70        m2 = ublas::conj (m1);
71        std::cout << "conj (m1) = " << m2 << std::endl;
72
73        // Binary matrix operations resulting in a matrix
74        initialize_matrix (m1);
75        initialize_matrix (m2);
76        m3 = m1 + m2;
77        std::cout << "m1 + m2 = " << m3 << std::endl;
78        m3 = m1 - m2;
79        std::cout << "m1 - m2 = " << m3 << std::endl;
80        m3 = ublas::element_prod (m1, m2);
81        std::cout << "element_prod (m1, m2) = " << m3 << std::endl;
82
83        // Scaling a matrix
84        t = N;
85        initialize_matrix (m1);
86        m2 = value_type (1.) * m1;
87        std::cout << "1. * m1 = " << m2 << std::endl;
88        m2 = t * m1;
89        std::cout << "N * m1 = " << m2 << std::endl;
90        initialize_matrix (m1);
91        m2 = m1 * value_type (1.);
92        std::cout << "m1 * 1. = " << 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}
Note: See TracBrowser for help on using the repository browser.