2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
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)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #if defined(__GNUC__) && (__GNUC__ >= 9)
14 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
19 // Test vector expression templates
20 template <class V, int N>
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;
28 void test_with(VP& v1, VP& v2, VP& v3) const
36 default_construct<VP>::test();
39 initialize_vector(v1);
40 initialize_vector(v2);
42 std::cout << "v1 = v2 = " << v1 << std::endl;
43 v1.assign_temporary(v2);
44 std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
46 std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
49 v1 = ublas::zero_vector<>(v1.size());
50 std::cout << "v1.zero_vector = " << v1 << std::endl;
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;
64 // Unary vector operations resulting in a vector
65 initialize_vector(v1);
67 std::cout << "- v1 = " << v2 << std::endl;
69 std::cout << "conj (v1) = " << v2 << std::endl;
71 // Binary vector operations resulting in a vector
72 initialize_vector(v1);
73 initialize_vector(v2);
74 initialize_vector(v3);
76 std::cout << "v1 + v2 = " << v3 << std::endl;
79 std::cout << "v1 - v2 = " << v3 << std::endl;
83 initialize_vector(v1);
84 v2 = value_type(1.) * v1;
85 std::cout << "1. * v1 = " << v2 << std::endl;
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;
92 std::cout << "v1 * N = " << v2 << std::endl;
95 initialize_vector(v1);
96 initialize_vector(v2);
98 std::cout << "v2 += v1 = " << v2 << std::endl;
100 std::cout << "v2 -= v1 = " << v2 << std::endl;
102 std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
104 std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
105 v1 *= value_type(1.);
106 std::cout << "v1 *= 1. = " << v1 << std::endl;
108 std::cout << "v1 *= N = " << v1 << std::endl;
110 // Unary vector operations resulting in a scalar
111 initialize_vector(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;
121 i = ublas::index_norm_inf(v1);
122 std::cout << "index_norm_inf (v1) = " << i << std::endl;
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;
131 void operator()() const
134 V v1(N, N), v2(N, N), v3(N, N);
135 test_with(v1, v2, v3);
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);
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);
157 std::cout << "test_vector" << std::endl;
159 #ifdef USE_SPARSE_VECTOR
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>()();
167 std::cout << "double, map_array" << std::endl;
168 test_my_vector<ublas::mapped_vector<double, ublas::map_array<std::size_t, double> >, 3>()();
171 #ifdef USE_STD_COMPLEX
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>()();
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>()();
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>()();
191 std::cout << "double, std::map" << std::endl;
192 test_my_vector<ublas::mapped_vector<double, std::map<std::size_t, double> >, 3>()();
195 #ifdef USE_STD_COMPLEX
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>()();
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>()();
209 #ifdef USE_COMPRESSED_VECTOR
211 std::cout << "mp_test_type compressed" << std::endl;
212 test_my_vector<ublas::compressed_vector<mp_test_type>, 3>()();
216 std::cout << "double compressed" << std::endl;
217 test_my_vector<ublas::compressed_vector<double>, 3>()();
220 #ifdef USE_STD_COMPLEX
222 std::cout << "std::complex<mp_test_type> compressed" << std::endl;
223 test_my_vector<ublas::compressed_vector<std::complex<mp_test_type> >, 3>()();
227 std::cout << "std::complex<double> compressed" << std::endl;
228 test_my_vector<ublas::compressed_vector<std::complex<double> >, 3>()();
233 #ifdef USE_COORDINATE_VECTOR
235 std::cout << "mp_test_type coordinate" << std::endl;
236 test_my_vector<ublas::coordinate_vector<mp_test_type>, 3>()();
240 std::cout << "double coordinate" << std::endl;
241 test_my_vector<ublas::coordinate_vector<double>, 3>()();
244 #ifdef USE_STD_COMPLEX
246 std::cout << "std::complex<mp_test_type> coordinate" << std::endl;
247 test_my_vector<ublas::coordinate_vector<std::complex<mp_test_type> >, 3>()();
251 std::cout << "std::complex<double> coordinate" << std::endl;
252 test_my_vector<ublas::coordinate_vector<std::complex<double> >, 3>()();