简体   繁体   English

表达式必须具有常数值本征矩阵

[英]Expression must have constant value Eigen matrix

I have been trying to give the matrix size while runtime via a function, for example 我一直在尝试通过函数来​​给出运行时的矩阵大小,例如

Eigen::MatrixXd FileReader::load_csv(const std::string & path, const int rows_csv, const int columns_csv) {
    std::ifstream indata;
    indata.open(path);
    std::string line;
    std::vector<double> values;
    int rows = 0;
    while (getline(indata, line)) {
        std::stringstream lineStream(line);
        std::string cell;
        while (std::getline(lineStream, cell, ',')) {
            values.push_back(std::stod(cell));
        }
        ++rows;
    }
    std::cout << "loading";
    return Eigen::Map<const Eigen::Matrix<double, columns_csv, rows_csv, Eigen::RowMajor>>(values.data(), rows, values.size() / rows);
}

(Above code is from https://stackoverflow.com/a/39146048/3782963 ), I am not able to send a constant value of the matrix size to the function, I get Expression must have constant value error. (以上代码来自https://stackoverflow.com/a/39146048/3782963 ),我无法将矩阵大小的常量发送给函数,我得到Expression must have constant value错误。 Is there any way I could do something like this: 有什么办法可以做这样的事情:

Eigen::MatrixXd mat = load_csv("some_path", 20, 30);

Any idea? 任何想法?

This line is your problem: 这行是你的问题:

Eigen::Map<const Eigen::Matrix<double, columns_csv, rows_csv, Eigen::RowMajor>>
                                          (values.data(), rows, values.size() / rows);

The

Eigen::Map<const Eigen::Matrix<double, columns_csv, rows_csv, Eigen::RowMajor>>
                                           ^^^         ^^^

tells the Map that the that it should expect a constant sized matrix, and the 告诉Map ,它应该期望尺寸不变的矩阵,并且

(values.data(), rows, values.size() / rows);
                ^^^              ^^^

is a dynamic sized matrix. 是一个动态大小的矩阵。 If, for some reason, you want to ignore the shape of the matrix in the file and define it in your program, try: 如果由于某种原因,您想要忽略文件中矩阵的形状并在程序中定义它,请尝试:

Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
                                                 (values.data(), rows_csv, columns_csv);

It is also helpful to read the documentation on Matrix size allocation which states: 阅读有关矩阵大小分配的文档也有帮助,该文档指出:

Fixed-size versus dynamic-size: 固定尺寸与动态尺寸:

Fixed-size means that the numbers of rows and columns are known are compile-time. 固定大小意味着已知的行数和列数是编译时的。 In this case, Eigen allocates the array of coefficients as a fixed-size array, as a class member. 在这种情况下,Eigen将系数数组分配为固定大小的数组,作为类成员。 This makes sense for very small matrices, typically up to 4x4, sometimes up to 16x16. 对于非常小的矩阵(通常最大为4x4,有时最大为16x16),这是有意义的。 Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time. 即使较大的矩阵在编译时就知道其大小,也应将其声明为动态大小。 Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. 动态大小意味着在编译时不一定知道行数或列数。 In this case they are runtime variables, and the array of coefficients is allocated dynamically on the heap. 在这种情况下,它们是运行时变量,并且系数数组在堆上动态分配。 Note that dense matrices, be they Fixed-size or Dynamic-size, do not expand dynamically in the sense of a std::map. 请注意,稠密矩阵(无论是固定大小还是动态大小)在std :: map的意义上不会动态扩展。 If you want this behavior, see the Sparse module. 如果您想要这种行为,请参阅稀疏模块。

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

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