Vex994
Vex994

Reputation: 49

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");
}

Upvotes: 0

Views: 1819

Answers (4)

JCut
JCut

Reputation: 31

Be ware that Eigen::loadMarket has a naive implementation and does not support symmetry. See doc here.

If your .mtx is symmetric (format indicated in the first line starting with %%MatrixMarket), this will only load one half and won't fill the other half for you.

Consider fast_matrix_market or matrix-market instead.

Upvotes: 1

Adam
Adam

Reputation: 17329

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);
}

Upvotes: 0

AlePalu
AlePalu

Reputation: 73

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 <[email protected]>
// Copyright (C) 2012 Desire NUENTSA WAKAM <[email protected]>
//
// 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.

Upvotes: 0

chtz
chtz

Reputation: 18807

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>

Upvotes: 2

Related Questions