简体   繁体   English

特征模板 function 接受矩阵 col 和向量

[英]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?我不明白如何编写同时接受向量和矩阵列的模板化 function?

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);只有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.他们都没有展示如何使用模板化 function 解决所描述的任务。

Edit: Also I would like to pass dynamic size vector and, but not necessary, fixed size编辑:我也想传递动态大小向量,但不是必须的,固定大小

I could get this to work using MatrixBase :我可以使用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);
}

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

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