• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test3.hpp"
14 
15 // Test vector expression templates
16 template<class V, int N>
17 struct test_my_vector {
18     typedef typename V::value_type value_type;
19     typedef typename V::size_type size_type;
20     typedef typename ublas::type_traits<value_type>::real_type real_type;
21 
22     template<class VP>
test_withtest_my_vector23     void test_with (VP &v1, VP &v2, VP &v3) const {
24         {
25             value_type t;
26             size_type i;
27             real_type n;
28 
29             // Default Construct
30             default_construct<VP>::test ();
31 
32             // Copy and swap
33             initialize_vector (v1);
34             initialize_vector (v2);
35             v1 = v2;
36             std::cout << "v1 = v2 = " << v1 << std::endl;
37             v1.assign_temporary (v2);
38             std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
39             v1.swap (v2);
40             std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
41 
42             // Zero assignment
43             v1 = ublas::zero_vector<> (v1.size ());
44             std::cout << "v1.zero_vector = " << v1 << std::endl;
45             v1 = v2;
46 
47 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
48             // Project range and slice
49             initialize_vector (v1);
50             initialize_vector (v2);
51             project (v1, ublas::range(0,1)) = project (v2, ublas::range(0,1));
52             project (v1, ublas::range(0,1)) = project (v2, ublas::slice(0,1,1));
53             project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::slice(0,1,2));
54             project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::range(0,2));
55             std::cout << "v1 = range/slice " << v1 << std::endl;
56 #endif
57 
58             // Unary vector operations resulting in a vector
59             initialize_vector (v1);
60             v2 = - v1;
61             std::cout << "- v1 = " << v2 << std::endl;
62             v2 = ublas::conj (v1);
63             std::cout << "conj (v1) = " << v2 << std::endl;
64 
65             // Binary vector operations resulting in a vector
66             initialize_vector (v1);
67             initialize_vector (v2);
68             initialize_vector (v3);
69             v3 = v1 + v2;
70             std::cout << "v1 + v2 = " << v3 << std::endl;
71 
72             v3 = v1 - v2;
73             std::cout << "v1 - v2 = " << v3 << std::endl;
74 
75             // Scaling a vector
76             t = N;
77             initialize_vector (v1);
78             v2 = value_type (1.) * v1;
79             std::cout << "1. * v1 = " << v2 << std::endl;
80             v2 = t * v1;
81             std::cout << "N * v1 = " << v2 << std::endl;
82             initialize_vector (v1);
83             v2 = v1 * value_type (1.);
84             std::cout << "v1 * 1. = " << v2 << std::endl;
85             v2 = v1 * t;
86             std::cout << "v1 * N = " << v2 << std::endl;
87 
88             // Some assignments
89             initialize_vector (v1);
90             initialize_vector (v2);
91             v2 += v1;
92             std::cout << "v2 += v1 = " << v2 << std::endl;
93             v2 -= v1;
94             std::cout << "v2 -= v1 = " << v2 << std::endl;
95             v2 = v2 + v1;
96             std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
97             v2 = v2 - v1;
98             std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
99             v1 *= value_type (1.);
100             std::cout << "v1 *= 1. = " << v1 << std::endl;
101             v1 *= t;
102             std::cout << "v1 *= N = " << v1 << std::endl;
103 
104             // Unary vector operations resulting in a scalar
105             initialize_vector (v1);
106             t = ublas::sum (v1);
107             std::cout << "sum (v1) = " << t << std::endl;
108             n = ublas::norm_1 (v1);
109             std::cout << "norm_1 (v1) = " << n << std::endl;
110             n = ublas::norm_2 (v1);
111             std::cout << "norm_2 (v1) = " << n << std::endl;
112             n = ublas::norm_inf (v1);
113             std::cout << "norm_inf (v1) = " << n << std::endl;
114 
115             i = ublas::index_norm_inf (v1);
116             std::cout << "index_norm_inf (v1) = " << i << std::endl;
117 
118             // Binary vector operations resulting in a scalar
119             initialize_vector (v1);
120             initialize_vector (v2);
121             t = ublas::inner_prod (v1, v2);
122             std::cout << "inner_prod (v1, v2) = " << t << std::endl;
123         }
124     }
operator ()test_my_vector125     void operator () () const {
126         {
127             V v1 (N, N), v2 (N, N), v3 (N, N);
128             test_with (v1, v2, v3);
129 
130 #ifdef USE_RANGE
131             ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
132                                    vr2 (v2, ublas::range (0, N)),
133                                    vr3 (v3, ublas::range (0, N));
134             test_with (vr1, vr2, vr3);
135 #endif
136 
137 #ifdef USE_SLICE
138             ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
139                                    vs2 (v2, ublas::slice (0, 1, N)),
140                                    vs3 (v3, ublas::slice (0, 1, N));
141             test_with (vs1, vs2, vs3);
142 #endif
143         }
144     }
145 };
146 
147 // Test vector
test_vector()148 void test_vector () {
149     std::cout << "test_vector" << std::endl;
150 
151 #ifdef USE_SPARSE_VECTOR
152 #ifdef USE_MAP_ARRAY
153 #ifdef USE_FLOAT
154     std::cout << "float, map_array" << std::endl;
155     test_my_vector<ublas::mapped_vector<float, ublas::map_array<std::size_t, float> >, 3 > () ();
156 #endif
157 
158 #ifdef USE_DOUBLE
159     std::cout << "double, map_array" << std::endl;
160     test_my_vector<ublas::mapped_vector<double, ublas::map_array<std::size_t, double> >, 3 > () ();
161 #endif
162 
163 #ifdef USE_STD_COMPLEX
164 #ifdef USE_FLOAT
165     std::cout << "std::complex<float>, map_array" << std::endl;
166     test_my_vector<ublas::mapped_vector<std::complex<float>, ublas::map_array<std::size_t, std::complex<float> > >, 3 > () ();
167 #endif
168 
169 #ifdef USE_DOUBLE
170     std::cout << "std::complex<double>, map_array" << std::endl;
171     test_my_vector<ublas::mapped_vector<std::complex<double>, ublas::map_array<std::size_t, std::complex<double> > >, 3 > () ();
172 #endif
173 #endif
174 #endif
175 
176 #ifdef USE_STD_MAP
177 #ifdef USE_FLOAT
178     std::cout << "float, std::map" << std::endl;
179     test_my_vector<ublas::mapped_vector<float, std::map<std::size_t, float> >, 3 > () ();
180 #endif
181 
182 #ifdef USE_DOUBLE
183     std::cout << "double, std::map" << std::endl;
184     test_my_vector<ublas::mapped_vector<double, std::map<std::size_t, double> >, 3 > () ();
185 #endif
186 
187 #ifdef USE_STD_COMPLEX
188 #ifdef USE_FLOAT
189     std::cout << "std::complex<float>, std::map" << std::endl;
190     test_my_vector<ublas::mapped_vector<std::complex<float>, std::map<std::size_t, std::complex<float> > >, 3 > () ();
191 #endif
192 
193 #ifdef USE_DOUBLE
194     std::cout << "std::complex<double>, std::map" << std::endl;
195     test_my_vector<ublas::mapped_vector<std::complex<double>, std::map<std::size_t, std::complex<double> > > , 3 > () ();
196 #endif
197 #endif
198 #endif
199 #endif
200 
201 #ifdef USE_COMPRESSED_VECTOR
202 #ifdef USE_FLOAT
203     std::cout << "float compressed" << std::endl;
204     test_my_vector<ublas::compressed_vector<float>, 3 > () ();
205 #endif
206 
207 #ifdef USE_DOUBLE
208     std::cout << "double compressed" << std::endl;
209     test_my_vector<ublas::compressed_vector<double>, 3 > () ();
210 #endif
211 
212 #ifdef USE_STD_COMPLEX
213 #ifdef USE_FLOAT
214     std::cout << "std::complex<float> compressed" << std::endl;
215     test_my_vector<ublas::compressed_vector<std::complex<float> >, 3 > () ();
216 #endif
217 
218 #ifdef USE_DOUBLE
219     std::cout << "std::complex<double> compressed" << std::endl;
220     test_my_vector<ublas::compressed_vector<std::complex<double> >, 3 > () ();
221 #endif
222 #endif
223 #endif
224 
225 #ifdef USE_COORDINATE_VECTOR
226 #ifdef USE_FLOAT
227     std::cout << "float coordinate" << std::endl;
228     test_my_vector<ublas::coordinate_vector<float>, 3 > () ();
229 #endif
230 
231 #ifdef USE_DOUBLE
232     std::cout << "double coordinate" << std::endl;
233     test_my_vector<ublas::coordinate_vector<double>, 3 > () ();
234 #endif
235 
236 #ifdef USE_STD_COMPLEX
237 #ifdef USE_FLOAT
238     std::cout << "std::complex<float> coordinate" << std::endl;
239     test_my_vector<ublas::coordinate_vector<std::complex<float> >, 3 > () ();
240 #endif
241 
242 #ifdef USE_DOUBLE
243     std::cout << "std::complex<double> coordinate" << std::endl;
244     test_my_vector<ublas::coordinate_vector<std::complex<double> >, 3 > () ();
245 #endif
246 #endif
247 #endif
248 }
249