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 | #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 | initialize_vector (v3); |
---|
68 | v3 = v1 + v2; |
---|
69 | std::cout << "v1 + v2 = " << v3 << std::endl; |
---|
70 | |
---|
71 | v3 = v1 - v2; |
---|
72 | std::cout << "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 | } |
---|
124 | void operator () () const { |
---|
125 | { |
---|
126 | V v1 (N, N), v2 (N, N), v3 (N, N); |
---|
127 | test_with (v1, v2, v3); |
---|
128 | |
---|
129 | #ifdef USE_RANGE |
---|
130 | ublas::vector_range<V> vr1 (v1, ublas::range (0, N)), |
---|
131 | vr2 (v2, ublas::range (0, N)), |
---|
132 | vr3 (v3, ublas::range (0, N)); |
---|
133 | test_with (vr1, vr2, vr3); |
---|
134 | #endif |
---|
135 | |
---|
136 | #ifdef USE_SLICE |
---|
137 | ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)), |
---|
138 | vs2 (v2, ublas::slice (0, 1, N)), |
---|
139 | vs3 (v3, ublas::slice (0, 1, N)); |
---|
140 | test_with (vs1, vs2, vs3); |
---|
141 | #endif |
---|
142 | } |
---|
143 | } |
---|
144 | }; |
---|
145 | |
---|
146 | // Test vector |
---|
147 | void test_vector () { |
---|
148 | std::cout << "test_vector" << std::endl; |
---|
149 | |
---|
150 | #ifdef USE_SPARSE_VECTOR |
---|
151 | #ifdef USE_MAP_ARRAY |
---|
152 | #ifdef USE_FLOAT |
---|
153 | std::cout << "float, map_array" << std::endl; |
---|
154 | test_my_vector<ublas::mapped_vector<float, ublas::map_array<std::size_t, float> >, 3 > () (); |
---|
155 | #endif |
---|
156 | |
---|
157 | #ifdef USE_DOUBLE |
---|
158 | std::cout << "double, map_array" << std::endl; |
---|
159 | test_my_vector<ublas::mapped_vector<double, ublas::map_array<std::size_t, double> >, 3 > () (); |
---|
160 | #endif |
---|
161 | |
---|
162 | #ifdef USE_STD_COMPLEX |
---|
163 | #ifdef USE_FLOAT |
---|
164 | std::cout << "std::complex<float>, map_array" << std::endl; |
---|
165 | test_my_vector<ublas::mapped_vector<std::complex<float>, ublas::map_array<std::size_t, std::complex<float> > >, 3 > () (); |
---|
166 | #endif |
---|
167 | |
---|
168 | #ifdef USE_DOUBLE |
---|
169 | std::cout << "std::complex<double>, map_array" << std::endl; |
---|
170 | test_my_vector<ublas::mapped_vector<std::complex<double>, ublas::map_array<std::size_t, std::complex<double> > >, 3 > () (); |
---|
171 | #endif |
---|
172 | #endif |
---|
173 | #endif |
---|
174 | |
---|
175 | #ifdef USE_STD_MAP |
---|
176 | #ifdef USE_FLOAT |
---|
177 | std::cout << "float, std::map" << std::endl; |
---|
178 | test_my_vector<ublas::mapped_vector<float, std::map<std::size_t, float> >, 3 > () (); |
---|
179 | #endif |
---|
180 | |
---|
181 | #ifdef USE_DOUBLE |
---|
182 | std::cout << "double, std::map" << std::endl; |
---|
183 | test_my_vector<ublas::mapped_vector<double, std::map<std::size_t, double> >, 3 > () (); |
---|
184 | #endif |
---|
185 | |
---|
186 | #ifdef USE_STD_COMPLEX |
---|
187 | #ifdef USE_FLOAT |
---|
188 | std::cout << "std::complex<float>, std::map" << std::endl; |
---|
189 | test_my_vector<ublas::mapped_vector<std::complex<float>, std::map<std::size_t, std::complex<float> > >, 3 > () (); |
---|
190 | #endif |
---|
191 | |
---|
192 | #ifdef USE_DOUBLE |
---|
193 | std::cout << "std::complex<double>, std::map" << std::endl; |
---|
194 | test_my_vector<ublas::mapped_vector<std::complex<double>, std::map<std::size_t, std::complex<double> > > , 3 > () (); |
---|
195 | #endif |
---|
196 | #endif |
---|
197 | #endif |
---|
198 | #endif |
---|
199 | |
---|
200 | #ifdef USE_COMPRESSED_VECTOR |
---|
201 | #ifdef USE_FLOAT |
---|
202 | std::cout << "float compressed" << std::endl; |
---|
203 | test_my_vector<ublas::compressed_vector<float>, 3 > () (); |
---|
204 | #endif |
---|
205 | |
---|
206 | #ifdef USE_DOUBLE |
---|
207 | std::cout << "double compressed" << std::endl; |
---|
208 | test_my_vector<ublas::compressed_vector<double>, 3 > () (); |
---|
209 | #endif |
---|
210 | |
---|
211 | #ifdef USE_STD_COMPLEX |
---|
212 | #ifdef USE_FLOAT |
---|
213 | std::cout << "std::complex<float> compressed" << std::endl; |
---|
214 | test_my_vector<ublas::compressed_vector<std::complex<float> >, 3 > () (); |
---|
215 | #endif |
---|
216 | |
---|
217 | #ifdef USE_DOUBLE |
---|
218 | std::cout << "std::complex<double> compressed" << std::endl; |
---|
219 | test_my_vector<ublas::compressed_vector<std::complex<double> >, 3 > () (); |
---|
220 | #endif |
---|
221 | #endif |
---|
222 | #endif |
---|
223 | |
---|
224 | #ifdef USE_COORDINATE_VECTOR |
---|
225 | #ifdef USE_FLOAT |
---|
226 | std::cout << "float coordinate" << std::endl; |
---|
227 | test_my_vector<ublas::coordinate_vector<float>, 3 > () (); |
---|
228 | #endif |
---|
229 | |
---|
230 | #ifdef USE_DOUBLE |
---|
231 | std::cout << "double coordinate" << std::endl; |
---|
232 | test_my_vector<ublas::coordinate_vector<double>, 3 > () (); |
---|
233 | #endif |
---|
234 | |
---|
235 | #ifdef USE_STD_COMPLEX |
---|
236 | #ifdef USE_FLOAT |
---|
237 | std::cout << "std::complex<float> coordinate" << std::endl; |
---|
238 | test_my_vector<ublas::coordinate_vector<std::complex<float> >, 3 > () (); |
---|
239 | #endif |
---|
240 | |
---|
241 | #ifdef USE_DOUBLE |
---|
242 | std::cout << "std::complex<double> coordinate" << std::endl; |
---|
243 | test_my_vector<ublas::coordinate_vector<std::complex<double> >, 3 > () (); |
---|
244 | #endif |
---|
245 | #endif |
---|
246 | #endif |
---|
247 | } |
---|