init.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2004 Michael Stevens
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*
  8. * Default construct test when possible
  9. */
  10. template <class E>
  11. struct default_construct
  12. {
  13. static void test() {}
  14. };
  15. template <class VC>
  16. struct default_construct<boost::numeric::ublas::vector_container<VC> >
  17. {
  18. static void test()
  19. {
  20. VC default_constuct;
  21. initialize_vector(default_constuct);
  22. std::cout << "default construct = " << default_constuct << std::endl;
  23. }
  24. };
  25. template <class MC>
  26. struct default_construct<boost::numeric::ublas::matrix_container<MC> >
  27. {
  28. static void test()
  29. {
  30. MC default_constuct;
  31. initialize_vector(default_constuct);
  32. std::cout << "default construct = " << default_constuct << std::endl;
  33. }
  34. };
  35. /*
  36. * Initialise test values in vector/matrix
  37. */
  38. template <class V>
  39. void initialize_vector(V& v)
  40. {
  41. typename V::size_type size = v.size();
  42. for (typename V::size_type i = 0; i < size; ++i)
  43. v[i] = typename V::value_type(i + 1.f);
  44. }
  45. template <class M>
  46. void initialize_matrix_impl(M& m, ublas::packed_proxy_tag)
  47. {
  48. typename M::size_type size1 = m.size1();
  49. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  50. for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++i)
  51. for (typename M::iterator2 j = i.begin(); j != i.end(); ++j)
  52. *j = typename M::value_type(i.index1() * size1 + j.index2() + 1.f);
  53. #else
  54. for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++i)
  55. for (typename M::iterator2 j = ublas::begin(i, ublas::iterator1_tag()); j != ublas::end(i, ublas::iterator1_tag()); ++j)
  56. *j = typename M::value_type(i.index1() * size1 + j.index2() + 1.f);
  57. #endif
  58. }
  59. template <class M>
  60. void initialize_matrix_impl(M& m, ublas::sparse_proxy_tag)
  61. {
  62. typename M::size_type size1 = m.size1();
  63. typename M::size_type size2 = m.size2();
  64. for (typename M::size_type i = 0; i < size1; ++i)
  65. for (typename M::size_type j = 0; j < size2; ++j)
  66. m(i, j) = typename M::value_type(i * size1 + j + 1.f);
  67. }
  68. template <class M>
  69. void initialize_matrix(M& m)
  70. {
  71. initialize_matrix_impl(m, typename M::storage_category());
  72. }
  73. template <class M>
  74. void initialize_matrix(M& m, ublas::lower_tag)
  75. {
  76. typename M::size_type size1 = m.size1();
  77. typename M::size_type size2 = m.size2();
  78. for (typename M::size_type i = 0; i < size1; ++i)
  79. {
  80. typename M::size_type j = 0;
  81. for (; j <= i; ++j)
  82. m(i, j) = i * size1 + j + 1.f;
  83. for (; j < size2; ++j)
  84. m(i, j) = 0.f;
  85. }
  86. }
  87. template <class M>
  88. void initialize_matrix(M& m, ublas::upper_tag)
  89. {
  90. typename M::size_type size1 = m.size1();
  91. typename M::size_type size2 = m.size2();
  92. for (typename M::size_type i = 0; i < size1; ++i)
  93. {
  94. typename M::size_type j = 0;
  95. for (; j < i; ++j)
  96. m(i, j) = 0.f;
  97. for (; j < size2; ++j)
  98. m(i, j) = i * size1 + j + 1.f;
  99. }
  100. }