我想使用Eigen的张量类。 我已经安装了Eigen 3.3.3,并确保我的项目选择了正确的版本(而不是旧的ubuntu软件包)
cout<<EIGEN_WORLD_VERSION<<"."<<EIGEN_MAJOR_VERSION<<"."<<EIGEN_MINOR_VERSION<<endl;
// prints 3.3.3
此代码有效:
Tensor<double, 3> t(3,3,3);
t.setConstant(1);
但是这段代码失败了:
t*=5;
错误消息抱怨int
类型:
[ 50%] Building CXX object CMakeFiles/sandbox.dir/src/SandBox/sandbox.cpp.o
In file included from /opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/Tensor:103:0,
from /home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:3:
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h: In instantiation of ‘Derived& Eigen::TensorBase<Derived, AccessLevel>::operator*=(const OtherDerived&) [with OtherDerived = int; Derived = Eigen::Tensor<double, 3>; int AccessLevel = 1]’:
/home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:30:6: required from here
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h:876:36: error: request for member ‘derived’ in ‘other’, which is of non-class type ‘const int’
return derived() = derived() * other.derived();
我已尝试t*=5.;
,但只会将错误消息更改为which is of non-class type ‘const double’
当我将代码更改为:
时t=t*5;
错误消息变得很长:https://pastebin.com/T6kvLaZ9
最终版本:
t=t*5.;
令人惊讶的是,这很有效。我不明白为什么t*=5.;
会产生错误。
答案 0 :(得分:2)
如上所述,本征张量当前不支持使用*
运算符进行标量乘法。然而,逐元素张量乘法是。要对张量进行标量乘法,请使用命令t.constant(Scalar)
创建尺寸与t
相同但所有元素等于Scalar
值的张量,只要Scalar
是类型float
。然后使用*
运算符乘以该张量:
Tensor<double, 3> t(5,5);
t.setConstant(1);
Tensor<double, 3> ans(5,5) = t*t.constant(5.);
std::cout << t << std::endl;
std::endl;
std::cout << ans << std::endl;
产生
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
答案 1 :(得分:0)
据我所知,当前的Eigen Tensor实现不支持标量乘法。 但您可以尝试将Tensor转换为Matrix。