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