Reputation: 87
I am having some trouble passing a function inside another function with Eigen tensor. For example, calling the Function1 in the main.cpp code and it works fine, but calling inside of Function2 is causing an error.
void Function1(Eigen::Tensor<std::complex<double>, 3>& vark, Eigen::Tensor<double, 3> Kin, Eigen::Tensor<std::complex<double>, 3>& Out){
Out = vark * Kin * 1i;
}
void Function2(Eigen::Tensor<std::complex<double>, 3>& In, Eigen::Tensor<std::complex<double>, 3>& kin){
Eigen::Tensor<std::complex<double>, 3> out(nx,ny,nz);
out.setZero();
Function1(In, kin, Out); //error in Kin as it's passed from the function
}
The error I get:
error: cannot convert ‘Eigen::TensorEvaluator<Eigen::Tensor<double, 3>, Eigen::DefaultDevice>::EvaluatorPointerType’ {aka ‘double*’} to ‘Eigen::TensorEvaluator<const Eigen::Tensor<std::complex<double>, 3>, Eigen::DefaultDevice>::EvaluatorPointerType’ {aka ‘const std::complex<double>*’}
152 | return m_rightImpl.evalSubExprsIfNeeded(m_leftImpl.data());
| ^
| |
| Eigen::TensorEvaluator<Eigen::Tensor<double, 3>, Eigen::DefaultDevice>::EvaluatorPointerType {aka double*}
I am confused why is this happening.
EDIT: I added the header file for the two functions:
void Function1(Eigen::Tensor<std::complex<double>, 3>& vark, Eigen::Tensor<double, 3> Kin, Eigen::Tensor<std::complex<double>, 3>& Out);
void Function2(Eigen::Tensor<std::complex<double>, 3>& In, Eigen::Tensor<std::complex<double>, 3>& kin);
Upvotes: 2
Views: 273
Reputation: 15
I was expecting a much worse template hell. This is a bit unrelated, but if you want to pass certain types of geometric operations as writable references (eg. passing reshaped Tensor to a function followed by chip, slice, etc... inside the function body) you have to use templated const TensorBase& as a function parameter and cast it to non-const in the function body:
template <class derived>
void func(const TensorBase<derived>& ten) {
auto& res = const_cast<Eigen::TensorBase<derived>&>(ten);
res.chip(0,0) = some_tensor_chip;
}
Hope this helps anyone struggling with this library as much as I did. The documentation is very brief to say at least...
Upvotes: 0
Reputation: 6123
The Kin
argument of Function1()
is a tensor of double
, not of complex<double>
. Yet, in Function2()
you pass in a complex<double>
. So the compiler complains that it cannot convert a tensor of complex<double>
to one of double
. Fix: Change the Kin
argument to Eigen::Tensor<std::complex<double>, 3>
, and also add const&
for performance reasons:
void Function1(
Eigen::Tensor<std::complex<double>, 3> const & vark,
Eigen::Tensor<std::complex<double>, 3> const & Kin,
Eigen::Tensor<std::complex<double>, 3> & Out)
{
Out = vark * Kin * 1i;
}
Note: You might also find the documentation about passing Eigen types to functions interesting.
Upvotes: 1