Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorInitializer.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_INITIALIZER_H
12#define EIGEN_TENSOR_TENSOR_INITIALIZER_H
13
14#include <initializer_list>
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
28template <typename Derived, int N>
30 typedef std::initializer_list<typename Initializer<Derived, N - 1>::InitList> InitList;
31
32 static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
33 Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
34 const InitList& vals) {
35 int i = 0;
36 for (const auto& v : vals) {
37 (*indices)[traits<Derived>::NumDimensions - N] = i++;
38 Initializer<Derived, N - 1>::run(tensor, indices, v);
39 }
40 }
41};
42
43template <typename Derived>
44struct Initializer<Derived, 1> {
45 typedef std::initializer_list<typename traits<Derived>::Scalar> InitList;
46
47 static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
48 Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
49 const InitList& vals) {
50 int i = 0;
51 // TODO: Consider a faster approach than iterating.
52 for (const auto& v : vals) {
53 (*indices)[traits<Derived>::NumDimensions - 1] = i++;
54 tensor.coeffRef(*indices) = v;
55 }
56 }
57};
58
59template <typename Derived>
60struct Initializer<Derived, 0> {
61 typedef typename traits<Derived>::Scalar InitList;
62
63 static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
64 Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>*, const InitList& v) {
65 tensor.coeffRef(0) = v;
66 }
67};
68
69template <typename Derived, int N>
70void initialize_tensor(TensorEvaluator<Derived, DefaultDevice>& tensor,
71 const typename Initializer<Derived, traits<Derived>::NumDimensions>::InitList& vals) {
72 Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions> indices;
73 Initializer<Derived, traits<Derived>::NumDimensions>::run(tensor, &indices, vals);
74}
75
76} // namespace internal
77} // namespace Eigen
78
79#endif // EIGEN_TENSOR_TENSOR_INITIALIZER_H
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47
Helper template to initialize Tensors from std::initializer_lists.
Definition TensorInitializer.h:29