-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.cpp
240 lines (217 loc) · 5.7 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
#include "Matrix.h"
Matrix::Matrix(){
m_rows = 0;
m_columns = 0;
m_data = NULL;
}
Matrix::Matrix(int r, int c){
m_rows = r;
m_columns = c;
m_data = new double[r*c];
ClearMtr();
}
Matrix::Matrix(int r, int c, double* data){
m_rows = r;
m_columns = c;
m_data = new double[r*c];
Setdata(data,r*c);
}
Matrix::~Matrix(){
//dtor
this->Empty();
}
Matrix::Matrix(const Matrix& other){
//copy ctor
int r = other.m_rows;
int c = other.m_columns;
ResizeMtr(r,c);
for(int i=0; i<r*c; i++){
m_data[i] = other.m_data[i];
}
}
Matrix& Matrix::operator=(const Matrix& rhs){
if (this == &rhs) return *this; // handle self assignment
//assignment operator
int r = rhs.m_rows;
int c = rhs.m_columns;
this->ResizeMtr(r, c);
for(int i=0; i<r*c; i++)
this->m_data[i] = rhs.m_data[i];
return *this;
}
Matrix Matrix::operator+(const Matrix& val){
Matrix m = Matrix(m_rows,m_columns);
try{
if(m_rows!=val.Getrows() || m_columns!=val.Getcolumns())
throw string("作用于+的操作数维数不同\n");
for(int i=0; i< m_rows*m_columns; i++)
m.m_data[i] = m_data[i] + val.m_data[i];
}
catch(string& e){
printf("%s\n", e.c_str());
}
return m;
}
Matrix Matrix::operator+(const double val){
Matrix m = Matrix(m_rows,m_columns);
for(int i=0; i< m_rows*m_columns; i++)
m.m_data[i] = m_data[i] + val;
return m;
}
Matrix Matrix::operator-(const Matrix& val){
Matrix m = Matrix(m_rows,m_columns);
try{
if(m_rows!=val.m_rows || m_columns!=val.m_columns)
throw string("作用于-的操作数维数不同\n");
for(int i=0; i< m_rows*m_columns; i++)
m.m_data[i] = m_data[i] - val.m_data[i];
}
catch(string& e){
printf("%s\n", e.c_str());
}
return m;
}
Matrix Matrix::operator*(const Matrix& val){
double sum;
Matrix m = Matrix(m_rows,val.m_columns);
try{
if(m_columns!=val.m_rows)
throw string("两个操作数无法相乘\n");
for(int row=1; row<=m_rows; row++){
for(int col=1; col<=val.m_columns; col++){
sum = 0;
for(int i=1; i<=val.m_columns; i++)
sum += (this->Get(row,i) * val.Get(i, col));
m.Set(row, col, sum);
}
}
}
catch(string& e){
printf("%s\n", e.c_str());
}
return m;
}
void Matrix::ResizeMtr(int r, int c){
if(m_rows!=r||m_columns!=c){
this->Empty();
m_rows = r;
m_columns = c;
m_data = new double[r*c];
}
ClearMtr();
}
void Matrix::ClearMtr(){
for(int i=0; i<m_rows*m_columns; i++)
m_data[i] = 0;
}
double Matrix::Get(int r, int c) const{
r--;
c--;
return m_data[c*m_rows+r];
}
void Matrix::Set(int r, int c, double val){
r--;
c--;
m_data[c*m_rows+r] = val;
}
void Matrix::Setdata(double* val, int len){
for(int i=0; i<len; i++)
m_data[i] = val[i];
}
void Matrix::DisplayMtr(){
for(int r=1; r<=Getrows(); r++){
for(int c=1; c<=Getcolumns(); c++)
cout << Get(r,c) << "\t";
cout << "\n";
}
}
Matrix Matrix::InverseMtr(){
Matrix m = Matrix(this->Getrows(),this->Getcolumns(),this->Getdata());
try{
if(m_rows!=m_columns)
throw string("不是方阵,没有逆矩阵\n");
inv(m.m_data, m_rows);
}
catch(string& e){
printf("%s\n", e.c_str());
}
return m;
}
Matrix Matrix::TransposeMtr(){
Matrix m = Matrix(m_columns, m_rows);
for(int row=1; row<=m.m_rows; row++){
for(int col=1; col<=m.m_columns; col++)
m.Set(row,col,Get(col,row));
}
return m;
}
void Matrix::inv(double *a,int n){
try{
int *is,*js,i,j,k,l,u,v;
double d,p;
is=(int*)malloc(n*sizeof(int));
js=(int*)malloc(n*sizeof(int));
for (k=0; k<=n-1; k++){
d=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++){
l=i*n+j; p=fabs(a[l]);
if (p>d){
d=p; is[k]=i; js[k]=j;
}
}
if (d+1.0==1.0){
free(is); free(js); throw string("err**not inv\n");
}
if (is[k]!=k)
for (j=0; j<=n-1; j++){
u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i<=n-1; i++){
u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j<=n-1; j++)
if (j!=k){
u=k*n+j; a[u]=a[u]*a[l];
}
for (i=0; i<=n-1; i++)
if (i!=k)
for (j=0; j<=n-1; j++)
if (j!=k){
u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i<=n-1; i++)
if (i!=k){
u=i*n+k; a[u]=-a[u]*a[l];
}
}
for (k=n-1; k>=0; k--){
if (js[k]!=k)
for (j=0; j<=n-1; j++){
u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i<=n-1; i++){
u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
}
catch(string& e){
printf("%s\n", e.c_str());
}
}
void Matrix::Empty(){
if(m_data!=NULL){
delete[] m_data;
m_data = NULL;
}
}