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

source code of boost/libs/numeric/ublas/test/test11.cpp