Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:14:21

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
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 // Print the tensor as a 2d matrix
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 // Print the tensor as a vector
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 // Print the tensor as a scalar
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   // Evaluate the expression if needed
0064   TensorForcedEvalOp<const T> eval = expr.eval();
0065   Evaluator tensor(eval, DefaultDevice());
0066   tensor.evalSubExprsIfNeeded(NULL);
0067 
0068   // Print the result
0069   static const int rank = internal::array_size<Dimensions>::value;
0070   internal::TensorPrinter<Evaluator, rank>::run(os, tensor);
0071 
0072   // Cleanup.
0073   tensor.cleanup();
0074   return os;
0075 }
0076 
0077 } // end namespace Eigen
0078 
0079 #endif // EIGEN_CXX11_TENSOR_TENSOR_IO_H