init.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. typename V::size_type size = v.size ();
  41. for (typename V::size_type i = 0; i < size; ++ i)
  42. v [i] = typename V::value_type ( i + 1.f );
  43. }
  44. template<class M>
  45. void initialize_matrix_impl (M &m, ublas::packed_proxy_tag) {
  46. typename M::size_type size1 = m.size1 ();
  47. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  48. for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++ i)
  49. for (typename M::iterator2 j = i.begin(); j != i.end(); ++ j)
  50. *j = typename M::value_type ( i.index1() * size1 + j.index2() + 1.f );
  51. #else
  52. for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++ i)
  53. for (typename M::iterator2 j = ublas::begin (i, ublas::iterator1_tag ()); j != ublas::end (i, ublas::iterator1_tag ()); ++ j)
  54. *j = typename M::value_type ( i.index1() * size1 + j.index2() + 1.f );
  55. #endif
  56. }
  57. template<class M>
  58. void initialize_matrix_impl (M &m, ublas::sparse_proxy_tag) {
  59. typename M::size_type size1 = m.size1 ();
  60. typename M::size_type size2 = m.size2 ();
  61. for (typename M::size_type i = 0; i < size1; ++ i)
  62. for (typename M::size_type j = 0; j < size2; ++ j)
  63. m (i, j) = typename M::value_type (i * size1 + j + 1.f);
  64. }
  65. template<class M>
  66. void initialize_matrix (M &m) {
  67. initialize_matrix_impl (m, typename M::storage_category());
  68. }
  69. template<class M>
  70. void initialize_matrix (M &m, ublas::lower_tag) {
  71. typename M::size_type size1 = m.size1 ();
  72. typename M::size_type size2 = m.size2 ();
  73. for (typename M::size_type i = 0; i < size1; ++ i) {
  74. typename M::size_type j = 0;
  75. for (; j <= i; ++ j)
  76. m (i, j) = i * size1 + j + 1.f;
  77. for (; j < size2; ++ j)
  78. m (i, j) = 0.f;
  79. }
  80. }
  81. template<class M>
  82. void initialize_matrix (M &m, ublas::upper_tag) {
  83. typename M::size_type size1 = m.size1 ();
  84. typename M::size_type size2 = m.size2 ();
  85. for (typename M::size_type i = 0; i < size1; ++ i) {
  86. typename M::size_type j = 0;
  87. for (; j < i; ++ j)
  88. m (i, j) = 0.f;
  89. for (; j < size2; ++ j)
  90. m (i, j) = i * size1 + j + 1.f;
  91. }
  92. }