• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program to test find functions of triagular matrices
2  *
3  * author: Gunter Winkler ( guwi17 at gmx dot de )
4  */
5 // Copyright 2008 Gunter Winkler <guwi17@gmx.de>
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 
11 #include <boost/numeric/ublas/triangular.hpp>
12 #include <boost/numeric/ublas/io.hpp>
13 #include <boost/cstdlib.hpp>
14 
15 #include "common/testhelper.hpp"
16 
17 #ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
18 using boost::numeric::ublas::iterator1_tag;
19 using boost::numeric::ublas::iterator2_tag;
20 #endif
21 
22 template < class MAT >
test_iterator(MAT & A)23 void test_iterator( MAT & A ) {
24 
25 #ifndef NOMESSAGES
26     std::cout << "=>";
27 #endif
28   // check mutable iterators
29   typename MAT::iterator1 it1 = A.begin1();
30   typename MAT::iterator1 it1_end = A.end1();
31 
32   for ( ; it1 != it1_end; ++it1 ) {
33 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
34     typename MAT::iterator2 it2 = it1.begin();
35     typename MAT::iterator2 it2_end = it1.end();
36 #else
37     typename MAT::iterator2 it2 = begin(it1, iterator1_tag());
38     typename MAT::iterator2 it2_end = end(it1, iterator1_tag());
39 #endif
40     for ( ; it2 != it2_end ; ++ it2 ) {
41 #ifndef NOMESSAGES
42       std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush;
43 #endif
44       * it2 = ( 10 * it2.index1() + it2.index2() );
45     }
46 #ifndef NOMESSAGES
47     std::cout << std::endl;
48 #endif
49   }
50 
51 }
52 
53 template < class MAT >
test_iterator2(MAT & A)54 void test_iterator2( MAT & A ) {
55 
56 #ifndef NOMESSAGES
57     std::cout << "=>";
58 #endif
59   // check mutable iterators
60   typename MAT::iterator2 it2 = A.begin2();
61   typename MAT::iterator2 it2_end = A.end2();
62 
63   for ( ; it2 != it2_end; ++it2 ) {
64 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
65     typename MAT::iterator1 it1 = it2.begin();
66     typename MAT::iterator1 it1_end = it2.end();
67 #else
68     typename MAT::iterator1 it1 = begin(it2, iterator2_tag());
69     typename MAT::iterator1 it1_end = end(it2, iterator2_tag());
70 #endif
71     for ( ; it1 != it1_end ; ++ it1 ) {
72 #ifndef NOMESSAGES
73       std::cout << "( " << it1.index1() << ", " << it1.index2() << ") " << std::flush;
74 #endif
75       * it1 = ( 10 * it1.index1() + it1.index2() );
76     }
77 #ifndef NOMESSAGES
78     std::cout << std::endl;
79 #endif
80   }
81 
82 }
83 
84 template < class MAT >
85 typename MAT::value_type
test_iterator3(const MAT & A)86 test_iterator3( const MAT & A ) {
87 
88 #ifndef NOMESSAGES
89     std::cout << "=>";
90 #endif
91   typename MAT::value_type result = 0;
92 
93   // check mutable iterators
94   typename MAT::const_iterator1 it1 = A.begin1();
95   typename MAT::const_iterator1 it1_end = A.end1();
96 
97   for ( ; it1 != it1_end; ++it1 ) {
98 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
99     typename MAT::const_iterator2 it2 = it1.begin();
100     typename MAT::const_iterator2 it2_end = it1.end();
101 #else
102     typename MAT::const_iterator2 it2 = begin(it1, iterator1_tag());
103     typename MAT::const_iterator2 it2_end = end(it1, iterator1_tag());
104 #endif
105     for ( ; it2 != it2_end ; ++ it2 ) {
106 #ifndef NOMESSAGES
107       std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush;
108 #endif
109       result += * it2;
110     }
111 #ifndef NOMESSAGES
112     std::cout << std::endl;
113 #endif
114   }
115   return result;
116 
117 }
118 
119 
main()120 int main () {
121     using namespace boost::numeric::ublas;
122 
123     typedef double VALUE_TYPE;
124     typedef triangular_matrix<VALUE_TYPE, lower>        LT;
125     typedef triangular_matrix<VALUE_TYPE, unit_lower>   ULT;
126     typedef triangular_matrix<VALUE_TYPE, strict_lower> SLT;
127     typedef triangular_matrix<VALUE_TYPE, upper>        UT;
128     typedef triangular_matrix<VALUE_TYPE, unit_upper>   UUT;
129     typedef triangular_matrix<VALUE_TYPE, strict_upper> SUT;
130 
131     LT A(5,5);
132 
133     test_iterator(A);
134     test_iterator2(A);
135 
136     ULT B(5,5);
137 
138     test_iterator(B);
139     test_iterator2(B);
140 
141     SLT C(5,5);
142 
143     test_iterator(C);
144     test_iterator2(C);
145 
146     UT D(5,5);
147 
148     test_iterator(D);
149     test_iterator2(D);
150 
151     UUT E(5,5);
152 
153     test_iterator(E);
154     test_iterator2(E);
155 
156     SUT F(5,5);
157 
158     test_iterator(F);
159     test_iterator2(F);
160 
161     assertTrue("Write access using iterators: ", true);
162 
163     assertEquals(" LT: ",420.0,test_iterator3(A));
164     assertEquals("ULT: ",315.0,test_iterator3(B));
165     assertEquals("SLT: ",310.0,test_iterator3(C));
166     assertEquals(" UT: ",240.0,test_iterator3(D));
167     assertEquals("UUT: ",135.0,test_iterator3(E));
168     assertEquals("SUT: ",130.0,test_iterator3(F));
169 
170     assertTrue("Read access using iterators: ", true);
171 
172 #ifndef NOMESSAGES
173     std::cout << A << B << C << D << E << F << std::endl;
174 #endif
175 
176     typedef matrix<VALUE_TYPE> MATRIX;
177     MATRIX mat(5,5);
178     triangular_adaptor<MATRIX, lower> lta((mat));
179     triangular_adaptor<MATRIX, unit_lower> ulta((mat));
180     triangular_adaptor<MATRIX, strict_lower> slta((mat));
181     triangular_adaptor<MATRIX, upper> uta((mat));
182     triangular_adaptor<MATRIX, unit_upper> uuta((mat));
183     triangular_adaptor<MATRIX, strict_upper> suta((mat));
184 
185     test_iterator ( lta );
186     test_iterator2( lta );
187 
188     test_iterator ( ulta );
189     test_iterator2( ulta );
190 
191     test_iterator ( slta );
192     test_iterator2( slta );
193 
194     test_iterator ( uta );
195     test_iterator2( uta );
196 
197     test_iterator ( uuta );
198     test_iterator2( uuta );
199 
200     test_iterator ( suta );
201     test_iterator2( suta );
202 
203     assertTrue("Write access using adaptors: ", true);
204 
205     assertEquals(" LTA: ",420.0,test_iterator3( lta ));
206     assertEquals("ULTA: ",315.0,test_iterator3( ulta ));
207     assertEquals("SLTA: ",310.0,test_iterator3( slta ));
208 
209     assertEquals(" UTA: ",240.0,test_iterator3( uta ));
210     assertEquals("UUTA: ",135.0,test_iterator3( uuta ));
211     assertEquals("SUTA: ",130.0,test_iterator3( suta ));
212 
213     assertTrue("Read access using adaptors: ", true);
214 
215 #ifndef NOMESSAGES
216     std::cout << mat << std::endl;
217 #endif
218 
219     return (getResults().second > 0) ? boost::exit_failure : boost::exit_success;
220 }
221