TensorIO.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_IO_H
00011 #define EIGEN_CXX11_TENSOR_TENSOR_IO_H
00012 
00013 namespace Eigen {
00014 
00015 namespace internal {
00016 
00017 // Print the tensor as a 2d matrix
00018 template <typename Tensor, int Rank>
00019 struct TensorPrinter {
00020   static void run (std::ostream& os, const Tensor& tensor) {
00021     typedef typename internal::remove_const<typename Tensor::Scalar>::type Scalar;
00022     typedef typename Tensor::Index Index;
00023     const Index total_size = internal::array_prod(tensor.dimensions());
00024     if (total_size > 0) {
00025       const Index first_dim = Eigen::internal::array_get<0>(tensor.dimensions());
00026       static const int layout = Tensor::Layout;
00027       Map<const Array<Scalar, Dynamic, Dynamic, layout> > matrix(const_cast<Scalar*>(tensor.data()), first_dim, total_size/first_dim);
00028       os << matrix;
00029     }
00030   }
00031 };
00032 
00033 
00034 // Print the tensor as a vector
00035 template <typename Tensor>
00036 struct TensorPrinter<Tensor, 1> {
00037   static void run (std::ostream& os, const Tensor& tensor) {
00038     typedef typename internal::remove_const<typename Tensor::Scalar>::type Scalar;
00039     typedef typename Tensor::Index Index;
00040     const Index total_size = internal::array_prod(tensor.dimensions());
00041     if (total_size > 0) {
00042       Map<const Array<Scalar, Dynamic, 1> > array(const_cast<Scalar*>(tensor.data()), total_size);
00043       os << array;
00044     }
00045   }
00046 };
00047 
00048 
00049 // Print the tensor as a scalar
00050 template <typename Tensor>
00051 struct TensorPrinter<Tensor, 0> {
00052   static void run (std::ostream& os, const Tensor& tensor) {
00053     os << tensor.coeff(0);
00054   }
00055 };
00056 }
00057 
00058 template <typename T>
00059 std::ostream& operator << (std::ostream& os, const TensorBase<T, ReadOnlyAccessors>& expr) {
00060   typedef TensorEvaluator<const TensorForcedEvalOp<const T>, DefaultDevice> Evaluator;
00061   typedef typename Evaluator::Dimensions Dimensions;
00062 
00063   // Evaluate the expression if needed
00064   TensorForcedEvalOp<const T> eval = expr.eval();
00065   Evaluator tensor(eval, DefaultDevice());
00066   tensor.evalSubExprsIfNeeded(NULL);
00067 
00068   // Print the result
00069   static const int rank = internal::array_size<Dimensions>::value;
00070   internal::TensorPrinter<Evaluator, rank>::run(os, tensor);
00071 
00072   // Cleanup.
00073   tensor.cleanup();
00074   return os;
00075 }
00076 
00077 } // end namespace Eigen
00078 
00079 #endif // EIGEN_CXX11_TENSOR_TENSOR_IO_H
 All Classes Functions Variables Typedefs Enumerator