Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/numeric/ublas/test/test1/test11.cpp @ 12

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

added boost

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