-
Notifications
You must be signed in to change notification settings - Fork 52
/
MyMatrix.cpp
110 lines (93 loc) · 2.1 KB
/
MyMatrix.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* MyMatrix.cpp
*
* Created on: May 21, 2018
* Author: david
*/
#include "MyMatrix.h"
MyMatrix::MyMatrix() {
// TODO Auto-generated constructor stub
}
MyMatrix::~MyMatrix() {
// TODO Auto-generated destructor stub
}
void MyMatrix::printMatrix(const MatrixXd &mat)
{
for(int i = 0;i < mat.rows(); i++)
{
for(int j = 0;j < mat.cols();j++)
{
cout << mat(i,j) << ", ";
}
cout << endl;
}
}
void MyMatrix::keepMatPricision(MatrixXd &Qmat, int keepNum)
{
if(keepNum <= 0) return ;
for(int i = 0;i < Qmat.rows();i++)
for(int j = 0;j < Qmat.cols();j++)
{
double temp_num = Qmat(i,j);
QString temp_str = QString::number(temp_num, 'f', keepNum);
Qmat(i,j) = temp_str.toDouble();
}
}
void MyMatrix::keepMatPricision(VectorXd &Vct_v, int keepNum)
{
if(keepNum <= 0) return ;
for(int i = 0;i < Vct_v.rows();i++)
{
double temp_num = Vct_v(i);
QString temp_str = QString::number(temp_num, 'f', keepNum);
Vct_v[i] = temp_str.toDouble();
}
}
MatrixXd MyMatrix::readCSV(const char *filename) {
ifstream in;
string line;
in.open(filename, std::ios::in);
int row = 0;
int col = 0;
MatrixXd res(1,1);
RowVectorXd rowVector(1);
if (in.is_open()) {
while (std::getline(in, line)) {
char *ptr = (char *)line.c_str();
int len = line.length();
col = 0;
char *start = ptr;
for (int i = 0; i < len; i++) {
if (ptr[i] == ',') {
//res(row, col++) = atof(start);
rowVector(col++) = atof(start);
start = ptr + i + 1;
rowVector.conservativeResize(col + 1);
}
}
//res(row++, col) = atof(start);
rowVector(col) = atof(start);
res.conservativeResize(row + 1, col + 1);
res.row(row++) = rowVector;
}
in.close();
}
return res;
}
bool MyMatrix::writeCSV(const char *filename, const MatrixXd &mat)
{
ofstream out;
out.open(filename, std::ios::out);
for(int i = 0; i < mat.rows(); i++)
{
for(int j = 0; j < mat.cols();j++)
{
out << setprecision(10) << mat(i,j);
if(j != mat.cols() - 1)
out << ",";
}
out << endl;
}
out.close();
return true;
}