-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMEngine.h
More file actions
157 lines (132 loc) · 3.94 KB
/
DMEngine.h
File metadata and controls
157 lines (132 loc) · 3.94 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef DMENGINE_H
#define DMENGINE_H
#include "engine.h"
#include "mex.h"
#include <string>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
class DMEngine {
Engine *ep;
public:
DMEngine() {
int statusmat = 0;
static bool firstOpen = false;
if (firstOpen)
ep = engOpen("");
else
ep = engOpenSingleUse(NULL, NULL,&statusmat);
firstOpen = false;
if (!ep) {
std::cerr << "\nCan't start MATLAB engine\n" << std::endl;
}
}
~DMEngine() {
engClose(ep);
}
int Eval(std::string s) {
return engEvalString(ep, s.c_str());
}
int PutVariable(std::string var_name, const mxArray *ap) {
return engPutVariable(ep, var_name.c_str(), ap);
}
mxArray* GetVariable(std::string var_name) {
return engGetVariable(ep, var_name.c_str());
}
void Pause() {system("pause");}
};
class DMMatrix {
public:
mxArray *p;
DMEngine *eng;
std::string name;
int n,m;
DMMatrix(DMEngine& eng, std::string name, int n, int m, double *data = NULL) {
this->n = n, this->m = m;
this->eng = ŋ
this->name = name;
if (!data) {
Update();
return;
}
p = mxCreateDoubleMatrix(n,m,mxREAL);
memcpy((void *)(mxGetPr(p)), (void *)data, sizeof(double)*n*m);
eng.PutVariable(name, p);
p = eng.GetVariable(name);
}
double& GetNum(int i, int j) {return mxGetPr(p)[i+j*n];}
~DMMatrix() {
mxDestroyArray(p);
}
friend std::ostream& operator<<(std::ostream& out, DMMatrix &mat) {
out << mat.name << " : \n";
for (int i=0; i<mat.n; i++) {
for (int j=0; j<mat.m; j++)
out << mat.GetNum(i,j) << '\t';
out << '\n';
}
out << std::endl;
return out;
}
void Update() {p = eng->GetVariable(name);}
};
typedef std::vector< std::pair< std::pair<int, int>, double> > DMSpMatrixData;
class DMSpMatrix {
public:
mxArray *p;
DMEngine *eng;
std::string name;
int n,m,l;
static void maintain(DMSpMatrixData &data) {
std::sort(data.begin(), data.end(),
[](const std::pair< std::pair<int, int>, double>& a, const std::pair< std::pair<int, int>, double>& b) -> bool {
return
a.first.second < b.first.second ||
a.first.second == b.first.second && a.first.first < b.first.first;
});
int j = 0;
for (int i = 1; i < data.size(); i++)
if (data[i].first != data[j].first) {
j++;
if (i != j) data[j] = data[i];
}
else
data[j].second += data[i].second;
data.erase(data.begin() + j, data.end());
}
DMSpMatrix(DMEngine& eng, std::string name, int n, int m, DMSpMatrixData data, bool NeedSort = false) {
this->n = n, this->m = m;
this->eng = ŋ
this->name = name;
this->l = (int)data.size();
p = mxCreateSparse(n,m,data.size(),mxREAL);
if (NeedSort)
maintain(data);
for (int i=0; i<(int)data.size(); i++) {
mxGetIr(p)[i] = data[i].first.first;
mxGetJc(p)[data[i].first.second+1] ++;
mxGetPr(p)[i] = data[i].second;
}
for (int i=1; i<=m; i++) mxGetJc(p)[i] += mxGetJc(p)[i-1];
eng.PutVariable(name, p);
p = eng.GetVariable(name);
}
std::pair< std::pair<int,int>, double> GetNum(int i) {
return std::make_pair( std::make_pair(mxGetIr(p)[i], mxGetJc(p)[i]), mxGetPr(p)[i]);
}
friend std::ostream& operator<<(std::ostream& out, DMSpMatrix &mat) {
out << mat.name << " : \n";
for (int i=0; i<mat.l; i++) {
auto val = mat.GetNum(i);
out << "{(" << val.first.first << ", " << val.first.second << ") = " << val.second << "}\n";
}
out << std::endl;
return out;
}
~DMSpMatrix() {
mxDestroyArray(p);
}
void Update() {p = eng->GetVariable(name);}
};
#endif // DMENGINE_H