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
18using boost::numeric::ublas::iterator1_tag;
19using boost::numeric::ublas::iterator2_tag;
20#endif
21
22template < class MAT >
23void 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
53template < class MAT >
54void 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
84template < class MAT >
85typename MAT::value_type
86test_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
120int 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(A&: B);
139 test_iterator2(A&: B);
140
141 SLT C(5,5);
142
143 test_iterator(A&: C);
144 test_iterator2(A&: C);
145
146 UT D(5,5);
147
148 test_iterator(A&: D);
149 test_iterator2(A&: D);
150
151 UUT E(5,5);
152
153 test_iterator(A&: E);
154 test_iterator2(A&: E);
155
156 SUT F(5,5);
157
158 test_iterator(A&: F);
159 test_iterator2(A&: F);
160
161 assertTrue(message: "Write access using iterators: ", condition: true);
162
163 assertEquals(message: " LT: ",expected: 420.0,actual: test_iterator3(A));
164 assertEquals(message: "ULT: ",expected: 315.0,actual: test_iterator3(A: B));
165 assertEquals(message: "SLT: ",expected: 310.0,actual: test_iterator3(A: C));
166 assertEquals(message: " UT: ",expected: 240.0,actual: test_iterator3(A: D));
167 assertEquals(message: "UUT: ",expected: 135.0,actual: test_iterator3(A: E));
168 assertEquals(message: "SUT: ",expected: 130.0,actual: test_iterator3(A: F));
169
170 assertTrue(message: "Read access using iterators: ", condition: 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 ( A&: lta );
186 test_iterator2( A&: lta );
187
188 test_iterator ( A&: ulta );
189 test_iterator2( A&: ulta );
190
191 test_iterator ( A&: slta );
192 test_iterator2( A&: slta );
193
194 test_iterator ( A&: uta );
195 test_iterator2( A&: uta );
196
197 test_iterator ( A&: uuta );
198 test_iterator2( A&: uuta );
199
200 test_iterator ( A&: suta );
201 test_iterator2( A&: suta );
202
203 assertTrue(message: "Write access using adaptors: ", condition: true);
204
205 assertEquals(message: " LTA: ",expected: 420.0,actual: test_iterator3( A: lta ));
206 assertEquals(message: "ULTA: ",expected: 315.0,actual: test_iterator3( A: ulta ));
207 assertEquals(message: "SLTA: ",expected: 310.0,actual: test_iterator3( A: slta ));
208
209 assertEquals(message: " UTA: ",expected: 240.0,actual: test_iterator3( A: uta ));
210 assertEquals(message: "UUTA: ",expected: 135.0,actual: test_iterator3( A: uuta ));
211 assertEquals(message: "SUTA: ",expected: 130.0,actual: test_iterator3( A: suta ));
212
213 assertTrue(message: "Read access using adaptors: ", condition: 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

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