begin_end.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2011 David Bellot
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <cmath>
  7. #include <boost/numeric/ublas/traits/const_iterator_type.hpp>
  8. #include <boost/numeric/ublas/traits/iterator_type.hpp>
  9. #include <boost/numeric/ublas/traits/c_array.hpp>
  10. #include <boost/numeric/ublas/fwd.hpp>
  11. #include <boost/numeric/ublas/matrix.hpp>
  12. #include <boost/numeric/ublas/matrix_expression.hpp>
  13. #include <boost/numeric/ublas/operation/begin.hpp>
  14. #include <boost/numeric/ublas/operation/end.hpp>
  15. #include <boost/numeric/ublas/tags.hpp>
  16. #include <boost/numeric/ublas/vector.hpp>
  17. #include <boost/numeric/ublas/vector_expression.hpp>
  18. #include <iostream>
  19. #include "utils.hpp"
  20. static const double TOL(1.0e-5); ///< Used for comparing two real numbers.
  21. #ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  22. #error "sorry this feature is not supported by your compiler"
  23. #endif
  24. BOOST_UBLAS_TEST_DEF( test_vector_iteration )
  25. {
  26. BOOST_UBLAS_DEBUG_TRACE( "TEST Vector Iteration" );
  27. typedef double value_type;
  28. typedef boost::numeric::ublas::vector<value_type> vector_type;
  29. vector_type v(5);
  30. v(0) = 0.555950;
  31. v(1) = 0.108929;
  32. v(2) = 0.948014;
  33. v(3) = 0.023787;
  34. v(4) = 1.023787;
  35. vector_type::size_type ix = 0;
  36. for (
  37. boost::numeric::ublas::iterator_type<vector_type>::type it = boost::numeric::ublas::begin<vector_type>(v);
  38. it != boost::numeric::ublas::end<vector_type>(v);
  39. ++it
  40. ) {
  41. BOOST_UBLAS_DEBUG_TRACE( "*it = " << *it << " ==> " << v(ix) );
  42. BOOST_UBLAS_TEST_CHECK( std::abs(*it - v(ix)) <= TOL );
  43. ++ix;
  44. }
  45. }
  46. BOOST_UBLAS_TEST_DEF( test_vector_const_iteration )
  47. {
  48. BOOST_UBLAS_DEBUG_TRACE( "TEST Vector Const Iteration" );
  49. typedef double value_type;
  50. typedef boost::numeric::ublas::vector<value_type> vector_type;
  51. vector_type v(5);
  52. v(0) = 0.555950;
  53. v(1) = 0.108929;
  54. v(2) = 0.948014;
  55. v(3) = 0.023787;
  56. v(4) = 1.023787;
  57. vector_type::size_type ix = 0;
  58. for (
  59. boost::numeric::ublas::const_iterator_type<vector_type>::type it = boost::numeric::ublas::begin<vector_type>(v);
  60. it != boost::numeric::ublas::end<vector_type>(v);
  61. ++it
  62. ) {
  63. BOOST_UBLAS_DEBUG_TRACE( "*it = " << *it << " ==> " << v(ix) );
  64. BOOST_UBLAS_TEST_CHECK( std::abs(*it - v(ix)) <= TOL );
  65. ++ix;
  66. }
  67. }
  68. BOOST_UBLAS_TEST_DEF( test_row_major_matrix_iteration )
  69. {
  70. BOOST_UBLAS_DEBUG_TRACE( "TEST Row-major Matrix Iteration" );
  71. typedef double value_type;
  72. typedef boost::numeric::ublas::matrix<value_type, boost::numeric::ublas::row_major> matrix_type;
  73. typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::major>::type outer_iterator_type;
  74. typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::minor>::type inner_iterator_type;
  75. matrix_type A(5,4);
  76. A(0,0) = 0.555950; A(0,1) = 0.274690; A(0,2) = 0.540605; A(0,3) = 0.798938;
  77. A(1,0) = 0.108929; A(1,1) = 0.830123; A(1,2) = 0.891726; A(1,3) = 0.895283;
  78. A(2,0) = 0.948014; A(2,1) = 0.973234; A(2,2) = 0.216504; A(2,3) = 0.883152;
  79. A(3,0) = 0.023787; A(3,1) = 0.675382; A(3,2) = 0.231751; A(3,3) = 0.450332;
  80. A(4,0) = 1.023787; A(4,1) = 1.675382; A(4,2) = 1.231751; A(4,3) = 1.450332;
  81. matrix_type::size_type row(0);
  82. for (
  83. outer_iterator_type outer_it = boost::numeric::ublas::begin<boost::numeric::ublas::tag::major>(A);
  84. outer_it != boost::numeric::ublas::end<boost::numeric::ublas::tag::major>(A);
  85. ++outer_it
  86. ) {
  87. matrix_type::size_type col(0);
  88. for (
  89. inner_iterator_type inner_it = boost::numeric::ublas::begin(outer_it);
  90. inner_it != boost::numeric::ublas::end(outer_it);
  91. ++inner_it
  92. ) {
  93. BOOST_UBLAS_DEBUG_TRACE( "*it = " << *inner_it << " ==> " << A(row,col) );
  94. BOOST_UBLAS_TEST_CHECK( std::abs(*inner_it - A(row,col)) <= TOL );
  95. ++col;
  96. }
  97. ++row;
  98. }
  99. }
  100. BOOST_UBLAS_TEST_DEF( test_col_major_matrix_iteration )
  101. {
  102. BOOST_UBLAS_DEBUG_TRACE( "TEST Column-major Matrix Iteration" );
  103. typedef double value_type;
  104. typedef boost::numeric::ublas::matrix<value_type, boost::numeric::ublas::column_major> matrix_type;
  105. typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::major>::type outer_iterator_type;
  106. typedef boost::numeric::ublas::iterator_type<matrix_type, boost::numeric::ublas::tag::minor>::type inner_iterator_type;
  107. matrix_type A(5,4);
  108. A(0,0) = 0.555950; A(0,1) = 0.274690; A(0,2) = 0.540605; A(0,3) = 0.798938;
  109. A(1,0) = 0.108929; A(1,1) = 0.830123; A(1,2) = 0.891726; A(1,3) = 0.895283;
  110. A(2,0) = 0.948014; A(2,1) = 0.973234; A(2,2) = 0.216504; A(2,3) = 0.883152;
  111. A(3,0) = 0.023787; A(3,1) = 0.675382; A(3,2) = 0.231751; A(3,3) = 0.450332;
  112. A(4,0) = 1.023787; A(4,1) = 1.675382; A(4,2) = 1.231751; A(4,3) = 1.450332;
  113. matrix_type::size_type col(0);
  114. for (
  115. outer_iterator_type outer_it = boost::numeric::ublas::begin<boost::numeric::ublas::tag::major>(A);
  116. outer_it != boost::numeric::ublas::end<boost::numeric::ublas::tag::major>(A);
  117. ++outer_it
  118. ) {
  119. matrix_type::size_type row(0);
  120. for (
  121. inner_iterator_type inner_it = boost::numeric::ublas::begin(outer_it);
  122. inner_it != boost::numeric::ublas::end(outer_it);
  123. ++inner_it
  124. ) {
  125. BOOST_UBLAS_DEBUG_TRACE( "*it = " << *inner_it << " ==> " << A(row,col) );
  126. BOOST_UBLAS_TEST_CHECK( std::abs(*inner_it - A(row,col)) <= TOL );
  127. ++row;
  128. }
  129. ++col;
  130. }
  131. }
  132. int main()
  133. {
  134. BOOST_UBLAS_TEST_BEGIN();
  135. BOOST_UBLAS_TEST_DO( test_vector_iteration );
  136. BOOST_UBLAS_TEST_DO( test_vector_const_iteration );
  137. BOOST_UBLAS_TEST_DO( test_row_major_matrix_iteration );
  138. BOOST_UBLAS_TEST_DO( test_col_major_matrix_iteration );
  139. BOOST_UBLAS_TEST_END();
  140. }