-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMat.h
More file actions
342 lines (305 loc) · 6.37 KB
/
Mat.h
File metadata and controls
342 lines (305 loc) · 6.37 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef _MATRIX_
#define _MATRIX_
#endif
#ifndef _IO_
#include "stdio.h"
#define _IO_
#endif
#ifndef _STDLIB_
#include "stdlib.h"
#define _STDLIB_
#endif
#ifndef _MALLOC_
#include <malloc.h>
#define _MALLOC_
#endif
#ifndef _MATH_
#define _MATH_
#include <math.h>
#endif
#ifndef _VECTOR_
#include "Vec.h"
#define _VECTOR_
#endif
#ifndef _UTIL_
#include "utilMat.h"
#define _UTIL_
#endif
void unittest(){
static int a=1;
printf("%d\n",a++);
}
void unittest(int a){
printf("%d\n",a);
}
class MatrixSource{
public:
double *value;
int dim1;
int dim2;
MatrixSource(){
value=NULL;
dim1=0;
dim2=0;
}
MatrixSource(int dim1,int dim2){
this->dim1=dim1;
this->dim2=dim2;
this->value= new double[dim1*dim2];
}
double& operator()(int i,int j){
return value[(i-1)*dim2+j-1];
}
int getdim2(){
return dim2;
}
int getdim1(){
return dim1;
}
~MatrixSource(){
printf("end-mat\n");
if(this){
delete []value;
}
}
};
class Matrix{
public:
MatrixSource *Mat;
int index_style;
int dim1;
int dim2;
IntVector dim1Index;
IntVector dim2Index;
Matrix(int row,int col)//默认行索引
{
index_style=0;
Mat=new MatrixSource(row,col);
dim1=row;
dim2=col;
this->dim1Index=*(new IntVector(1,this->dim1,1));
this->dim2Index=*(new IntVector(1,this->dim2,1));
}
Matrix(){
index_style=0;
Mat=NULL;
dim1=0;
dim2=0;
dim1Index=IntVector();
dim2Index=IntVector();
}
Matrix(Matrix *mat){
this->Mat=mat->Mat;
this->dim1=mat->dim1;
this->dim2=mat->dim2;
this->index_style=mat->index_style;
this->dim1Index=*(new IntVector(1,this->dim1,1));
this->dim2Index=*(new IntVector(1,this->dim2,1));
}
Matrix(Vector Vec){
Mat=new MatrixSource();
Mat->dim1=Vec.size;
Mat->dim2=1;
index_style=0;
dim1=Vec.size;
dim2=1;
dim1Index=*(new IntVector(1,this->dim1,1));
dim2Index=*(new IntVector(1,this->dim2,1));
Mat->value=Vec.value;
}
double& operator()(int i,int j){
return (index_style==0)?(*Mat)(dim1Index(i),dim2Index(j)):(*Mat)(dim1Index(j),dim2Index(i));
}
int col(){
return (index_style==0)?dim2:dim1;
}
int row(){
return (index_style==0)?dim1:dim2;
}
IntVector& getRowIndex(){
return (index_style==0)? this->dim1Index:this->dim2Index;
}
IntVector& getColIndex(){
return (index_style==0)? this->dim2Index:this->dim1Index;
}
Matrix copy(){
Matrix newMat(dim1,dim2);
Matrix oldMat=*this;
newMat.dim1Index.copy(oldMat.dim1Index); //!!!!!需要深拷贝!回来改
newMat.dim2Index.copy(oldMat.dim2Index);
newMat.index_style=oldMat.index_style;
int row=oldMat.row();
int col=oldMat.col();
int i,j;
for (i=1;i<=row;++i)
for(j=1;j<=col;++j){
newMat(i,j)= oldMat(i,j);
}
return newMat;
}
void copy(Matrix oldMat){
Matrix newMat=*this;
newMat.dim1Index.copy(oldMat.dim1Index); //!!!!!需要深拷贝!回来改
newMat.dim2Index.copy(oldMat.dim2Index);
newMat.index_style=oldMat.index_style;
int row=oldMat.row();
int col=oldMat.col();
int i,j;
for (i=1;i<=row;++i)
for(j=1;j<=col;++j){
newMat(i,j)= oldMat(i,j);
}
}
Matrix T(){
Matrix mat(this);
mat.index_style=(mat.index_style==0)?1:0;
return mat;
}
#include "matMul.h"
#include "matPlus.h"
#include "matNeg.h"
#include "matDiv.h"
#include "matArg.h"
void toEye(){
Matrix mat=*this;
int row= mat.row();
int col= mat.col();
int i,j;
for(i=1;i<=row;++i){
for(j=1;j<=col;++j){
mat(i,j)=(i!=j)?0:1;
}
}
}
void disp(){
int i,j;
int row=this->row(),col=this->col();
Matrix mat=*this;
for(i=1;i<=row;++i){
for(j=1;j<=col;++j){
printf("%f ",mat(i,j));
}
printf("\n");
}
printf("\n");
}
void deleteData(){
delete Mat;
dim1Index.deleteData();
dim2Index.deleteData();
}
Matrix inv();
Vector toVector();
Matrix cut(int,int,int,int);
};
void matImmerse(Matrix &invmat, Matrix &mat,int row1,int row2,int col1,int col2,int descend=1){
if (col2<col1) IntSwap(col1,col2);
if (row2<row1) IntSwap(row1,row2);
if (descend==1){
int i;
double div=mat(row2,col1)/mat(row1,col1);
for(i=col1;i<=col2;++i){
mat(row2,i)-=div*mat(row1,i);
invmat(row2,i)-=div*invmat(row1,i);
}
for(i=1;i<col1;++i){
invmat(row2,i)-=div*invmat(row1,i);
}
return;
}
if(descend==-1){
int i;
double div=mat(row1,col2)/mat(row2,col2);
for(i=col2;i>=col1;--i){
mat(row1,i)-=div*mat(row2,i);
invmat(row1,i)-=div*invmat(row2,i);
}
for(i=invmat.col();i>col2;--i){
invmat(row1,i)-=div*invmat(row2,i);
}
return;
}
}
void matImmerse(Matrix &mat,int row1,int row2,int col1,int col2,int descend=1){
if (col2<col1) IntSwap(col1,col2);
if (row2<row1) IntSwap(row1,row2);
if (descend==1){
int i;
double div=mat(row2,col1)/mat(row1,col1);
for(i=col1;i<=col2;++i){
mat(row2,i)-=div*mat(row1,i);
}
return;
}
if(descend==-1){
int i;
double div=mat(row1,col2)/mat(row2,col2);
for(i=col2;i>=col1;--i){
mat(row1,i)-=div*mat(row2,i);
}
return;
}
}
void matStandard(Matrix &invmat, Matrix &mat){
int row=mat.row();
int col=mat.col();//行列不同则无法对角标准化。
if(row!=col)return;
int i,j;
for(i=1;i<=row;++i)
{
for(j=1;j<=row;++j)
invmat(i,j)/=mat(i,i);
}
}
Matrix Matrix::inv(){
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(row!=col) return Matrix();
Matrix Eye(row,row);
Eye.toEye();
Eye.disp();
Matrix newMat=(*this).copy();
int i,j,maxIndex;
IntVector rowIndex=newMat.getRowIndex();
IntVector eyeRowIndex=Eye.getRowIndex();
for(i=1;i<=row-1;++i){
maxIndex=newMat.argabsmax(i,i,-1,1);
rowIndex.swap(i,maxIndex);
eyeRowIndex.swap(i,maxIndex);
for(j=i+1;j<=row;++j){
matImmerse(Eye,newMat,i,j,i,row,1);
}
}
for(i=row;i>=2;--i){
for(j=i-1;j>=1;--j){
matImmerse(Eye,newMat,j,i,1,i,-1);
}
}
matStandard(Eye,newMat);
return Eye;
}
Vector Matrix::toVector(){
int col=(*this).col(),row=(*this).row();
if(col!=1){
return Vector();
}
Vector Vec=Vector();
Vec.size=row;
Vec.value=this->Mat->value;
Vec.size=row;
return Vec;
}
#include "matLinalg.h"
Matrix Matrix::cut(int row1, int row2,int col1, int col2){
if(row2<row1) IntSwap(row1,row2);
if(col2<col1) IntSwap(col1,col2);
int row=row2-row1+1,col=col2-col1+1;
Matrix newMat(row,col);
Matrix oldMat=*this;
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j){
newMat(i,j)=oldMat(i+row1-1,j+col1-1);
}
return newMat;
}