File indexing completed on 2025-12-16 10:14:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IO_H
0011 #define EIGEN_CXX11_TENSOR_TENSOR_IO_H
0012
0013 namespace Eigen {
0014
0015 namespace internal {
0016
0017
0018 template <typename Tensor, int Rank>
0019 struct TensorPrinter {
0020 static void run (std::ostream& os, const Tensor& tensor) {
0021 typedef typename internal::remove_const<typename Tensor::Scalar>::type Scalar;
0022 typedef typename Tensor::Index Index;
0023 const Index total_size = internal::array_prod(tensor.dimensions());
0024 if (total_size > 0) {
0025 const Index first_dim = Eigen::internal::array_get<0>(tensor.dimensions());
0026 static const int layout = Tensor::Layout;
0027 Map<const Array<Scalar, Dynamic, Dynamic, layout> > matrix(const_cast<Scalar*>(tensor.data()), first_dim, total_size/first_dim);
0028 os << matrix;
0029 }
0030 }
0031 };
0032
0033
0034
0035 template <typename Tensor>
0036 struct TensorPrinter<Tensor, 1> {
0037 static void run (std::ostream& os, const Tensor& tensor) {
0038 typedef typename internal::remove_const<typename Tensor::Scalar>::type Scalar;
0039 typedef typename Tensor::Index Index;
0040 const Index total_size = internal::array_prod(tensor.dimensions());
0041 if (total_size > 0) {
0042 Map<const Array<Scalar, Dynamic, 1> > array(const_cast<Scalar*>(tensor.data()), total_size);
0043 os << array;
0044 }
0045 }
0046 };
0047
0048
0049
0050 template <typename Tensor>
0051 struct TensorPrinter<Tensor, 0> {
0052 static void run (std::ostream& os, const Tensor& tensor) {
0053 os << tensor.coeff(0);
0054 }
0055 };
0056 }
0057
0058 template <typename T>
0059 std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
0060 typedef TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> Evaluator;
0061 typedef typename Evaluator::Dimensions Dimensions;
0062
0063
0064 TensorForcedEvalOp<const T> eval = expr.eval();
0065 Evaluator tensor(eval, DefaultDevice());
0066 tensor.evalSubExprsIfNeeded(NULL);
0067
0068
0069 static const int rank = internal::array_size<Dimensions>::value;
0070 internal::TensorPrinter<Evaluator, rank>::run(os, tensor);
0071
0072
0073 tensor.cleanup();
0074 return os;
0075 }
0076
0077 }
0078
0079 #endif