ostream.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // The authors gratefully acknowledge the support of
  9. // Fraunhofer IOSB, Ettlingen, Germany
  10. //
  11. #ifndef BOOST_UBLAS_TENSOR_OSTREAM_HPP
  12. #define BOOST_UBLAS_TENSOR_OSTREAM_HPP
  13. #include <ostream>
  14. #include <complex>
  15. namespace boost {
  16. namespace numeric {
  17. namespace ublas {
  18. namespace detail {
  19. template <class value_type>
  20. void print(std::ostream& out, value_type const& p)
  21. {
  22. out << p << " ";
  23. }
  24. template <class value_type>
  25. void print(std::ostream& out, const std::complex<value_type>& p)
  26. {
  27. out << std::real(p) << "+" << std::imag(p) << "i ";
  28. }
  29. template <class size_type, class value_type>
  30. void print(std::ostream& out, size_type r, const value_type* p, const size_type* w, const size_type* n)
  31. {
  32. if(r < 2)
  33. {
  34. out << "[ ... " << std::endl;
  35. for(auto row = 0u; row < n[0]; p += w[0], ++row) // iterate over one column
  36. {
  37. auto p1 = p;
  38. for(auto col = 0u; col < n[1]; p1 += w[1], ++col) // iterate over first row
  39. {
  40. print(out,*p1);
  41. }
  42. if(row < n[0]-1)
  43. out << "; " << std::endl;
  44. }
  45. out << "]";
  46. }
  47. else
  48. {
  49. out << "cat("<< r+1 <<",..." << std::endl;
  50. for(auto d = 0u; d < n[r]-1; p += w[r], ++d){
  51. print(out, r-1, p, w, n);
  52. out << ",..." << std::endl;
  53. }
  54. print(out, r-1, p, w, n);
  55. }
  56. if(r>1)
  57. out << ")";
  58. }
  59. ////////////////////////////
  60. }
  61. }
  62. }
  63. }
  64. namespace boost {
  65. namespace numeric {
  66. namespace ublas {
  67. template<class T, class F, class A>
  68. class tensor;
  69. template<class T, class F, class A>
  70. class matrix;
  71. template<class T, class A>
  72. class vector;
  73. }
  74. }
  75. }
  76. template <class V, class F, class A>
  77. std::ostream& operator << (std::ostream& out, boost::numeric::ublas::tensor<V,F,A> const& t)
  78. {
  79. if(t.extents().is_scalar()){
  80. out << '[';
  81. boost::numeric::ublas::detail::print(out,t[0]);
  82. out << ']';
  83. }
  84. else if(t.extents().is_vector()) {
  85. const auto& cat = t.extents().at(0) > t.extents().at(1) ? ';' : ',';
  86. out << '[';
  87. for(auto i = 0u; i < t.size()-1; ++i){
  88. boost::numeric::ublas::detail::print(out,t[i]);
  89. out << cat << ' ';
  90. }
  91. boost::numeric::ublas::detail::print(out,t[t.size()-1]);
  92. out << ']';
  93. }
  94. else{
  95. boost::numeric::ublas::detail::print(out, t.rank()-1, t.data(), t.strides().data(), t.extents().data());
  96. }
  97. return out;
  98. }
  99. #endif