简体   繁体   English

c++中如何用eigen库导入矩阵行情文件

[英]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.我是 C++ 的新手,以前使用过 MATLAB。不幸的是,我的矩阵大小对于 MATLAB 来说太大了,所以我想在 C++ 中尝试一下。我找到了特征库 3.3.7 来进行矩阵操作。 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.为此,我需要将我的矩阵市场文件导入 Visual Studio 2019。我了解 C++ 中的一些基础知识,并尝试使用 loadMarket 导入我的文件。 After trying to compile it I get like 30 errors in the MarketIO.h file.尝试编译后,我在 MarketIO.h 文件中遇到了 30 个错误。

This is the file I'm using.这是我正在使用的文件。 https://eigen.tuxfamily.org/dox/unsupported/MarketIO_8h_source.html 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/... ).您绝不能直接包含来自unsupported/Eigen/src/... (或来自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.在使用问题中的SparseExtra模块时,我可能遇到了相同的问题或密切相关的问题(不幸的是,问题没有详细的错误消息)。

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使用g++ -std=c++17编译,g++ 版本 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 .我不知道这是否是一个 Eigen 错误,但正如上面错误中的第一行所示,编译器似乎无法弄清楚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:通过修改MarketIO.h源代码(在我的机器上位于usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h )解决了这个问题,如下所示:

// 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 .由于 OP 提到大矩阵,另一种选择是fast_matrix_marketEigen bindings The Eigen's MarketIO.h loader is sequential, fast_matrix_market is parallel. Eigen 的MarketIO.h加载器是顺序的,fast_matrix_market 是并行的。

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

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

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