简体   繁体   中英

Eigen3: Writing out sparse matrix

I have created a sparse martrix in Eigen3 using the following code:

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

}

Unfortunalty, the code does not compile. There is seems to be something wrong in 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";
         ^

... Any suggestions?

As @ggael said: Don't include individual files but the module/directory containing them. Note that in this case you also need to drop the src from the path.

Previously:

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

Now:

#include <eigen3/unsupported/Eigen/SparseExtra>

Full code:

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

}

Now it should compile using for instance

g++ -std=c++11 mycode.cc

With

#include <fstream>
#include <Eigen/Sparse>
#include <unsupported/Eigen/SparseExtra>

instead of

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

the code compiles

Note : #include <eigen3/unsupported/Eigen/SparseExtra> does not compile

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