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::Tensor
The tensor class.
Definition: Tensor.h:63
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
array
int array[24]
Definition: Map_general_stride.cpp:1
Eigen::operator<<
std::ostream & operator<<(std::ostream &os, const DSizes< IndexType, NumDims > &dims)
Definition: TensorDimensions.h:387
Eigen::Tensor::coeff
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar & coeff(const array< Index, NumIndices > &indices) const
Definition: Tensor.h:124
Eigen::internal::TensorPrinter< Tensor, 1 >::run
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:37
Eigen::internal::TensorPrinter
Definition: TensorIO.h:19
os
ofstream os("timeSchurFactors.csv")
Eigen::TensorForcedEvalOp
Definition: TensorForcedEval.h:59
Eigen::internal::TensorPrinter::run
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:20
Eigen::DefaultDevice
Definition: TensorDeviceDefault.h:17
Eigen::Tensor::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: Tensor.h:102
Eigen::Triplet< double >
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:973
Eigen::internal::array_prod
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
Definition: TensorDimensions.h:140
Eigen::internal::array_size
Definition: Meta.h:445
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
Eigen::Tensor::Layout
@ Layout
Definition: Tensor.h:77
Eigen::internal::TensorPrinter< Tensor, 0 >::run
static void run(std::ostream &os, const Tensor &tensor)
Definition: TensorIO.h:52
Eigen::Tensor::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
internal
Definition: BandTriangularSolver.h:13
NULL
#define NULL
Definition: ccolamd.c:609
Eigen::Tensor::Index
internal::traits< Self >::Index Index
Definition: Tensor.h:70
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Wed May 15 2024 15:23:38