File indexing completed on 2025-01-18 09:43:03
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_UBLAS_TENSOR_OSTREAM_HPP
0013 #define BOOST_UBLAS_TENSOR_OSTREAM_HPP
0014
0015 #include <ostream>
0016 #include <complex>
0017
0018 namespace boost {
0019 namespace numeric {
0020 namespace ublas {
0021 namespace detail {
0022
0023 template <class value_type>
0024 void print(std::ostream& out, value_type const& p)
0025 {
0026 out << p << " ";
0027 }
0028
0029 template <class value_type>
0030 void print(std::ostream& out, const std::complex<value_type>& p)
0031 {
0032 out << std::real(p) << "+" << std::imag(p) << "i ";
0033 }
0034
0035
0036 template <class size_type, class value_type>
0037 void print(std::ostream& out, size_type r, const value_type* p, const size_type* w, const size_type* n)
0038 {
0039
0040 if(r < 2)
0041 {
0042 out << "[ ... " << std::endl;
0043
0044 for(auto row = 0u; row < n[0]; p += w[0], ++row)
0045 {
0046 auto p1 = p;
0047 for(auto col = 0u; col < n[1]; p1 += w[1], ++col)
0048 {
0049 print(out,*p1);
0050 }
0051 if(row < n[0]-1)
0052 out << "; " << std::endl;
0053 }
0054 out << "]";
0055 }
0056 else
0057 {
0058 out << "cat("<< r+1 <<",..." << std::endl;
0059 for(auto d = 0u; d < n[r]-1; p += w[r], ++d){
0060 print(out, r-1, p, w, n);
0061 out << ",..." << std::endl;
0062 }
0063 print(out, r-1, p, w, n);
0064 }
0065 if(r>1)
0066 out << ")";
0067 }
0068
0069
0070
0071
0072 }
0073 }
0074 }
0075 }
0076
0077
0078 namespace boost {
0079 namespace numeric {
0080 namespace ublas {
0081
0082 template<class T, class F, class A>
0083 class tensor;
0084
0085 template<class T, class F, class A>
0086 class matrix;
0087
0088 template<class T, class A>
0089 class vector;
0090
0091 }
0092 }
0093 }
0094
0095
0096 template <class V, class F, class A>
0097 std::ostream& operator << (std::ostream& out, boost::numeric::ublas::tensor<V,F,A> const& t)
0098 {
0099
0100 if(t.extents().is_scalar()){
0101 out << '[';
0102 boost::numeric::ublas::detail::print(out,t[0]);
0103 out << ']';
0104 }
0105 else if(t.extents().is_vector()) {
0106 const auto& cat = t.extents().at(0) > t.extents().at(1) ? ';' : ',';
0107 out << '[';
0108 for(auto i = 0u; i < t.size()-1; ++i){
0109 boost::numeric::ublas::detail::print(out,t[i]);
0110 out << cat << ' ';
0111 }
0112 boost::numeric::ublas::detail::print(out,t[t.size()-1]);
0113 out << ']';
0114 }
0115 else{
0116 boost::numeric::ublas::detail::print(out, t.rank()-1, t.data(), t.strides().data(), t.extents().data());
0117 }
0118 return out;
0119 }
0120
0121
0122 #endif