Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/numeric/ublas/test/test11.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: 8.8 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 vector expression templates
20template<class V, int N>
21struct test_my_vector {
22    typedef typename V::value_type value_type;
23    typedef typename V::size_type size_type;
24    typedef typename ublas::type_traits<value_type>::real_type real_type;
25
26    template<class VP>
27    void test_container_with (VP &v1) const {
28        // Container type tests in addition to expression types
29        // Insert and erase
30        v1.insert_element (0, 55);
31        v1.erase_element (1);
32        v1.clear ();
33    }
34   
35    template<class VP>
36    void test_expression_with (VP &v1, VP &v2, VP &v3) const {
37        // Expression type tests
38        value_type t;
39        size_type i;
40        real_type n;
41
42        // Default Construct
43        default_construct<VP>::test ();
44       
45        // Copy and swap
46        initialize_vector (v1);
47        initialize_vector (v2);
48        v1 = v2;
49        std::cout << "v1 = v2 = " << v1 << std::endl;
50        v1.assign_temporary (v2);
51        std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
52        v1.swap (v2);
53        std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
54       
55        // Zero assignment
56        v1 = ublas::zero_vector<> (v1.size ());
57        std::cout << "v1.zero_vector = " << v1 << std::endl;
58        v1 = v2;
59
60#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
61            // Project range and slice
62        initialize_vector (v1);
63        initialize_vector (v2);
64        project (v1, ublas::range(0,1)) = project (v2, ublas::range(0,1));
65        project (v1, ublas::range(0,1)) = project (v2, ublas::slice(0,1,1));
66        project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::slice(0,1,2));
67        project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::range(0,2));
68        std::cout << "v1 = range/slice " << v1 << std::endl;
69#endif
70
71            // Unary vector operations resulting in a vector
72        initialize_vector (v1);
73        v2 = - v1;
74        std::cout << "- v1 = " << v2 << std::endl;
75        v2 = ublas::conj (v1);
76        std::cout << "conj (v1) = " << v2 << std::endl;
77
78        // Binary vector operations resulting in a vector
79        initialize_vector (v1);
80        initialize_vector (v2);
81        v3 = v1 + v2;
82        std::cout << "v1 + v2 = " << v3 << std::endl;
83        v3 = v1 - v2;
84        std::cout << "v1 - v2 = " << v3 << std::endl;
85        v3 = ublas::element_prod (v1, v2);
86        std::cout << "element_prod (v1, v2) = " << v3 << std::endl;
87
88        // Scaling a vector
89        t = N;
90        initialize_vector (v1);
91        v2 = value_type (1.) * v1;
92        std::cout << "1. * v1 = " << v2 << std::endl;
93        v2 = t * v1;
94        std::cout << "N * v1 = " << v2 << std::endl;
95        initialize_vector (v1);
96        v2 = v1 * value_type (1.);
97        std::cout << "v1 * 1. = " << v2 << std::endl;
98        v2 = v1 * t;
99        std::cout << "v1 * N = " << v2 << std::endl;
100
101        // Some assignments
102        initialize_vector (v1);
103        initialize_vector (v2);
104        v2 += v1;
105        std::cout << "v2 += v1 = " << v2 << std::endl;
106        v2 -= v1;
107        std::cout << "v2 -= v1 = " << v2 << std::endl;
108        v2 = v2 + v1;
109        std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
110        v2 = v2 - v1;
111        std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
112        v1 *= value_type (1.);
113        std::cout << "v1 *= 1. = " << v1 << std::endl;
114        v1 *= t;
115        std::cout << "v1 *= N = " << v1 << std::endl;
116
117        // Unary vector operations resulting in a scalar
118        initialize_vector (v1);
119        t = ublas::sum (v1);
120        std::cout << "sum (v1) = " << t << std::endl;
121        n = ublas::norm_1 (v1);
122        std::cout << "norm_1 (v1) = " << n << std::endl;
123        n = ublas::norm_2 (v1);
124        std::cout << "norm_2 (v1) = " << n << std::endl;
125        n = ublas::norm_inf (v1);
126        std::cout << "norm_inf (v1) = " << n << std::endl;
127
128        i = ublas::index_norm_inf (v1);
129        std::cout << "index_norm_inf (v1) = " << i << std::endl;
130
131        // Binary vector operations resulting in a scalar
132        initialize_vector (v1);
133        initialize_vector (v2);
134        t = ublas::inner_prod (v1, v2);
135        std::cout << "inner_prod (v1, v2) = " << t << std::endl;
136
137        // Scalar and Binary vector expression resulting in a vector
138        initialize_vector (v1);
139        initialize_vector (v2);
140        v1 = v1 * ublas::inner_prod (v1, v2);
141        std::cout << "v1 * inner_prod (v1, v2) = " << v1 << std::endl;
142    }
143
144    void operator () () const {
145        V v1 (N), v2 (N), v3 (N);
146        test_expression_with (v1, v2, v3);
147        test_container_with (v1);
148
149#ifdef USE_RANGE
150        ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
151                               vr2 (v2, ublas::range (0, N)),
152                               vr3 (v3, ublas::range (0, N));
153        test_expression_with (vr1, vr2, vr3);
154#endif
155
156#ifdef USE_SLICE
157        ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
158                               vs2 (v2, ublas::slice (0, 1, N)),
159                               vs3 (v3, ublas::slice (0, 1, N));
160        test_expression_with (vs1, vs2, vs3);
161#endif
162    }
163};
164
165// Test vector
166void test_vector () {
167    std::cout << "test_vector" << std::endl;
168
169#ifdef USE_BOUNDED_ARRAY
170#ifdef USE_FLOAT
171    std::cout << "float, bounded_array" << std::endl;
172    test_my_vector<ublas::vector<float, ublas::bounded_array<float, 3> >, 3 > () ();
173#endif
174
175#ifdef USE_DOUBLE
176    std::cout << "double, bounded_array" << std::endl;
177    test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3 > () ();
178#endif
179
180#ifdef USE_STD_COMPLEX
181#ifdef USE_FLOAT
182    std::cout << "std::complex<float>, bounded_array" << std::endl;
183    test_my_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >, 3 > () ();
184#endif
185
186#ifdef USE_DOUBLE
187    std::cout << "std::complex<double>, bounded_array" << std::endl;
188    test_my_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >, 3 > () ();
189#endif
190#endif
191#endif
192
193#ifdef USE_UNBOUNDED_ARRAY
194#ifdef USE_FLOAT
195    std::cout << "float, unbounded_array" << std::endl;
196    test_my_vector<ublas::vector<float, ublas::unbounded_array<float> >, 3 > () ();
197#endif
198
199#ifdef USE_DOUBLE
200    std::cout << "double, unbounded_array" << std::endl;
201    test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3 > () ();
202#endif
203
204#ifdef USE_STD_COMPLEX
205#ifdef USE_FLOAT
206    std::cout << "std::complex<float>, unbounded_array" << std::endl;
207    test_my_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >, 3 > () ();
208#endif
209
210#ifdef USE_DOUBLE
211    std::cout << "std::complex<double>, unbounded_array" << std::endl;
212    test_my_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >, 3 > () ();
213#endif
214#endif
215#endif
216
217#ifdef USE_STD_VECTOR
218#ifdef USE_FLOAT
219    std::cout << "float, std::vector" << std::endl;
220    test_my_vector<ublas::vector<float, std::vector<float> >, 3 > () ();
221#endif
222
223#ifdef USE_DOUBLE
224    std::cout << "double, std::vector" << std::endl;
225    test_my_vector<ublas::vector<double, std::vector<double> >, 3 > () ();
226#endif
227
228#ifdef USE_STD_COMPLEX
229#ifdef USE_FLOAT
230    std::cout << "std::complex<float>, std::vector" << std::endl;
231    test_my_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >, 3 > () ();
232#endif
233
234#ifdef USE_DOUBLE
235    std::cout << "std::complex<double>, std::vector" << std::endl;
236    test_my_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >, 3 > () ();
237#endif
238#endif
239#endif
240
241#ifdef USE_BOUNDED_VECTOR
242#ifdef USE_FLOAT
243    std::cout << "float, bounded" << std::endl;
244    test_my_vector<ublas::bounded_vector<float, 3>, 3> () ();
245#endif
246
247#ifdef USE_DOUBLE
248    std::cout << "double, bounded" << std::endl;
249    test_my_vector<ublas::bounded_vector<double, 3>, 3> () ();
250#endif
251
252#ifdef USE_STD_COMPLEX
253#ifdef USE_FLOAT
254    std::cout << "std::complex<float>, bounded" << std::endl;
255    test_my_vector<ublas::bounded_vector<std::complex<float>, 3>, 3> () ();
256#endif
257
258#ifdef USE_DOUBLE
259    std::cout << "std::complex<double>, bounded" << std::endl;
260    test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3> () ();
261#endif
262#endif
263#endif
264}
Note: See TracBrowser for help on using the repository browser.