1 // Copyright (c) 2000-2002
2 // Joerg Walter, Mathias Koch
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)
8 // The authors gratefully acknowledge the support of
9 // GeNeSys mbH & Co. KG in producing this work.
11 #if defined(__GNUC__) && (__GNUC__ >= 9)
12 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
17 // Test vector expression templates
18 template <class V, int N>
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;
26 void test_container_with(VP& v1) const
28 // Container type tests in addition to expression types
30 v1.insert_element(0, 55);
36 void test_expression_with(VP& v1, VP& v2, VP& v3) const
38 // Expression type tests
44 default_construct<VP>::test();
47 initialize_vector(v1);
48 initialize_vector(v2);
50 std::cout << "v1 = v2 = " << v1 << std::endl;
51 v1.assign_temporary(v2);
52 std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
54 std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
57 v1 = ublas::zero_vector<>(v1.size());
58 std::cout << "v1.zero_vector = " << v1 << std::endl;
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;
72 // Unary vector operations resulting in a vector
73 initialize_vector(v1);
75 std::cout << "- v1 = " << v2 << std::endl;
77 std::cout << "conj (v1) = " << v2 << std::endl;
79 // Binary vector operations resulting in a vector
80 initialize_vector(v1);
81 initialize_vector(v2);
83 std::cout << "v1 + v2 = " << v3 << std::endl;
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;
91 initialize_vector(v1);
92 v2 = value_type(1.) * v1;
93 std::cout << "1. * v1 = " << v2 << std::endl;
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;
100 std::cout << "v1 * value_type(N) = " << v2 << std::endl;
101 // test interop with integer
104 std::cout << "v1 * N = " << v2 << std::endl;
107 initialize_vector(v1);
108 initialize_vector(v2);
110 std::cout << "v2 += v1 = " << v2 << std::endl;
112 std::cout << "v2 -= v1 = " << v2 << std::endl;
114 std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
116 std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
117 v1 *= value_type(1.);
118 std::cout << "v1 *= 1. = " << v1 << std::endl;
120 std::cout << "v1 *= value_type(N) = " << v1 << std::endl;
121 // test interop with integer
123 std::cout << "v1 *= N = " << v1 << std::endl;
125 // Unary vector operations resulting in a scalar
126 initialize_vector(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;
136 i = ublas::index_norm_inf(v1);
137 std::cout << "index_norm_inf (v1) = " << i << std::endl;
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;
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;
152 void operator()() const
154 V v1(N), v2(N), v3(N);
155 test_expression_with(v1, v2, v3);
156 test_container_with(v1);
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);
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);
177 std::cout << "test_vector" << std::endl;
179 #ifdef USE_BOUNDED_ARRAY
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>()();
186 std::cout << "double, bounded_array" << std::endl;
187 test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3>()();
190 #ifdef USE_STD_COMPLEX
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>()();
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>()();
203 #ifdef USE_UNBOUNDED_ARRAY
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>()();
210 std::cout << "double, unbounded_array" << std::endl;
211 test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3>()();
214 #ifdef USE_STD_COMPLEX
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>()();
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>()();
227 #ifdef USE_STD_VECTOR
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>()();
234 std::cout << "double, std::vector" << std::endl;
235 test_my_vector<ublas::vector<double, std::vector<double> >, 3>()();
238 #ifdef USE_STD_COMPLEX
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>()();
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>()();
251 #ifdef USE_BOUNDED_VECTOR
253 std::cout << "mp_test_type, bounded" << std::endl;
254 test_my_vector<ublas::bounded_vector<mp_test_type, 3>, 3>()();
258 std::cout << "double, bounded" << std::endl;
259 test_my_vector<ublas::bounded_vector<double, 3>, 3>()();
262 #ifdef USE_STD_COMPLEX
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>()();
269 std::cout << "std::complex<double>, bounded" << std::endl;
270 test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3>()();