简体   繁体   中英

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. 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

(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. This makes sense for very small matrices, typically up to 4x4, sometimes up to 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. If you want this behavior, see the Sparse module.

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