![](/img/trans.png)
[英]Strategies for filling an Eigen3 sparse matrix when the order of insertion is known
[英]Eigen3: Writing out sparse matrix
我使用以下代碼在 Eigen3 中創建了一個稀疏矩陣:
#include <eigen3/Eigen/Sparse>
#include <eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h>
#include <vector>
using SpMatrix = Eigen::SparseMatrix<double>;
using Eigen::SparseMatrix;
using S = Eigen::Triplet<double>;
using namespace std;
using AdjacenyMatrix = SpMatrix;
int main() {
vector <S> nonzero_compenents;
AdjacenyMatrix am(10, 10);
nonzero_compenents.push_back(move(S(1, 1, 1.0)));
am.setFromTriplets(nonzero_compenents.begin(), nonzero_compenents.end());
bool s = saveMarket(am, "/home/morris/Schreibtisch/sparse_matrices");
return 0;
}
不幸的是,代碼無法編譯。 MarketIO.h
似乎有問題:
In file included from /home/morris/sparse/main.cpp:2:0:
/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h: In function ‘void Eigen::internal::PutMatrixElt(Scalar, int, int, std::ofstream&)’:
/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h:87:9: error: no match for ‘operator<<’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘int’)
out << row << " "<< col << " " << value << "\n";
^
......有什么建議嗎?
正如@ggael 所說:不要包含單個文件,而是包含它們的模塊/目錄。 請注意,在這種情況下,您還需要從路徑中刪除src
。
以前:
#include <eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h>
現在:
#include <eigen3/unsupported/Eigen/SparseExtra>
完整代碼:
#include <eigen3/Eigen/Sparse>
#include <eigen3/unsupported/Eigen/SparseExtra>
#include <vector>
using SpMatrix = Eigen::SparseMatrix<double>;
using Eigen::SparseMatrix;
using S = Eigen::Triplet<double>;
using namespace std;
using AdjacenyMatrix = SpMatrix;
int main() {
vector <S> nonzero_compenents;
AdjacenyMatrix am(10, 10);
nonzero_compenents.push_back(move(S(1, 1, 1.0)));
am.setFromTriplets(nonzero_compenents.begin(), nonzero_compenents.end());
bool s = saveMarket(am, "/home/morris/Schreibtisch/sparse_matrices");
return 0;
}
現在它應該編譯使用例如
g++ -std=c++11 mycode.cc
和
#include <fstream>
#include <Eigen/Sparse>
#include <unsupported/Eigen/SparseExtra>
代替
#include <eigen3/Eigen/Sparse>
#include <eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h>
代碼編譯
注意: #include <eigen3/unsupported/Eigen/SparseExtra>
不編譯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.