简体   繁体   English

如何将 Eigen::matrix 除以 Eigen::vector?

[英]How can I divide the Eigen::matrix by Eigen::vector?

following is my code.以下是我的代码。

    Eigen::Matrix3d first_rotation = firstPoint.q.matrix();
    Eigen::Vector3d first_trans= firstPoint.t;
    for(auto &iter:in_points )
    {
                
                iter.second.t= first_rotation / (iter.second.t-first_trans).array();
    }

However,the vscode says"no operator / matches the operands" for division." How can I division a matrix by a vector?然而,vscode 说“没有运算符/匹配操作数”用于除法。我怎样才能用向量除以矩阵?

In Matlab, the line was t2 = R1 \ (R2 - t1);在 Matlab 中,直线为t2 = R1 \ (R2 - t1);

Matlab defined the / and \ operators, when applied to matrices, as solving linear equation systems, as you can read up on in their operator documentation . Matlab 定义了/\运算符,当应用于矩阵时,作为求解线性方程系统,您可以在他们的运算符文档中阅读。 In particular尤其

x = A\B solves the system of linear equations A*x = B x = A\B求解线性方程组A*x = B

Eigen doesn't do this.本征不这样做。 And I don't think most other languages or libraries do it either.而且我认为大多数其他语言或库也不会这样做。 The main point is that there are multiple ways to decompose a matrix for solving.要点是有多种方法可以分解矩阵进行求解。 Each with their own pros and cons.每个都有自己的优点和缺点。 You can read up on it in their documentation .您可以在他们的文档中阅读它。

In your case, you can save time by reusing the decomposition multiple times.在您的情况下,您可以通过多次重复使用分解来节省时间。 Something like this:是这样的:

Eigen::PartialPivLU<Eigen::Matrix3d> first_rotation =
      firstPoint.q.matrix().partialPivLu();
for(auto &iter: in_points)
    iter.second.t = first_rotation.solve(
          (iter.second.t-first_trans).eval());

Note: I picked LU over eg householderQr because that is what Matlab does for general square matrices .注意:我选择了 LU 而不是 householderQr,因为这就是 Matlab 对一般方矩阵所做的 If you know more about your input matrix, eg that it is symmetrical, or not invertible, try something else.如果您对输入矩阵了解更多,例如它是对称的或不可逆的,请尝试其他方法。

Note 2: I'm not sure it is necessary in this case but I added a call to eval so that the left side of the assignment is not an alias of anything on the right side.注 2:我不确定在这种情况下是否有必要,但我添加了对eval的调用,以便赋值的左侧不是右侧任何内容的别名。 With dynamically sized vectors, this would introduce extra memory allocations but here it is fine.对于动态大小的向量,这将引入额外的 memory 分配,但在这里没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM