summaryrefslogtreecommitdiff
path: root/libs/numeric/ublas/test/triangular_access.cpp
blob: 9777632be9380fe1e667963427a99dacb63725e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Test program to test find functions of triagular matrices
 *
 * author: Gunter Winkler ( guwi17 at gmx dot de )
 */
// Copyright 2008 Gunter Winkler <guwi17@gmx.de>
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/cstdlib.hpp>

#include "common/testhelper.hpp"

#ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
using boost::numeric::ublas::iterator1_tag;
using boost::numeric::ublas::iterator2_tag;
#endif

template < class MAT >
void test_iterator( MAT & A ) {

#ifndef NOMESSAGES
    std::cout << "=>";
#endif
  // check mutable iterators
  typename MAT::iterator1 it1 = A.begin1();
  typename MAT::iterator1 it1_end = A.end1();
  
  for ( ; it1 != it1_end; ++it1 ) {
#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
    typename MAT::iterator2 it2 = it1.begin();
    typename MAT::iterator2 it2_end = it1.end();
#else
    typename MAT::iterator2 it2 = begin(it1, iterator1_tag());
    typename MAT::iterator2 it2_end = end(it1, iterator1_tag());
#endif
    for ( ; it2 != it2_end ; ++ it2 ) {
#ifndef NOMESSAGES
      std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush;
#endif
      * it2 = ( 10 * it2.index1() + it2.index2() );
    }
#ifndef NOMESSAGES
    std::cout << std::endl;
#endif
  }

}

template < class MAT >
void test_iterator2( MAT & A ) {

#ifndef NOMESSAGES
    std::cout << "=>";
#endif
  // check mutable iterators
  typename MAT::iterator2 it2 = A.begin2();
  typename MAT::iterator2 it2_end = A.end2();
  
  for ( ; it2 != it2_end; ++it2 ) {
#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
    typename MAT::iterator1 it1 = it2.begin();
    typename MAT::iterator1 it1_end = it2.end();
#else
    typename MAT::iterator1 it1 = begin(it2, iterator2_tag());
    typename MAT::iterator1 it1_end = end(it2, iterator2_tag());
#endif
    for ( ; it1 != it1_end ; ++ it1 ) {
#ifndef NOMESSAGES
      std::cout << "( " << it1.index1() << ", " << it1.index2() << ") " << std::flush;
#endif
      * it1 = ( 10 * it1.index1() + it1.index2() );
    }
#ifndef NOMESSAGES
    std::cout << std::endl;
#endif
  }

}

template < class MAT >
typename MAT::value_type 
test_iterator3( const MAT & A ) {

#ifndef NOMESSAGES
    std::cout << "=>";
#endif
  typename MAT::value_type result = 0;

  // check mutable iterators
  typename MAT::const_iterator1 it1 = A.begin1();
  typename MAT::const_iterator1 it1_end = A.end1();
  
  for ( ; it1 != it1_end; ++it1 ) {
#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
    typename MAT::const_iterator2 it2 = it1.begin();
    typename MAT::const_iterator2 it2_end = it1.end();
#else
    typename MAT::const_iterator2 it2 = begin(it1, iterator1_tag());
    typename MAT::const_iterator2 it2_end = end(it1, iterator1_tag());
#endif
    for ( ; it2 != it2_end ; ++ it2 ) {
#ifndef NOMESSAGES
      std::cout << "( " << it2.index1() << ", " << it2.index2() << ") " << std::flush;
#endif
      result += * it2;
    }
#ifndef NOMESSAGES
    std::cout << std::endl;
#endif
  }
  return result;

}


int main (int argc, char * argv[]) {
    using namespace boost::numeric::ublas;

    typedef double VALUE_TYPE;
    typedef triangular_matrix<VALUE_TYPE, lower>        LT;
    typedef triangular_matrix<VALUE_TYPE, unit_lower>   ULT;
    typedef triangular_matrix<VALUE_TYPE, strict_lower> SLT;
    typedef triangular_matrix<VALUE_TYPE, upper>        UT;
    typedef triangular_matrix<VALUE_TYPE, unit_upper>   UUT;
    typedef triangular_matrix<VALUE_TYPE, strict_upper> SUT;

    LT A(5,5);

    test_iterator(A);
    test_iterator2(A);

    ULT B(5,5);

    test_iterator(B);
    test_iterator2(B);

    SLT C(5,5);

    test_iterator(C);
    test_iterator2(C);

    UT D(5,5);

    test_iterator(D);
    test_iterator2(D);

    UUT E(5,5);

    test_iterator(E);
    test_iterator2(E);

    SUT F(5,5);

    test_iterator(F);
    test_iterator2(F);

    assertTrue("Write access using iterators: ", true);

    assertEquals(" LT: ",420.0,test_iterator3(A));
    assertEquals("ULT: ",315.0,test_iterator3(B));
    assertEquals("SLT: ",310.0,test_iterator3(C));
    assertEquals(" UT: ",240.0,test_iterator3(D));
    assertEquals("UUT: ",135.0,test_iterator3(E));
    assertEquals("SUT: ",130.0,test_iterator3(F));

    assertTrue("Read access using iterators: ", true);

#ifndef NOMESSAGES
    std::cout << A << B << C << D << E << F << std::endl;
#endif

    typedef matrix<VALUE_TYPE> MATRIX;
    MATRIX mat(5,5);
    triangular_adaptor<MATRIX, lower> lta((mat));
    triangular_adaptor<MATRIX, unit_lower> ulta((mat));
    triangular_adaptor<MATRIX, strict_lower> slta((mat));
    triangular_adaptor<MATRIX, upper> uta((mat));
    triangular_adaptor<MATRIX, unit_upper> uuta((mat));
    triangular_adaptor<MATRIX, strict_upper> suta((mat));

    test_iterator ( lta );
    test_iterator2( lta );

    test_iterator ( ulta );
    test_iterator2( ulta );

    test_iterator ( slta );
    test_iterator2( slta );

    test_iterator ( uta );
    test_iterator2( uta );

    test_iterator ( uuta );
    test_iterator2( uuta );

    test_iterator ( suta );
    test_iterator2( suta );

    assertTrue("Write access using adaptors: ", true);

    assertEquals(" LTA: ",420.0,test_iterator3( lta ));
    assertEquals("ULTA: ",315.0,test_iterator3( ulta ));
    assertEquals("SLTA: ",310.0,test_iterator3( slta ));

    assertEquals(" UTA: ",240.0,test_iterator3( uta ));
    assertEquals("UUTA: ",135.0,test_iterator3( uuta ));
    assertEquals("SUTA: ",130.0,test_iterator3( suta ));

    assertTrue("Read access using adaptors: ", true);
    
#ifndef NOMESSAGES
    std::cout << mat << std::endl;
#endif

    return (getResults().second > 0) ? boost::exit_failure : boost::exit_success;
}