[英]How to import matrix market files with eigen library in c++
我是 C++ 的新手,以前使用過 MATLAB。不幸的是,我的矩陣大小對於 MATLAB 來說太大了,所以我想在 C++ 中嘗試一下。我找到了特征庫 3.3.7 來進行矩陣操作。 為此,我需要將我的矩陣市場文件導入 Visual Studio 2019。我了解 C++ 中的一些基礎知識,並嘗試使用 loadMarket 導入我的文件。 嘗試編譯后,我在 MarketIO.h 文件中遇到了 30 個錯誤。
這是我正在使用的文件。 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");
}
您絕不能直接包含來自unsupported/Eigen/src/...
(或來自Eigen/src/...
)的文件。 只需包含相應的父標頭即可:
#include <unsupported/Eigen/SparseExtra>
在使用問題中的SparseExtra
模塊時,我可能遇到了相同的問題或密切相關的問題(不幸的是,問題沒有詳細的錯誤消息)。
通過編譯問題中提供的代碼,我得到了許多錯誤,其中包括:
/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]’)
使用g++ -std=c++17
編譯,g++ 版本 12.1.0,Eigen3 v 3.3
我不知道這是否是一個 Eigen 錯誤,但正如上面錯誤中的第一行所示,編譯器似乎無法弄清楚std::ifstream
的定義。
通過修改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
這消除了任何錯誤並使代碼能夠編譯並正確加載矩陣。
由於 OP 提到大矩陣,另一種選擇是fast_matrix_market的Eigen bindings 。 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.