-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatMul.h
166 lines (150 loc) · 3.65 KB
/
matMul.h
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
//乘法
Matrix operator* (Matrix matb){
Matrix mata=*this;
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
if(rowb!=cola){
return NULL;
}
int i,j,k;
Matrix retMat(rowa,colb);
for(i=1;i<=rowa;++i){
for(j=1;j<=colb;++j)
{
retMat(i,j)=0;
for(k=1;k<=rowb;++k){
retMat(i,j)+=mata(i,k)*matb(k,j);
}
}
}
return retMat;
}
Matrix operator*=(Matrix matb){
Matrix mata=*this;
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
if(rowb!=cola){
return NULL;
}
int i,j,k;
Matrix retMat(rowa,colb);
for(i=1;i<=rowa;++i){
for(j=1;j<=colb;++j)
{
retMat(i,j)=0;
for(k=1;k<=rowb;++k){
retMat(i,j)+=mata(i,k)*matb(k,j);
}
}
}
return retMat;
}
Matrix operator*(double C){
Matrix mat=*this;
int row=mat.row(),col=mat.col();
Matrix newMat(row,col);
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
{
newMat(i,j)=mat(i,j)*C;
}
return newMat;
}
Matrix operator*=(double C){
Matrix mat=*this;
int row=mat.row(),col=mat.col();
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
{
mat(i,j)*=C;
}
return mat;
}
Matrix operator*(int C){
Matrix mat=*this;
double Cf=double(C);
return mat*Cf;
}
Matrix operator*=(int C){
Matrix mat=*this;
double Cf=double(C);
return mat*=Cf;
}
void getLinear(Matrix mata,Matrix matb,Matrix matc){
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col(),rowc=matc.row(),colc=matc.col();
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(rowb!=cola||row!=rowa||col!=colb||rowc!=row||colc!=col){printf("error_getLinear\n"); return;}
int i,j,k;
for(i=1;i<=row;++i){
for(j=1;j<=col;++j)
{
mat(i,j)=0;
for(k=1;k<=rowb;++k){
mat(i,j)+=mata(i,k)*matb(k,j);
}
mat(i,j)+=matc(i,j);
}
}
}
void getMul(Matrix mata,Matrix matb){
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(rowb!=cola||row!=rowa||col!=colb){printf("error_getMul\n"); return;}
int i,j,k;
for(i=1;i<=row;++i){
for(j=1;j<=col;++j)
{
mat(i,j)=0;
for(k=1;k<=rowb;++k){
mat(i,j)+=mata(i,k)*matb(k,j);
}
}
}
}
void getAdd(Matrix mata,Matrix matb){
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(row!=rowa||row!=rowb||col!=cola||col!=colb) {printf("error_getAdd\n"); return;}
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
mat(i,j)=mata(i,j)+matb(i,j);
}
void getSub(Matrix mata,Matrix matb){
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(row!=rowa||row!=rowb||col!=cola||col!=colb) {printf("error_getSub\n"); return;}
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
mat(i,j)=mata(i,j)-matb(i,j);
}
void Hadamard(Matrix mata,Matrix matb){
int rowa=mata.row(),rowb=matb.row(),cola=mata.col(),colb=matb.col();
Matrix mat=*this;
int row=mat.row(),col=mat.col();
if(row!=rowa||row!=rowb||col!=cola||col!=colb) {printf("error_Hadamard\n"); return;}
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
mat(i,j)=mata(i,j)*matb(i,j);
}
void randomShack(){
double sum=0,v;
Matrix mat=*this;
int i,j,row=mat.row(),col=mat.col();
for(i=1;i<=row;i++)
for(j=1;j<=col;++j)
sum+=fabs(mat(i,j));
sum/=row*col/100;
rand()/(1.0*RAND_MAX);
for(i=1;i<=row;i++)
for(j=1;j<=col;++j)
{ v=rand()/(1.0*RAND_MAX)*sum;
mat+=(rand()%2==0)?v:-v;
}
}