TensorIO.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IO_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_IO_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 // Print the tensor as a 2d matrix
18 template <typename Tensor, int Rank>
19 struct TensorPrinter {
20  static void run (std::ostream& os, const Tensor& tensor) {
22  typedef typename Tensor::Index Index;
23  const Index total_size = internal::array_prod(tensor.dimensions());
24  if (total_size > 0) {
25  const Index first_dim = Eigen::internal::array_get<0>(tensor.dimensions());
26  static const int layout = Tensor::Layout;
27  Map<const Array<Scalar, Dynamic, Dynamic, layout> > matrix(const_cast<Scalar*>(tensor.data()), first_dim, total_size/first_dim);
28  os << matrix;
29  }
30  }
31 };
32 
33 
34 // Print the tensor as a vector
35 template <typename Tensor>
36 struct TensorPrinter<Tensor, 1> {
37  static void run (std::ostream& os, const Tensor& tensor) {
39  typedef typename Tensor::Index Index;
40  const Index total_size = internal::array_prod(tensor.dimensions());
41  if (total_size > 0) {
42  Map<const Array<Scalar, Dynamic, 1> > array(const_cast<Scalar*>(tensor.data()), total_size);
43  os << array;
44  }
45  }
46 };
47 
48 
49 // Print the tensor as a scalar
50 template <typename Tensor>
51 struct TensorPrinter<Tensor, 0> {
52  static void run (std::ostream& os, const Tensor& tensor) {
53  os << tensor.coeff(0);
54  }
55 };
56 }
57 
58 template <typename T>
59 std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
61  typedef typename Evaluator::Dimensions Dimensions;
62 
63  // Evaluate the expression if needed
64  TensorForcedEvalOp<const T> eval = expr.eval();
65  Evaluator tensor(eval, DefaultDevice());
66  tensor.evalSubExprsIfNeeded(NULL);
67 
68  // Print the result
69  static const int rank = internal::array_size<Dimensions>::value;
71 
72  // Cleanup.
73  tensor.cleanup();
74  return os;
75 }
76 
77 } // end namespace Eigen
78 
79 #endif // EIGEN_CXX11_TENSOR_TENSOR_IO_H
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:20
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
Definition: LDLT.h:16
A cost model used to limit the number of threads used for evaluating tensor expression.
internal::traits< Self >::Index Index
Definition: Tensor.h:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:37
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(const array< Index, NumIndices > &indices) const
Definition: Tensor.h:124
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: Tensor.h:102
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:52
The tensor class.
Definition: Tensor.h:63


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:09:22