Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c++ - Eigen templated function that accepts both Matrix col and vector

I can't understand how to write templated function that accepts both vector and matrix column?

For example:

template<typename T>
void foo(
    const Eigen::MatrixX<T>& M){

}

int main(){
  Eigen::VectorX<double> v(3);
  Eigen::MatrixX<double> m(4,3);

  foo(m); // fine
  foo(m.col(0)); // broken
  foo(m.row(0)); // broken
  foo(v); // broken
}

only foo(m); is ok.

I've seen examples that do this with predefined types and I've seen examples that explore templates. Neither of them do shows how to solve described task with templated function.

Edit: Also I would like to pass dynamic size vector and, but not necessary, fixed size


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I could get this to work using MatrixBase:

#include <Eigen/Dense>

template<typename T>
void foo(const Eigen::MatrixBase<T>& M){}

int main(){
    Eigen::Vector3d v(3);
    Eigen::Matrix<double,4,3> m(4,3);
    Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> q(5,6);

    foo(m);
    foo(m.col(0));
    foo(m.row(0));
    foo(v);
    foo(q);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...