-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathMatrix.cpp
257 lines (228 loc) · 4.84 KB
/
Matrix.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "Matrix.h"
#include "debugModule.h"
#include "inv.h"
#include <vector>
#include <assert.h>
#include <iostream>
using namespace std;
void MAT::resizeMat(int row,int col)
{
mMat.resize(row);
for(int curRow = 0;curRow < row;curRow++)
{
mMat[curRow].resize(col);
}
}
double MAT::at(int row,int col)
{
return mMat[row][col];
}
MAT MAT::MyMatDivide(double value)
{
MAT outMat(getRowSize(),getColSize());
for(int row = 0;row < outMat.getRowSize();row++)
{
for(int col=0;col<outMat.getColSize();col++)
{
outMat.setValue(row,col,at(row,col)/value);
}
}
return outMat;
}
MAT MAT::MyMatTanspose()
{
MAT outMat(this->getColSize(),this->getRowSize());
for(int row = 0;row < outMat.getRowSize();row++)
{
for(int col = 0;col < outMat.getColSize();col++)
{
outMat.setValue(row,col,this->at(col,row));
}
}
return outMat;
}
MAT MAT::MyMatrixDiffConstValue (double conValue)
{
MAT outMat(getRowSize(),getColSize());
for(int row = 0;row < getRowSize(); row++)
{
for(int col = 0;col <getColSize();col++)
{
outMat.setValue(row,col,mMat[row][col] - conValue);
}
}
return outMat;
}
MAT MAT::MyInv ()
{
if(this->getColSize() != this->getRowSize())
{
MyAssert(true,"inv mat row != col !");
}
const int rowSize = this->getColSize();
double inMatArray[9] = {} ;
double outMatArray[9] = {};
for(int row = 0;row <rowSize; row++)
{
for(int col =0;col<rowSize;col++)
{
inMatArray[row*rowSize+col] = this->at(row,col);
}
}
inv(inMatArray,outMatArray,rowSize);
MAT outMatMat(rowSize,rowSize);
for(int row = 0;row <rowSize; row++)
{
for(int col =0;col<rowSize;col++)
{
outMatMat.setValue(row,col,(outMatArray[row*rowSize+col]));
}
}
return outMatMat;
}
MAT MAT::MyGetCol(int colNum)
{
MAT outMat(getRowSize(),1);
for(int row = 0;row<getRowSize();row++)
{
outMat .setValue(row,0,at(row,colNum));
}
return outMat;
}
MAT MAT::MyGetRow(int rowNum)
{
MAT outMat(1,getColSize());
for(int col = 0;col<getRowSize();col++)
{
outMat .setValue(0,col,at(rowNum,col));
}
return outMat;
}
void MAT::operator =(vector<vector<double>> value)
{
this->resizeMat(value.size(),value[0].size());
for(int row = 0;row<getRowSize();row++)
{
for(int col = 0;col<getColSize();col++)
{
this->setValue(row,col,value[row][col]);
}
}
}
double MAT::MyMean(DIM inDim,int index)
{
double meanValue = 0;
if(inDim == COL)
{
for(int row = 0;row<this->getRowSize();row++)
{
meanValue += this->at(row,index);
}
meanValue /= this->getRowSize();
}
else
{
for(int col = 0;col<this->getColSize();col++)
{
meanValue += this->at(index,col);
}
meanValue /= this->getColSize();
}
return meanValue;
}
MAT MAT::operator -(MAT &matRight)
{
if(!this->MyMatMatch_DimEqual( matRight))
{
MyAssert(1,"MAT not match!");
}
MAT outMat (this->getRowSize(),this->getColSize());
for(int row = 0;row<this->getRowSize();row++)
{
for(int col =0;col<this->getColSize();col++)
{
outMat.setValue(row,col,this->at(row,col) - matRight.at(row,col));
}
}
return outMat;
}
MAT MAT::operator -(double value)
{
MAT outMat(getRowSize(),getColSize());
for(int row = 0;row < getRowSize(); row++)
{
for(int col = 0;col <getColSize();col++)
{
outMat.setValue(row,col,mMat[row][col] - value);
}
}
return outMat;
}
MAT MAT:: operator *(MAT &matRight)
{
if((this->getColSize() != matRight.getRowSize()))
{
cout<<"matrix left col not equals matrix right row.";
assert(1);
}
MAT outMatrix(this->getRowSize(),matRight.getColSize());
for(int row = 0;row < this->getRowSize();row++)
{
for(int col = 0;col < matRight.getColSize();col++)
{
double tempValue = 0;
for(int index = 0;index < this->getColSize();index++)
{
tempValue += this->at(row,index)*matRight.at(index,col);
}
outMatrix.setValue(row,col,tempValue);
}
}
return outMatrix;
}
MAT MAT::operator *(double multiValue)
{
MAT outMat(*this);
for(int row = 0;row<getRowSize();row++)
{
for(int col = 0;col<getColSize();col++)
{
outMat.setValue(row,col,at(row,col)*multiValue);
}
}
return outMat;
}
bool MAT::MyMatMatch_DimEqual(MAT &matRight)
{
if(this->getRowSize() == matRight.getRowSize() && this->getColSize() == matRight.getColSize())
{
return true;
}
else
{
return false;
}
}
bool MAT::MyMatMatch_DimMulti(MAT &matRight)
{
if(this->getColSize() == matRight.getRowSize())
{
return true;
}
else
{
return false;
}
}
void MAT::ArrayToMat_Constructor(double *inArray,int rowSize,int colSize)
{
this->resizeMat(rowSize,colSize);
for(int row=0;row<rowSize;row++)
{
for(int col=0;col<colSize;col++)
{
this->setValue(row,col,inArray[row*colSize + col]);
}
}
return ;
}