API
 
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include <Eigen/Dense>
2#include <string>
3#include <iostream>
4#include <fstream>
5#include <vector>
6
7#include "utils.hpp"
8
9namespace DDSPC
10{
11 void save_matrix(std::string fileName, Matrix mat){
12 // https://eigen.tuxfamily.org/dox/structEigen_1_1IOFormat.html
13 const static Eigen::IOFormat CSVFormat (Eigen::FullPrecision, Eigen::DontAlignCols, ", ", "\n");
14
15 std::ofstream file(fileName);
16 if (file.is_open()){
17 file << mat.format(CSVFormat);
18 file.close();
19 }
20 }
21
22 void print_matrix(Matrix mat, std::string name=""){
23 if(name.length() > 0)
24 std::cout << name << std::endl;
25
26 for(int i=0; i < mat.rows(); i++){
27 std::cout << "[";
28 for(int j=0; j < mat.cols(); j++){
29 std::cout << mat(i, j);
30 if(j < (mat.cols() - 1))
31 std::cout << ",";
32 }
33 std::cout << "]";
34 std::cout << std::endl;
35 }
36 }
37
38 Matrix load_matrix(std::string fileName){
39
40 std::vector<realT> matrixEntries;
41 std::ifstream matrixDataFile(fileName);
42 std::string matrixRowString;
43 std::string matrixEntry;
44 int matrixRowNumber = 0;
45
46 while (getline(matrixDataFile, matrixRowString)){
47
48 std::stringstream matrixRowStringStream(matrixRowString);
49
50 while (getline(matrixRowStringStream, matrixEntry, ',')){
51 matrixEntries.push_back(static_cast<realT>(stod(matrixEntry)));
52 }
53 matrixRowNumber++;
54 }
55
56 return Eigen::Map<Matrix>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber);
57
58 }
59}
Eigen::Matrix< realT, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition utils.hpp:12
float realT
Definition utils.hpp:11
void save_matrix(std::string fileName, Matrix mat)
Definition utils.cpp:11
Matrix load_matrix(std::string fileName)
Definition utils.cpp:38
void print_matrix(Matrix mat, std::string name="")
Definition utils.cpp:22