在 EIGEN 中,c++ 不能将向量乘以它的转置

In EIGEN c++ cannot multiply vector by it's transpose

本文关键字:转置 向量 EIGEN c++ 不能      更新时间:2023-10-16

在执行下面的代码时,我得到了这个错误:" invalid_vector_vector_product_if_you_wanted_a_dot_or_coeff_wise_product_you_must_use_the_explicit_functions "

#include <iostream>
#include <Eigen/Dense> 
using namespace Eigen;
int main()
{
    Vector3d v(1, 2, 3);
    Vector3d vT = v.transpose();
    Matrix3d ans = v*vT;
    std::cout << ans << std::endl;
}
是否有其他方法可以做到这一点,而不会引起编译器的抱怨?

Vector3d定义为列向量,因此vvT都是列向量。因此,操作v*vT没有意义。你要做的是

Matrix3d ans = v*v.transpose();

或将vT定义为RowVector3d

Vector3d v(1, 2, 3);
RowVector3d vT = v.transpose();
Matrix3d ans = v*vT;