TensorInitializer.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_INITIALIZER_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
12 
13 #if EIGEN_HAS_VARIADIC_TEMPLATES
14 
15 #include <initializer_list>
16 
17 namespace Eigen {
18 
24 namespace internal {
25 
26 template <typename Derived, int N>
27 struct Initializer {
28  typedef std::initializer_list<
29  typename Initializer<Derived, N - 1>::InitList> InitList;
30 
31  static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
32  Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
33  const InitList& vals) {
34  int i = 0;
35  for (const auto& v : vals) {
36  (*indices)[traits<Derived>::NumDimensions - N] = i++;
38  }
39  }
40 };
41 
42 template <typename Derived>
43 struct Initializer<Derived, 1> {
45 
46  static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
47  Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
48  const InitList& vals) {
49  int i = 0;
50  // There is likely a faster way to do that than iterating.
51  for (const auto& v : vals) {
52  (*indices)[traits<Derived>::NumDimensions - 1] = i++;
53  tensor.coeffRef(*indices) = v;
54  }
55  }
56 };
57 
58 template <typename Derived>
59 struct Initializer<Derived, 0> {
60  typedef typename traits<Derived>::Scalar InitList;
61 
62  static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
63  Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>*,
64  const InitList& v) {
65  tensor.coeffRef(0) = v;
66  }
67 };
68 
69 
70 template <typename Derived, int N>
71 void initialize_tensor(TensorEvaluator<Derived, DefaultDevice>& tensor,
72  const typename Initializer<Derived, traits<Derived>::NumDimensions>::InitList& vals) {
73  Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions> indices;
74  Initializer<Derived, traits<Derived>::NumDimensions>::run(tensor, &indices, vals);
75 }
76 
77 } // namespace internal
78 } // namespace Eigen
79 
80 #endif // EIGEN_HAS_VARIADIC_TEMPLATES
81 
82 #endif // EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::array
Definition: EmulateArray.h:21
test_eigen_tensor.indices
indices
Definition: test_eigen_tensor.py:33
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
N
#define N
Definition: igam.h:9
internal
Definition: BandTriangularSolver.h:13
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
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 Sun Dec 22 2024 04:15:05