简体   繁体   中英

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);

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 eg householderQr because that is what Matlab does for general square matrices . 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. With dynamically sized vectors, this would introduce extra memory allocations but here it is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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