-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix.cpp
More file actions
339 lines (285 loc) · 5.05 KB
/
Copy pathmatrix.cpp
File metadata and controls
339 lines (285 loc) · 5.05 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
#include "stdafx.h"
#include "matrix.h"
_MATRIX CMyMatrix::CMyMatrix()
{
nCol = 0;
nRow = 0;
pMatrix = NULL;
}
_MATRIX CMyMatrix::CMyMatrix(unsigned int row, unsigned int col, double value)
{
nCol = col;
nRow = row;
pMatrix = new double [nRow * nCol];
if(pMatrix != NULL)
{
for(unsigned int i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = value;
}
}
}
_MATRIX CMyMatrix::CMyMatrix(const CMyMatrix &source)
{
if(this != &source)
{
nRow = source.nRow;
nCol = source.nCol;
pMatrix = new double [nRow * nCol];
if(pMatrix != NULL)
{
for(unsigned int i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = source.pMatrix[i];
}
}
}
}
_MATRIX CMyMatrix::~CMyMatrix()
{
if(pMatrix != NULL)
{
delete []pMatrix;
}
}
_MATRIX void CMyMatrix::Add(const CMyMatrix &source)
{
if(pMatrix != NULL && nRow == source.nRow && nCol == source.nCol)
{
for(unsigned int i = 0; i < nRow * nCol; ++i)
{
pMatrix[i] += source.pMatrix[i];
}
}
}
_MATRIX unsigned int CMyMatrix::Col()
{
return nCol;
}
_MATRIX unsigned int CMyMatrix::Row()
{
return nRow;
}
_MATRIX void CMyMatrix::Resize(unsigned int row, unsigned int col, double value)
{
delete []pMatrix;
nRow = row;
nCol = col;
pMatrix = new double [nRow * nCol];
if(pMatrix != NULL)
{
for(unsigned int i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = value;
}
}
}
_MATRIX void CMyMatrix::Transposition()
{
double *temp;
unsigned int i, j;
temp = new double [nRow * nCol];
if(temp != NULL)
{
for(i = 0; i < nRow * nCol; i++)
{
temp[i] = pMatrix[i];
}
}
nRow += nCol;
nCol = nRow - nCol;
nRow = nRow - nCol;
for(i = 0; i < nRow; i++)
{
for(j = 0; j < nCol; ++j)
{
pMatrix[i * nCol + j] = temp[j * nRow + i];
}
}
delete []temp;
}
_MATRIX CMyMatrix & CMyMatrix::operator=(const CMyMatrix &a)
{
unsigned int i;
if(nRow != a.nRow || nCol != a.nCol)
{
printf("Unmatch CMyMatrix!\n");
return *this;
}
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = a.pMatrix[i];
}
return *this;
}
_MATRIX CMyMatrix & CMyMatrix::operator=(double value)
{
unsigned int i;
pMatrix = new double[nRow * nCol];
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = value;
}
return *this;
}
//重载+运算符,要求两个矩阵的大小相同
_MATRIX const CMyMatrix CMyMatrix::operator+(const CMyMatrix &a)
{
unsigned int i;
if(nRow != a.nRow || nCol != a.nCol)
{
printf("Unmatch CMyMatrix!\n");
return *this;
}
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] += a.pMatrix[i];
}
return *this;
}
_MATRIX const CMyMatrix CMyMatrix::operator+(double value)
{
unsigned int i;
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] += value;
}
return *this;
}
//重载-运算符,要求两个矩阵的大小相同
_MATRIX const CMyMatrix CMyMatrix::operator-(const CMyMatrix &a)
{
unsigned int i;
if(nRow != a.nRow || nCol != a.nCol)
{
printf("Unmatch CMyMatrix!\n");
return *this;
}
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] = pMatrix[i] - a.pMatrix[i];
}
return *this;
}
_MATRIX const CMyMatrix CMyMatrix::operator-(double value)
{
unsigned int i;
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] -= value;
}
return *this;
}
//重载*运算符,要求矩阵a的列数等于b的行数
_MATRIX const CMyMatrix CMyMatrix::operator*(const CMyMatrix &a)
{
unsigned int i, j, k;
double tmp;
if(nCol != a.nRow)
{
printf("Unmatch CMyMatrix!\n");
return *this;
}
CMyMatrix temp(nRow, a.nCol, 0);
for(i = 0; i < nRow; i++)
{
for(j = 0; j < a.nCol; j++)
{
tmp = 0;
for(k = 0; k < a.nRow; k++)
{
tmp += pMatrix[i * nCol + k] * a.pMatrix[k * a.nCol + j];
}
temp.pMatrix[i * a.nCol + j] = tmp;
}
}
return temp;
}
_MATRIX const CMyMatrix CMyMatrix::operator*(double value)
{
unsigned int i;
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] *= value;
}
return *this;
}
///重载==运算符,要求两个矩阵的大小相同
_MATRIX bool CMyMatrix::operator==(const CMyMatrix &a)
{
unsigned int i;
if(nRow != a.nRow || nCol != a.nCol)
{
printf("Unmatch CMyMatrix!\n");
return false;
}
for(i = 0; i < nRow * nCol; i++)
{
if(this->pMatrix[i] != a.pMatrix[i])
{
return false;
}
}
return true;
}
_MATRIX bool CMyMatrix::operator==(double value)
{
unsigned int i;
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] != value;
return false;
}
return true;
}
_MATRIX bool CMyMatrix::operator!=(const CMyMatrix &source)
{
unsigned int i;
if(nRow != source.nRow || nCol != source.nCol)
{
printf("Unmatch CMyMatrix!\n");
return true;
}
else
{
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] == source.pMatrix[i];
return false;
}
return true;
}
}
_MATRIX bool CMyMatrix::operator!=(double value)
{
unsigned int i;
for(i = 0; i < nRow * nCol; i++)
{
pMatrix[i] == value;
return false;
}
return true;
}
_MATRIX double CMyMatrix::get(unsigned int row, unsigned int col)
{
double value;
if (row > nRow || col > nCol)
{
return -1;
}
else
{
value = pMatrix[row * nCol +col];
return value;
}
}
_MATRIX void CMyMatrix::set(unsigned int row, unsigned int col, double value)
{
if (row > nRow || col > nCol)
{
}
else
{
pMatrix[row * nCol +col] = value;
}
}