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
12#include "test7.hpp"
13
14// Test vector expression templates
15template<class V, int N>
16struct test_my_vector {
17 typedef typename V::value_type value_type;
18 typedef typename V::size_type size_type;
19 typedef typename ublas::type_traits<value_type>::real_type real_type;
20
21 template<class VP>
22 void test_with (VP &v1, VP &v2, VP &v3) const {
23 {
24 value_type t;
25 size_type i;
26 real_type n;
27
28 // Copy and swap
29 initialize_vector (v1);
30 initialize_vector (v2);
31 v1 = v2;
32 std::cout << "v1 = v2 = " << v1 << std::endl;
33 v1.assign_temporary (v2);
34 std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
35 v1.swap (v2);
36 std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
37
38 // Zero assignment
39 v1 = ublas::zero_vector<value_type> (v1.size ());
40 std::cout << "v1.zero_vector = " << v1 << std::endl;
41 v1 = v2;
42
43 // Unary vector operations resulting in a vector
44 initialize_vector (v1);
45 v2 = - v1;
46 std::cout << "- v1 = " << v2 << std::endl;
47 v2 = ublas::conj (v1);
48 std::cout << "conj (v1) = " << v2 << std::endl;
49
50 // Binary vector operations resulting in a vector
51 initialize_vector (v1);
52 initialize_vector (v2);
53 v3 = v1 + v2;
54 std::cout << "v1 + v2 = " << v3 << std::endl;
55
56 v3 = v1 - v2;
57 std::cout << "v1 - v2 = " << v3 << std::endl;
58
59 // Scaling a vector
60 t = value_type (N);
61 initialize_vector (v1);
62 v2 = value_type (1.) * v1;
63 std::cout << "1. * v1 = " << v2 << std::endl;
64// v2 = t * v1;
65 std::cout << "N * v1 = " << v2 << std::endl;
66 initialize_vector (v1);
67// v2 = v1 * value_type (1.);
68 std::cout << "v1 * 1. = " << v2 << std::endl;
69// v2 = v1 * t;
70 std::cout << "v1 * N = " << v2 << std::endl;
71
72 // Some assignments
73 initialize_vector (v1);
74 initialize_vector (v2);
75 v2 += v1;
76 std::cout << "v2 += v1 = " << v2 << std::endl;
77 v2 -= v1;
78 std::cout << "v2 -= v1 = " << v2 << std::endl;
79 v2 = v2 + v1;
80 std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
81 v2 = v2 - v1;
82 std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
83 v1 *= value_type (1.);
84 std::cout << "v1 *= 1. = " << v1 << std::endl;
85 v1 *= t;
86 std::cout << "v1 *= N = " << v1 << std::endl;
87
88 // Unary vector operations resulting in a scalar
89 initialize_vector (v1);
90 t = ublas::sum (v1);
91 std::cout << "sum (v1) = " << t << std::endl;
92 n = ublas::norm_1 (v1);
93 std::cout << "norm_1 (v1) = " << n << std::endl;
94 n = ublas::norm_2 (v1);
95 std::cout << "norm_2 (v1) = " << n << std::endl;
96 n = ublas::norm_inf (v1);
97 std::cout << "norm_inf (v1) = " << n << std::endl;
98
99 i = ublas::index_norm_inf (v1);
100 std::cout << "index_norm_inf (v1) = " << i << std::endl;
101
102 // Binary vector operations resulting in a scalar
103 initialize_vector (v1);
104 initialize_vector (v2);
105 t = ublas::inner_prod (v1, v2);
106 std::cout << "inner_prod (v1, v2) = " << t << std::endl;
107 }
108 }
109 void operator () () const {
110 {
111 V v1 (N), v2 (N), v3 (N);
112 test_with (v1, v2, v3);
113
114#ifdef USE_RANGE
115 ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
116 vr2 (v2, ublas::range (0, N)),
117 vr3 (v3, ublas::range (0, N));
118 test_with (vr1, vr2, vr3);
119#endif
120
121#ifdef USE_SLICE
122 ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
123 vs2 (v2, ublas::slice (0, 1, N)),
124 vs3 (v3, ublas::slice (0, 1, N));
125 test_with (vs1, vs2, vs3);
126#endif
127 }
128 }
129};
130
131// Test vector
132void test_vector () {
133 std::cout << "test_vector" << std::endl;
134
135#ifdef USE_BOUNDED_ARRAY
136#ifdef USE_FLOAT
137 std::cout << "boost::numeric::interval<float>, bounded_array" << std::endl;
138 test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::bounded_array<boost::numeric::interval<float>, 3> >, 3 > () ();
139#endif
140
141#ifdef USE_DOUBLE
142 std::cout << "boost::numeric::interval<double>, bounded_array" << std::endl;
143 test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::bounded_array<boost::numeric::interval<double>, 3> >, 3 > () ();
144#endif
145#endif
146
147#ifdef USE_UNBOUNDED_ARRAY
148#ifdef USE_FLOAT
149 std::cout << "boost::numeric::interval<float>, unbounded_array" << std::endl;
150 test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::unbounded_array<boost::numeric::interval<float> > >, 3 > () ();
151#endif
152
153#ifdef USE_DOUBLE
154 std::cout << "boost::numeric::interval<double>, unbounded_array" << std::endl;
155 test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::unbounded_array<boost::numeric::interval<double> > >, 3 > () ();
156#endif
157#endif
158
159#ifdef USE_STD_VECTOR
160#ifdef USE_FLOAT
161 std::cout << "boost::numeric::interval<float>, std::vector" << std::endl;
162 test_my_vector<ublas::vector<boost::numeric::interval<float>, std::vector<boost::numeric::interval<float> > >, 3 > () ();
163#endif
164
165#ifdef USE_DOUBLE
166 std::cout << "boost::numeric::interval<double>, std::vector" << std::endl;
167 test_my_vector<ublas::vector<boost::numeric::interval<double>, std::vector<boost::numeric::interval<double> > >, 3 > () ();
168#endif
169#endif
170}
171

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