10 template <
typename Scalar>
12 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> ReturnType;
13 return ReturnType::Constant(1,
value);
16 template <
typename Scalar>
17 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>
matrix1x1(
18 const Scalar&
value) {
19 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> ReturnType;
20 return ReturnType::Constant(1, 1,
value);
23 template <
typename Tensor>
24 Eigen::TensorRef<Tensor>
make_ref(Tensor& tensor) {
25 return Eigen::TensorRef<Tensor>(tensor);
28 template <
typename Tensor>
29 void fill(Eigen::TensorRef<Tensor> tensor,
typename Tensor::Scalar
value) {
30 for (Eigen::DenseIndex k = 0; k < tensor.size(); ++k)
31 tensor.coeffRef(k) =
value;
34 template <
typename Tensor>
35 void print(
const Tensor& tensor) {
36 std::cout << tensor << std::endl;
39 template <
typename Tensor>
40 void print_ref(
const Eigen::TensorRef<const Tensor> tensor) {
44 template <
typename Tensor>
45 void print_base(
const Eigen::TensorBase<Tensor>& tensor) {
49 template <
typename Tensor>
50 Tensor
copy(
const Eigen::TensorBase<Tensor>& tensor) {
51 return const_cast<Tensor&
>(
static_cast<const Tensor&
>(tensor));
54 template <
typename Tensor>
55 Eigen::TensorRef<Tensor>
ref(Eigen::TensorRef<Tensor> tensor) {
59 template <
typename Tensor>
61 const Eigen::TensorRef<const Tensor> tensor) {
65 template <
typename Scalar,
int Rank>
67 return Eigen::Tensor<Scalar, Rank>();
70 template <
typename Scalar>
72 Eigen::Tensor<Scalar, 1> tensor(
r);
77 template <
typename Scalar>
79 const Eigen::DenseIndex s) {
80 Eigen::Tensor<Scalar, 2> tensor(
r, s);
85 template <
typename Scalar>
87 const Eigen::DenseIndex s,
88 const Eigen::DenseIndex
t) {
89 Eigen::Tensor<Scalar, 3> tensor(
r, s,
t);
94 template <
typename Scalar>
97 Eigen::Tensor<Scalar, 1> tensor(
r);
102 template <
typename Scalar>
104 const Eigen::DenseIndex s,
106 Eigen::Tensor<Scalar, 2> tensor(
r, s);
111 template <
typename Scalar>
113 const Eigen::DenseIndex s,
114 const Eigen::DenseIndex
t,
116 Eigen::Tensor<Scalar, 3> tensor(
r, s,
t);
121 template <
typename Scalar,
int Rank>
123 typedef Eigen::Tensor<Scalar, Rank>
Tensor;
125 typedef Eigen::Matrix<typename Tensor::Index, Rank, 1>
Dimensions;
129 typedef Eigen::array<typename Tensor::Index, Rank> InternalDimension;
130 InternalDimension _dims;
131 for (
size_t k = 0; k < Rank; ++k) _dims[k] = dims[Eigen::DenseIndex(k)];
140 template <
typename Scalar,
int Rank>
143 const std::string class_name =
"TensorContainer" + std::to_string(Rank);
144 bp::class_<T>(class_name.c_str(), bp::no_init)
145 .def(bp::init<typename T::Dimensions>())
146 .def(
"get_copy", &T::get_copy)
147 .def(
"get_ref", &T::get_ref,
148 bp::with_custodian_and_ward_postcall<0, 1>());
152 using namespace Eigen;
155 typedef Eigen::Tensor<double, 1> Tensor1;
156 typedef Eigen::Tensor<double, 2> Tensor2;
157 typedef Eigen::Tensor<double, 3> Tensor3;
159 bp::def(
"emptyTensor1", emptyTensor<double, 1>);
160 bp::def(
"emptyTensor2", emptyTensor<double, 2>);
161 bp::def(
"emptyTensor3", emptyTensor<double, 3>);
163 bp::def(
"zeroTensor1", zeroTensor1<double>);
164 bp::def(
"zeroTensor2", zeroTensor2<double>);
165 bp::def(
"zeroTensor3", zeroTensor3<double>);
167 bp::def(
"createTensor1", createTensor1<double>);
168 bp::def(
"createTensor2", createTensor2<double>);
169 bp::def(
"createTensor3", createTensor3<double>);
171 bp::def(
"print", print<Tensor1>);
172 bp::def(
"print", print<Tensor2>);
173 bp::def(
"print", print<Tensor3>);
175 bp::def(
"print_ref", print_ref<Tensor1>);
176 bp::def(
"print_ref", print_ref<Tensor2>);
177 bp::def(
"print_ref", print_ref<Tensor3>);
179 bp::def(
"print_base", print_base<Tensor1>);
180 bp::def(
"print_base", print_base<Tensor2>);
181 bp::def(
"print_base", print_base<Tensor3>);
183 bp::def(
"copy", copy<Tensor1>);
184 bp::def(
"copy", copy<Tensor2>);
185 bp::def(
"copy", copy<Tensor3>);
187 bp::def(
"fill", fill<Tensor1>);
188 bp::def(
"fill", fill<Tensor2>);
189 bp::def(
"fill", fill<Tensor3>);
191 bp::def(
"ref", ref<Tensor1>, bp::with_custodian_and_ward_postcall<0, 1>());
192 bp::def(
"ref", ref<Tensor2>, bp::with_custodian_and_ward_postcall<0, 1>());
193 bp::def(
"ref", ref<Tensor3>, bp::with_custodian_and_ward_postcall<0, 1>());
195 bp::def(
"const_ref", const_ref<Tensor1>,
196 bp::with_custodian_and_ward_postcall<0, 1>());
197 bp::def(
"const_ref", const_ref<Tensor2>,
198 bp::with_custodian_and_ward_postcall<0, 1>());
199 bp::def(
"const_ref", const_ref<Tensor3>,
200 bp::with_custodian_and_ward_postcall<0, 1>());
202 exposeTensorContainer<double, 1>();
203 exposeTensorContainer<double, 2>();
204 exposeTensorContainer<double, 3>();