简体   繁体   中英

How to import matrix market files with eigen library in c++

I'm new to C++ and used to MATLAB. Unfortunatly my matrix size got too big for MATLAB, so I want to try it in C++. I've found the eigen library 3.3.7 to do matrix manipulations. For this I need to import my matrix market files into Visual Studio 2019. I know some basics in C++ and tried to import my files with loadMarket. After trying to compile it I get like 30 errors in the MarketIO.h file.

This is the file I'm using. https://eigen.tuxfamily.org/dox/unsupported/MarketIO_8h_source.html

#include <Eigen/Sparse>
#include <unsupported/Eigen/src/SparseExtra/MarketIO.h>

int main(){
    typedef Eigen::SparseMatrix<float, Eigen::RowMajor>SMatrixXf;
    SMatrixXf A;
    Eigen::loadMarket(A, "B.mtx");
}

You must never directly include files from unsupported/Eigen/src/... (or from Eigen/src/... ). Just include the corresponding parent header instead:

#include <unsupported/Eigen/SparseExtra>

I probably met the same problem or a closely related one (unfortunately the question has not a detailed error message) while using the SparseExtra module as in the question.

By compiling the code provided in the question I get many errors among which:

/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h:115:20: 
error: variable ‘std::ifstream in’ has initializer but incomplete type
  115 |   std::ifstream in(filename.c_str(),std::ios::in);
      |                    ^~~~~~~~

.... many warnings and errors releated to std::ifstream and std::ofstream ...

/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h:272:9: 
error: no match for ‘operator<<’ (operand types are ‘std::ofstream’ {aka 
‘std::basic_ofstream<char>’} and ‘const char [42]’)

compiled using g++ -std=c++17 , g++ version 12.1.0, Eigen3 v 3.3

I don't know if this is an Eigen bug, but as the first line in the error above shows it seems that the compiler is not able to figure out the definition of std::ifstream .

The problem is solved by modifying the MarketIO.h source code (on my machine located at usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h ) as follow:

// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
// Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_SPARSE_MARKET_IO_H
#define EIGEN_SPARSE_MARKET_IO_H

#include <iostream>
#include <vector>
#include <fstream>  // IMPORT THIS HEADER FILE!

... // no changes in the rest of the file

This removes any error and makes the code to compile and properly load the matrix.

Since OP mentions large matrices, another option is fast_matrix_market 's Eigen bindings . The Eigen's MarketIO.h loader is sequential, fast_matrix_market is parallel.

#include <fstream>
#include <Eigen/Sparse>
#include <fast_matrix_market/app/Eigen.hpp>

int main(){
    typedef Eigen::SparseMatrix<float, Eigen::RowMajor>SMatrixXf;
    SMatrixXf A;

    std::ifstream file("B.mtx");
    fast_matrix_market::read_matrix_market_eigen(file, A);
}

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