summaryrefslogtreecommitdiff
path: root/tools/matrixIO.h
blob: ea5f85f27d1b2a21fdb6e37d7a00458bd59a1dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Some helper to read&write matrices data

#ifndef MATIO_H
#define MATIO_H

#include<iostream>

namespace Eigen{
    template<class Matrix>
    void write_stream(std::ostream &ostr, const Matrix& matrix){
        typename Matrix::Index rows=matrix.rows(), cols=matrix.cols();
        ostr<<rows<<" "<<cols<<std::endl;
        for (int r=0;r<rows;++r)
        {
            for (int c=0;c<cols;++c)
                ostr<<matrix(r,c)<<" ";
            ostr<<std::endl;
        }
    }
    template<class Matrix>
    void read_stream(std::istream &istr, Matrix& matrix){
        typename Matrix::Index rows=0, cols=0;
        istr>>rows>>cols;
        matrix.resize(rows, cols);
        for (int r=0;r<rows;++r)
            for (int c=0;c<cols;++c)
                istr>>matrix(r,c);
    }
} // Eigen::


#endif