'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?

In Matlab, the line was t2 = R1 \ (R2 - t1);



Solution 1:[1]

Matlab defined the / and \ operators, when applied to matrices, as solving linear equation systems, as you can read up on in their operator documentation. In particular

x = A\B solves the system of linear equations 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 e.g. householderQr because that is what Matlab does for general square matrices. If you know more about your input matrix, e.g. 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. With dynamically sized vectors, this would introduce extra memory allocations but here it is fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1