Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/numeric/ublas/test/test7/test71.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 6.2 KB
Line 
1//  Copyright (c) 2000-2002
2//  Joerg Walter, Mathias Koch
3//
4//  Permission to use, copy, modify, distribute and sell this software
5//  and its documentation for any purpose is hereby granted without fee,
6//  provided that the above copyright notice appear in all copies and
7//  that both that copyright notice and this permission notice appear
8//  in supporting documentation.  The authors make no representations
9//  about the suitability of this software for any purpose.
10//  It is provided "as is" without express or implied warranty.
11//
12//  The authors gratefully acknowledge the support of
13//  GeNeSys mbH & Co. KG in producing this work.
14//
15
16#include "test7.hpp"
17
18// Test vector expression templates
19template<class V, int N>
20struct test_my_vector {
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;
24
25    template<class VP>
26    void test_with (VP &v1, VP &v2, VP &v3) const {
27        {
28            value_type t;
29            size_type i;
30            real_type n;
31
32            // Copy and swap
33            initialize_vector (v1);
34            initialize_vector (v2);
35            v1 = v2;
36            std::cout << "v1 = v2 = " << v1 << std::endl;
37            v1.assign_temporary (v2);
38            std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
39            v1.swap (v2);
40            std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
41
42            // Unary vector operations resulting in a vector
43            initialize_vector (v1);
44            v2 = - v1;
45            std::cout << "- v1 = " << v2 << std::endl;
46            v2 = ublas::conj (v1);
47            std::cout << "conj (v1) = " << v2 << std::endl;
48
49            // Binary vector operations resulting in a vector
50            initialize_vector (v1);
51            initialize_vector (v2);
52            v3 = v1 + v2;
53            std::cout << "v1 + v2 = " << v3 << std::endl;
54
55            v3 = v1 - v2;
56            std::cout << "v1 - v2 = " << v3 << std::endl;
57
58            // Scaling a vector
59            t = value_type (N);
60            initialize_vector (v1);
61            v2 = value_type (1.) * v1;
62            std::cout << "1. * v1 = " << v2 << std::endl;
63//            v2 = t * v1;
64            std::cout << "N * v1 = " << v2 << std::endl;
65            initialize_vector (v1);
66//            v2 = v1 * value_type (1.);
67            std::cout << "v1 * 1. = " << v2 << std::endl;
68//            v2 = v1 * t;
69            std::cout << "v1 * N = " << v2 << std::endl;
70
71            // Some assignments
72            initialize_vector (v1);
73            initialize_vector (v2);
74            v2 += v1;
75            std::cout << "v2 += v1 = " << v2 << std::endl;
76            v2 -= v1;
77            std::cout << "v2 -= v1 = " << v2 << std::endl;
78            v2 = v2 + v1;
79            std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
80            v2 = v2 - v1;
81            std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
82            v1 *= value_type (1.);
83            std::cout << "v1 *= 1. = " << v1 << std::endl;
84            v1 *= t;
85            std::cout << "v1 *= N = " << v1 << std::endl;
86
87            // Unary vector operations resulting in a scalar
88            initialize_vector (v1);
89            t = ublas::sum (v1);
90            std::cout << "sum (v1) = " << t << std::endl;
91            n = ublas::norm_1 (v1);
92            std::cout << "norm_1 (v1) = " << n << std::endl;
93            n = ublas::norm_2 (v1);
94            std::cout << "norm_2 (v1) = " << n << std::endl;
95            n = ublas::norm_inf (v1);
96            std::cout << "norm_inf (v1) = " << n << std::endl;
97
98            i = ublas::index_norm_inf (v1);
99            std::cout << "index_norm_inf (v1) = " << i << std::endl;
100
101            // Binary vector operations resulting in a scalar
102            initialize_vector (v1);
103            initialize_vector (v2);
104            t = ublas::inner_prod (v1, v2);
105            std::cout << "inner_prod (v1, v2) = " << t << std::endl;
106        }
107    }
108    void operator () () const {
109        {
110            V v1 (N), v2 (N), v3 (N);
111            test_with (v1, v2, v3);
112
113#ifdef USE_RANGE
114            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
115                                   vr2 (v2, ublas::range (0, N)),
116                                   vr3 (v3, ublas::range (0, N));
117            test_with (vr1, vr2, vr3);
118#endif
119
120#ifdef USE_SLICE
121            ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
122                                   vs2 (v2, ublas::slice (0, 1, N)),
123                                   vs3 (v3, ublas::slice (0, 1, N));
124            test_with (vs1, vs2, vs3);
125#endif
126        }
127    }
128};
129
130// Test vector
131void test_vector () {
132    std::cout << "test_vector" << std::endl;
133
134#ifdef USE_BOUNDED_ARRAY
135#ifdef USE_FLOAT
136    std::cout << "boost::numeric::interval<float>, bounded_array" << std::endl;
137    test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::bounded_array<boost::numeric::interval<float>, 3> >, 3 > () ();
138#endif
139
140#ifdef USE_DOUBLE
141    std::cout << "boost::numeric::interval<double>, bounded_array" << std::endl;
142    test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::bounded_array<boost::numeric::interval<double>, 3> >, 3 > () ();
143#endif
144#endif
145
146#ifdef USE_UNBOUNDED_ARRAY
147#ifdef USE_FLOAT
148    std::cout << "boost::numeric::interval<float>, unbounded_array" << std::endl;
149    test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::unbounded_array<boost::numeric::interval<float> > >, 3 > () ();
150#endif
151
152#ifdef USE_DOUBLE
153    std::cout << "boost::numeric::interval<double>, unbounded_array" << std::endl;
154    test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::unbounded_array<boost::numeric::interval<double> > >, 3 > () ();
155#endif
156#endif
157
158#ifdef USE_STD_VECTOR
159#ifdef USE_FLOAT
160    std::cout << "boost::numeric::interval<float>, std::vector" << std::endl;
161    test_my_vector<ublas::vector<boost::numeric::interval<float>, std::vector<boost::numeric::interval<float> > >, 3 > () ();
162#endif
163
164#ifdef USE_DOUBLE
165    std::cout << "boost::numeric::interval<double>, std::vector" << std::endl;
166    test_my_vector<ublas::vector<boost::numeric::interval<double>, std::vector<boost::numeric::interval<double> > >, 3 > () ();
167#endif
168#endif
169}
Note: See TracBrowser for help on using the repository browser.