1 // Copyright (c) 2000-2002
2 // Joerg Walter, Mathias Koch
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // The authors gratefully acknowledge the support of
9 // GeNeSys mbH & Co. KG in producing this work.
10
11 #if defined(__GNUC__) && (__GNUC__ >= 9)
12 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
13 #endif
14
15 #include "test1.hpp"
16
17 // Test vector expression templates
18 template <class V, int N>
19 struct test_my_vector
20 {
21 typedef typename V::value_type value_type;
22 typedef typename V::size_type size_type;
23 typedef typename ublas::type_traits<value_type>::real_type real_type;
24
25 template <class VP>
test_container_withtest_my_vector26 void test_container_with(VP& v1) const
27 {
28 // Container type tests in addition to expression types
29 // Insert and erase
30 v1.insert_element(0, 55);
31 v1.erase_element(1);
32 v1.clear();
33 }
34
35 template <class VP>
test_expression_withtest_my_vector36 void test_expression_with(VP& v1, VP& v2, VP& v3) const
37 {
38 // Expression type tests
39 value_type t;
40 size_type i;
41 real_type n;
42
43 // Default Construct
44 default_construct<VP>::test();
45
46 // Copy and swap
47 initialize_vector(v1);
48 initialize_vector(v2);
49 v1 = v2;
50 std::cout << "v1 = v2 = " << v1 << std::endl;
51 v1.assign_temporary(v2);
52 std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
53 v1.swap(v2);
54 std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
55
56 // Zero assignment
57 v1 = ublas::zero_vector<>(v1.size());
58 std::cout << "v1.zero_vector = " << v1 << std::endl;
59 v1 = v2;
60
61 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
62 // Project range and slice
63 initialize_vector(v1);
64 initialize_vector(v2);
65 project(v1, ublas::range(0, 1)) = project(v2, ublas::range(0, 1));
66 project(v1, ublas::range(0, 1)) = project(v2, ublas::slice(0, 1, 1));
67 project(v1, ublas::slice(2, -1, 2)) = project(v2, ublas::slice(0, 1, 2));
68 project(v1, ublas::slice(2, -1, 2)) = project(v2, ublas::range(0, 2));
69 std::cout << "v1 = range/slice " << v1 << std::endl;
70 #endif
71
72 // Unary vector operations resulting in a vector
73 initialize_vector(v1);
74 v2 = -v1;
75 std::cout << "- v1 = " << v2 << std::endl;
76 v2 = ublas::conj(v1);
77 std::cout << "conj (v1) = " << v2 << std::endl;
78
79 // Binary vector operations resulting in a vector
80 initialize_vector(v1);
81 initialize_vector(v2);
82 v3 = v1 + v2;
83 std::cout << "v1 + v2 = " << v3 << std::endl;
84 v3 = v1 - v2;
85 std::cout << "v1 - v2 = " << v3 << std::endl;
86 v3 = ublas::element_prod(v1, v2);
87 std::cout << "element_prod (v1, v2) = " << v3 << std::endl;
88
89 // Scaling a vector
90 t = N;
91 initialize_vector(v1);
92 v2 = value_type(1.) * v1;
93 std::cout << "1. * v1 = " << v2 << std::endl;
94 v2 = t * v1;
95 std::cout << "N * v1 = " << v2 << std::endl;
96 initialize_vector(v1);
97 v2 = v1 * value_type(1.);
98 std::cout << "v1 * 1. = " << v2 << std::endl;
99 v2 = v1 * t;
100 std::cout << "v1 * value_type(N) = " << v2 << std::endl;
101 // test interop with integer
102 v2 = v1 * N;
103
104 std::cout << "v1 * N = " << v2 << std::endl;
105
106 // Some assignments
107 initialize_vector(v1);
108 initialize_vector(v2);
109 v2 += v1;
110 std::cout << "v2 += v1 = " << v2 << std::endl;
111 v2 -= v1;
112 std::cout << "v2 -= v1 = " << v2 << std::endl;
113 v2 = v2 + v1;
114 std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
115 v2 = v2 - v1;
116 std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
117 v1 *= value_type(1.);
118 std::cout << "v1 *= 1. = " << v1 << std::endl;
119 v1 *= t;
120 std::cout << "v1 *= value_type(N) = " << v1 << std::endl;
121 // test interop with integer
122 v1 *= N;
123 std::cout << "v1 *= N = " << v1 << std::endl;
124
125 // Unary vector operations resulting in a scalar
126 initialize_vector(v1);
127 t = ublas::sum(v1);
128 std::cout << "sum (v1) = " << t << std::endl;
129 n = ublas::norm_1(v1);
130 std::cout << "norm_1 (v1) = " << n << std::endl;
131 n = ublas::norm_2(v1);
132 std::cout << "norm_2 (v1) = " << n << std::endl;
133 n = ublas::norm_inf(v1);
134 std::cout << "norm_inf (v1) = " << n << std::endl;
135
136 i = ublas::index_norm_inf(v1);
137 std::cout << "index_norm_inf (v1) = " << i << std::endl;
138
139 // Binary vector operations resulting in a scalar
140 initialize_vector(v1);
141 initialize_vector(v2);
142 t = ublas::inner_prod(v1, v2);
143 std::cout << "inner_prod (v1, v2) = " << t << std::endl;
144
145 // Scalar and Binary vector expression resulting in a vector
146 initialize_vector(v1);
147 initialize_vector(v2);
148 v1 = v1 * ublas::inner_prod(v1, v2);
149 std::cout << "v1 * inner_prod (v1, v2) = " << v1 << std::endl;
150 }
151
operator ()test_my_vector152 void operator()() const
153 {
154 V v1(N), v2(N), v3(N);
155 test_expression_with(v1, v2, v3);
156 test_container_with(v1);
157
158 #ifdef USE_RANGE
159 ublas::vector_range<V> vr1(v1, ublas::range(0, N)),
160 vr2(v2, ublas::range(0, N)),
161 vr3(v3, ublas::range(0, N));
162 test_expression_with(vr1, vr2, vr3);
163 #endif
164
165 #ifdef USE_SLICE
166 ublas::vector_slice<V> vs1(v1, ublas::slice(0, 1, N)),
167 vs2(v2, ublas::slice(0, 1, N)),
168 vs3(v3, ublas::slice(0, 1, N));
169 test_expression_with(vs1, vs2, vs3);
170 #endif
171 }
172 };
173
174 // Test vector
test_vector()175 void test_vector()
176 {
177 std::cout << "test_vector" << std::endl;
178
179 #ifdef USE_BOUNDED_ARRAY
180 #ifdef USE_FLOAT
181 std::cout << "mp_test_type, bounded_array" << std::endl;
182 test_my_vector<ublas::vector<mp_test_type, ublas::bounded_array<mp_test_type, 3> >, 3>()();
183 #endif
184
185 #ifdef USE_DOUBLE
186 std::cout << "double, bounded_array" << std::endl;
187 test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3>()();
188 #endif
189
190 #ifdef USE_STD_COMPLEX
191 #ifdef USE_FLOAT
192 std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
193 test_my_vector<ublas::vector<std::complex<mp_test_type>, ublas::bounded_array<std::complex<mp_test_type>, 3> >, 3>()();
194 #endif
195
196 #ifdef USE_DOUBLE
197 std::cout << "std::complex<double>, bounded_array" << std::endl;
198 test_my_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >, 3>()();
199 #endif
200 #endif
201 #endif
202
203 #ifdef USE_UNBOUNDED_ARRAY
204 #ifdef USE_FLOAT
205 std::cout << "mp_test_type, unbounded_array" << std::endl;
206 test_my_vector<ublas::vector<mp_test_type, ublas::unbounded_array<mp_test_type> >, 3>()();
207 #endif
208
209 #ifdef USE_DOUBLE
210 std::cout << "double, unbounded_array" << std::endl;
211 test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3>()();
212 #endif
213
214 #ifdef USE_STD_COMPLEX
215 #ifdef USE_FLOAT
216 std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
217 test_my_vector<ublas::vector<std::complex<mp_test_type>, ublas::unbounded_array<std::complex<mp_test_type> > >, 3>()();
218 #endif
219
220 #ifdef USE_DOUBLE
221 std::cout << "std::complex<double>, unbounded_array" << std::endl;
222 test_my_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >, 3>()();
223 #endif
224 #endif
225 #endif
226
227 #ifdef USE_STD_VECTOR
228 #ifdef USE_FLOAT
229 std::cout << "mp_test_type, std::vector" << std::endl;
230 test_my_vector<ublas::vector<mp_test_type, std::vector<mp_test_type> >, 3>()();
231 #endif
232
233 #ifdef USE_DOUBLE
234 std::cout << "double, std::vector" << std::endl;
235 test_my_vector<ublas::vector<double, std::vector<double> >, 3>()();
236 #endif
237
238 #ifdef USE_STD_COMPLEX
239 #ifdef USE_FLOAT
240 std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
241 test_my_vector<ublas::vector<std::complex<mp_test_type>, std::vector<std::complex<mp_test_type> > >, 3>()();
242 #endif
243
244 #ifdef USE_DOUBLE
245 std::cout << "std::complex<double>, std::vector" << std::endl;
246 test_my_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >, 3>()();
247 #endif
248 #endif
249 #endif
250
251 #ifdef USE_BOUNDED_VECTOR
252 #ifdef USE_FLOAT
253 std::cout << "mp_test_type, bounded" << std::endl;
254 test_my_vector<ublas::bounded_vector<mp_test_type, 3>, 3>()();
255 #endif
256
257 #ifdef USE_DOUBLE
258 std::cout << "double, bounded" << std::endl;
259 test_my_vector<ublas::bounded_vector<double, 3>, 3>()();
260 #endif
261
262 #ifdef USE_STD_COMPLEX
263 #ifdef USE_FLOAT
264 std::cout << "std::complex<mp_test_type>, bounded" << std::endl;
265 test_my_vector<ublas::bounded_vector<std::complex<mp_test_type>, 3>, 3>()();
266 #endif
267
268 #ifdef USE_DOUBLE
269 std::cout << "std::complex<double>, bounded" << std::endl;
270 test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3>()();
271 #endif
272 #endif
273 #endif
274 }
275