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