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.
15
// Test vector expression templates
16
template<class V, int N>
17
struct test_my_vector {
18
typedef typename V::value_type value_type;
19
typedef typename V::size_type size_type;
20
typedef typename ublas::type_traits<value_type>::real_type real_type;
23
void test_container_with (VP &v1) const {
24
// Container type tests in addition to expression types
26
v1.insert_element (0, 55);
32
void test_expression_with (VP &v1, VP &v2, VP &v3) const {
33
// Expression type tests
39
default_construct<VP>::test ();
42
initialize_vector (v1);
43
initialize_vector (v2);
45
std::cout << "v1 = v2 = " << v1 << std::endl;
46
v1.assign_temporary (v2);
47
std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
49
std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
52
v1 = ublas::zero_vector<> (v1.size ());
53
std::cout << "v1.zero_vector = " << v1 << std::endl;
56
#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
57
// Project range and slice
58
initialize_vector (v1);
59
initialize_vector (v2);
60
project (v1, ublas::range(0,1)) = project (v2, ublas::range(0,1));
61
project (v1, ublas::range(0,1)) = project (v2, ublas::slice(0,1,1));
62
project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::slice(0,1,2));
63
project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::range(0,2));
64
std::cout << "v1 = range/slice " << v1 << std::endl;
67
// Unary vector operations resulting in a vector
68
initialize_vector (v1);
70
std::cout << "- v1 = " << v2 << std::endl;
71
v2 = ublas::conj (v1);
72
std::cout << "conj (v1) = " << v2 << std::endl;
74
// Binary vector operations resulting in a vector
75
initialize_vector (v1);
76
initialize_vector (v2);
78
std::cout << "v1 + v2 = " << v3 << std::endl;
80
std::cout << "v1 - v2 = " << v3 << std::endl;
81
v3 = ublas::element_prod (v1, v2);
82
std::cout << "element_prod (v1, v2) = " << v3 << std::endl;
86
initialize_vector (v1);
87
v2 = value_type (1.) * v1;
88
std::cout << "1. * v1 = " << v2 << std::endl;
90
std::cout << "N * v1 = " << v2 << std::endl;
91
initialize_vector (v1);
92
v2 = v1 * value_type (1.);
93
std::cout << "v1 * 1. = " << v2 << std::endl;
95
std::cout << "v1 * value_type(N) = " << v2 << std::endl;
96
// test interop with integer
99
std::cout << "v1 * N = " << v2 << std::endl;
102
initialize_vector (v1);
103
initialize_vector (v2);
105
std::cout << "v2 += v1 = " << v2 << std::endl;
107
std::cout << "v2 -= v1 = " << v2 << std::endl;
109
std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
111
std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
112
v1 *= value_type (1.);
113
std::cout << "v1 *= 1. = " << v1 << std::endl;
115
std::cout << "v1 *= value_type(N) = " << v1 << std::endl;
116
// test interop with integer
118
std::cout << "v1 *= N = " << v1 << std::endl;
120
// Unary vector operations resulting in a scalar
121
initialize_vector (v1);
123
std::cout << "sum (v1) = " << t << std::endl;
124
n = ublas::norm_1 (v1);
125
std::cout << "norm_1 (v1) = " << n << std::endl;
126
n = ublas::norm_2 (v1);
127
std::cout << "norm_2 (v1) = " << n << std::endl;
128
n = ublas::norm_inf (v1);
129
std::cout << "norm_inf (v1) = " << n << std::endl;
131
i = ublas::index_norm_inf (v1);
132
std::cout << "index_norm_inf (v1) = " << i << std::endl;
134
// Binary vector operations resulting in a scalar
135
initialize_vector (v1);
136
initialize_vector (v2);
137
t = ublas::inner_prod (v1, v2);
138
std::cout << "inner_prod (v1, v2) = " << t << std::endl;
140
// Scalar and Binary vector expression resulting in a vector
141
initialize_vector (v1);
142
initialize_vector (v2);
143
v1 = v1 * ublas::inner_prod (v1, v2);
144
std::cout << "v1 * inner_prod (v1, v2) = " << v1 << std::endl;
147
void operator () () const {
148
V v1 (N), v2 (N), v3 (N);
149
test_expression_with (v1, v2, v3);
150
test_container_with (v1);
153
ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
154
vr2 (v2, ublas::range (0, N)),
155
vr3 (v3, ublas::range (0, N));
156
test_expression_with (vr1, vr2, vr3);
160
ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
161
vs2 (v2, ublas::slice (0, 1, N)),
162
vs3 (v3, ublas::slice (0, 1, N));
163
test_expression_with (vs1, vs2, vs3);
169
void test_vector () {
170
std::cout << "test_vector" << std::endl;
172
#ifdef USE_BOUNDED_ARRAY
174
std::cout << "float, bounded_array" << std::endl;
175
test_my_vector<ublas::vector<float, ublas::bounded_array<float, 3> >, 3 > () ();
179
std::cout << "double, bounded_array" << std::endl;
180
test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3 > () ();
183
#ifdef USE_STD_COMPLEX
185
std::cout << "std::complex<float>, bounded_array" << std::endl;
186
test_my_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >, 3 > () ();
190
std::cout << "std::complex<double>, bounded_array" << std::endl;
191
test_my_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >, 3 > () ();
196
#ifdef USE_UNBOUNDED_ARRAY
198
std::cout << "float, unbounded_array" << std::endl;
199
test_my_vector<ublas::vector<float, ublas::unbounded_array<float> >, 3 > () ();
203
std::cout << "double, unbounded_array" << std::endl;
204
test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3 > () ();
207
#ifdef USE_STD_COMPLEX
209
std::cout << "std::complex<float>, unbounded_array" << std::endl;
210
test_my_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >, 3 > () ();
214
std::cout << "std::complex<double>, unbounded_array" << std::endl;
215
test_my_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >, 3 > () ();
220
#ifdef USE_STD_VECTOR
222
std::cout << "float, std::vector" << std::endl;
223
test_my_vector<ublas::vector<float, std::vector<float> >, 3 > () ();
227
std::cout << "double, std::vector" << std::endl;
228
test_my_vector<ublas::vector<double, std::vector<double> >, 3 > () ();
231
#ifdef USE_STD_COMPLEX
233
std::cout << "std::complex<float>, std::vector" << std::endl;
234
test_my_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >, 3 > () ();
238
std::cout << "std::complex<double>, std::vector" << std::endl;
239
test_my_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >, 3 > () ();
244
#ifdef USE_BOUNDED_VECTOR
246
std::cout << "float, bounded" << std::endl;
247
test_my_vector<ublas::bounded_vector<float, 3>, 3> () ();
251
std::cout << "double, bounded" << std::endl;
252
test_my_vector<ublas::bounded_vector<double, 3>, 3> () ();
255
#ifdef USE_STD_COMPLEX
257
std::cout << "std::complex<float>, bounded" << std::endl;
258
test_my_vector<ublas::bounded_vector<std::complex<float>, 3>, 3> () ();
262
std::cout << "std::complex<double>, bounded" << std::endl;
263
test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3> () ();