~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/numeric/ublas/test/test11.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  Copyright (c) 2000-2002
 
3
//  Joerg Walter, Mathias Koch
 
4
//
 
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)
 
8
//
 
9
//  The authors gratefully acknowledge the support of
 
10
//  GeNeSys mbH & Co. KG in producing this work.
 
11
//
 
12
 
 
13
#include "test1.hpp"
 
14
 
 
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;
 
21
 
 
22
    template<class VP>
 
23
    void test_container_with (VP &v1) const {
 
24
        // Container type tests in addition to expression types
 
25
        // Insert and erase
 
26
        v1.insert_element (0, 55);
 
27
        v1.erase_element (1);
 
28
        v1.clear ();
 
29
    }
 
30
    
 
31
    template<class VP>
 
32
    void test_expression_with (VP &v1, VP &v2, VP &v3) const {
 
33
        // Expression type tests
 
34
        value_type t;
 
35
        size_type i;
 
36
        real_type n;
 
37
 
 
38
        // Default Construct
 
39
        default_construct<VP>::test ();
 
40
        
 
41
        // Copy and swap
 
42
        initialize_vector (v1);
 
43
        initialize_vector (v2);
 
44
        v1 = v2;
 
45
        std::cout << "v1 = v2 = " << v1 << std::endl;
 
46
        v1.assign_temporary (v2);
 
47
        std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
 
48
        v1.swap (v2);
 
49
        std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
 
50
        
 
51
        // Zero assignment
 
52
        v1 = ublas::zero_vector<> (v1.size ());
 
53
        std::cout << "v1.zero_vector = " << v1 << std::endl;
 
54
        v1 = v2;
 
55
 
 
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;
 
65
#endif
 
66
 
 
67
            // Unary vector operations resulting in a vector
 
68
        initialize_vector (v1);
 
69
        v2 = - v1;
 
70
        std::cout << "- v1 = " << v2 << std::endl;
 
71
        v2 = ublas::conj (v1);
 
72
        std::cout << "conj (v1) = " << v2 << std::endl;
 
73
 
 
74
        // Binary vector operations resulting in a vector
 
75
        initialize_vector (v1);
 
76
        initialize_vector (v2);
 
77
        v3 = v1 + v2;
 
78
        std::cout << "v1 + v2 = " << v3 << std::endl;
 
79
        v3 = v1 - v2;
 
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;
 
83
 
 
84
        // Scaling a vector
 
85
        t = N;
 
86
        initialize_vector (v1);
 
87
        v2 = value_type (1.) * v1;
 
88
        std::cout << "1. * v1 = " << v2 << std::endl;
 
89
        v2 = t * v1;
 
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;
 
94
        v2 = v1 * t;
 
95
          std::cout << "v1 * value_type(N) = " << v2 << std::endl;
 
96
          // test interop with integer
 
97
          v2 = v1 * N;
 
98
 
 
99
        std::cout << "v1 * N = " << v2 << std::endl;
 
100
 
 
101
        // Some assignments
 
102
        initialize_vector (v1);
 
103
        initialize_vector (v2);
 
104
        v2 += v1;
 
105
        std::cout << "v2 += v1 = " << v2 << std::endl;
 
106
        v2 -= v1;
 
107
        std::cout << "v2 -= v1 = " << v2 << std::endl;
 
108
        v2 = v2 + v1;
 
109
        std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
 
110
        v2 = v2 - v1;
 
111
        std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
 
112
        v1 *= value_type (1.);
 
113
        std::cout << "v1 *= 1. = " << v1 << std::endl;
 
114
        v1 *= t;
 
115
          std::cout << "v1 *= value_type(N) = " << v1 << std::endl;
 
116
          // test interop with integer
 
117
          v1 *= N;
 
118
        std::cout << "v1 *= N = " << v1 << std::endl;
 
119
 
 
120
        // Unary vector operations resulting in a scalar
 
121
        initialize_vector (v1);
 
122
        t = ublas::sum (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;
 
130
 
 
131
        i = ublas::index_norm_inf (v1);
 
132
        std::cout << "index_norm_inf (v1) = " << i << std::endl;
 
133
 
 
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;
 
139
 
 
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;
 
145
    }
 
146
 
 
147
    void operator () () const {
 
148
        V v1 (N), v2 (N), v3 (N);
 
149
        test_expression_with (v1, v2, v3);
 
150
        test_container_with (v1);
 
151
 
 
152
#ifdef USE_RANGE
 
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);
 
157
#endif
 
158
 
 
159
#ifdef USE_SLICE
 
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);
 
164
#endif
 
165
    }
 
166
};
 
167
 
 
168
// Test vector
 
169
void test_vector () {
 
170
    std::cout << "test_vector" << std::endl;
 
171
 
 
172
#ifdef USE_BOUNDED_ARRAY
 
173
#ifdef USE_FLOAT
 
174
    std::cout << "float, bounded_array" << std::endl;
 
175
    test_my_vector<ublas::vector<float, ublas::bounded_array<float, 3> >, 3 > () ();
 
176
#endif
 
177
 
 
178
#ifdef USE_DOUBLE
 
179
    std::cout << "double, bounded_array" << std::endl;
 
180
    test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3 > () ();
 
181
#endif
 
182
 
 
183
#ifdef USE_STD_COMPLEX
 
184
#ifdef USE_FLOAT
 
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 > () ();
 
187
#endif
 
188
 
 
189
#ifdef USE_DOUBLE
 
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 > () ();
 
192
#endif
 
193
#endif
 
194
#endif
 
195
 
 
196
#ifdef USE_UNBOUNDED_ARRAY
 
197
#ifdef USE_FLOAT
 
198
    std::cout << "float, unbounded_array" << std::endl;
 
199
    test_my_vector<ublas::vector<float, ublas::unbounded_array<float> >, 3 > () ();
 
200
#endif
 
201
 
 
202
#ifdef USE_DOUBLE
 
203
    std::cout << "double, unbounded_array" << std::endl;
 
204
    test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3 > () ();
 
205
#endif
 
206
 
 
207
#ifdef USE_STD_COMPLEX
 
208
#ifdef USE_FLOAT
 
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 > () ();
 
211
#endif
 
212
 
 
213
#ifdef USE_DOUBLE
 
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 > () ();
 
216
#endif
 
217
#endif
 
218
#endif
 
219
 
 
220
#ifdef USE_STD_VECTOR
 
221
#ifdef USE_FLOAT
 
222
    std::cout << "float, std::vector" << std::endl;
 
223
    test_my_vector<ublas::vector<float, std::vector<float> >, 3 > () ();
 
224
#endif
 
225
 
 
226
#ifdef USE_DOUBLE
 
227
    std::cout << "double, std::vector" << std::endl;
 
228
    test_my_vector<ublas::vector<double, std::vector<double> >, 3 > () ();
 
229
#endif
 
230
 
 
231
#ifdef USE_STD_COMPLEX
 
232
#ifdef USE_FLOAT
 
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 > () ();
 
235
#endif
 
236
 
 
237
#ifdef USE_DOUBLE
 
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 > () ();
 
240
#endif
 
241
#endif
 
242
#endif
 
243
 
 
244
#ifdef USE_BOUNDED_VECTOR
 
245
#ifdef USE_FLOAT
 
246
    std::cout << "float, bounded" << std::endl;
 
247
    test_my_vector<ublas::bounded_vector<float, 3>, 3> () ();
 
248
#endif
 
249
 
 
250
#ifdef USE_DOUBLE
 
251
    std::cout << "double, bounded" << std::endl;
 
252
    test_my_vector<ublas::bounded_vector<double, 3>, 3> () ();
 
253
#endif
 
254
 
 
255
#ifdef USE_STD_COMPLEX
 
256
#ifdef USE_FLOAT
 
257
    std::cout << "std::complex<float>, bounded" << std::endl;
 
258
    test_my_vector<ublas::bounded_vector<std::complex<float>, 3>, 3> () ();
 
259
#endif
 
260
 
 
261
#ifdef USE_DOUBLE
 
262
    std::cout << "std::complex<double>, bounded" << std::endl;
 
263
    test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3> () ();
 
264
#endif
 
265
#endif
 
266
#endif
 
267
}