-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
326 lines (274 loc) · 7.24 KB
/
main.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
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
#include <iostream>
using namespace std;
//求|A|
double getA(double *arcs, int n)//按第一行展开计算|A|
{
if (n == 1) {
return arcs[0];
}
double ret = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//求去除i行j列后的矩阵的行列式
int k = 0;
double *temp = new double[(n - 1) * (n - 1)]();
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (x != i && y != j) {
temp[k++] = arcs[x * n + y];
}
}
}
double f = ((i + j) % 2 == 0 ? 1 : -1);
ret += (f * getA(temp, n - 1)) * (arcs[i * n + j]);
delete[]temp;
}
return ret;
}
return ret;
}
//求伴随矩阵
double *getAStart(double *arcs, int n)//计算每一行每一列的每个元素所对应的余子式,组成A*
{
if (n == 1) {
return arcs;
}
double *ret = new double[n * n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//求去除i行j列后的矩阵的行列式
int k = 0;
double *temp = new double[(n - 1) * (n - 1)]();
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if (x != i && y != j) {
temp[k++] = arcs[x * n + y];
}
}
}
double f = ((i + j) % 2 == 0 ? 1 : -1);
double v = (f * getA(temp, n - 1));
delete[]temp;
ret[i * n + j] = v;
}
}
return ret;
}
//求逆矩阵
double *GetMatrixInverse(double *src, int n) {
double flag = getA(src, n);
double *t = nullptr;
double *ret = new double[n * n];
if (flag == 0) {
return nullptr;
} else {
t = getAStart(src, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ret[j * n + i] = t[i * n + j] / flag;
}
}
}
return ret;
}
// m * n 矩阵
class Matrix {
private:
int m, n;
double *ptr = nullptr;
private:
void destroy() {
if (ptr)
delete[]ptr;
this->m = this->n = 0;
}
public :
Matrix() {
this->m = this->n = 0;
}
Matrix(int m, int n) {
this->m = m, this->n = n;
//分配矩阵空间
this->ptr = new double[m * n];
}
Matrix(int m, int n, double *p) {
this->m = m, this->n = n;
//分配矩阵空间
this->ptr = new double[m * n];
//
this->setMatrix(p);
}
Matrix(const Matrix &b) {
this->m = b.m;
this->n = b.n;
*this = b;
}
~Matrix() {
destroy();
}
//返回行
int getH() {
return m;
}
//返回列
int getL() {
return n;
}
//返回一维数组数据
double *getData() {
return ptr;
}
//设置矩阵数据
void setMatrix(double *mat) {
if (mat && ptr) {
memcpy(this->ptr, mat, m * n * sizeof(double));
}
}
//转置矩阵
Matrix getT() {
double *t = new double[n * m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int a = i * n + j;
int b = j * m + i;
t[b] = ptr[a];
}
}
Matrix matrix(n, m);
matrix.setMatrix(t);
delete[]t;
return matrix;
}
//输出显示矩阵
void displayMatrix() {
if (ptr) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << ptr[i * n + j] << " ";
}
cout << endl;
}
}
cout << endl;
}
//矩阵加法
Matrix operator+(const Matrix &b) const {
//不符合加法原则,返回空矩阵
if (b.m != this->m && b.n != this->n && this->ptr && b.ptr) {
return {};
}
Matrix matrix = *this;
for (int i = 0; i < b.m; i++) {
for (int j = 0; j < b.n; j++) {
matrix.ptr[i * n + j] += b.ptr[i * n + j];
}
}
return matrix;
}
//矩阵减法
Matrix operator-(const Matrix &b) const {
//不符合加法原则,返回空矩阵
if (b.m != this->m && b.n != this->n && this->ptr && b.ptr) {
return {};
}
Matrix matrix = *this;
for (int i = 0; i < b.m; i++) {
for (int j = 0; j < b.n; j++) {
matrix.ptr[i * n + j] -= b.ptr[i * n + j];
}
}
return matrix;
}
//矩阵赋值
Matrix &operator=(const Matrix &b) {
//释放原空间
destroy();
this->ptr = new double[b.m * b.n];
this->m = b.m;
this->n = b.n;
memcpy(this->ptr, b.ptr, b.n * b.m * sizeof(double));
return *this;
}
//矩阵乘法(m1,n1) * (m2,n2) = m1 * n2 n1 = m2
Matrix operator*(const Matrix &b) const {
//不符合乘法运算,结束
if (this->n != b.m || !this->ptr || !b.ptr) {
return {};
}
Matrix matrix(this->m, b.n);
for (int i = 0; i < this->m; i++) {
for (int j = 0; j < b.n; j++) {
double r = 0;
for (int k = 0; k < this->n; k++) {
r += (this->ptr[i * this->n + k] * b.ptr[k * b.n + j]);
}
matrix.ptr[i * this->n + j] = r;
}
}
return matrix;
}
//数乘
Matrix operator*(const int num) {
Matrix matrix = *this;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix.ptr[i * n + j] *= num;
}
}
return matrix;
}
//数乘
Matrix friend operator*(const int num, const Matrix &a) {
Matrix matrix = a;
for (int i = 0; i < a.m; i++) {
for (int j = 0; j < a.n; j++) {
matrix.ptr[i * a.n + j] *= num;
}
}
return matrix;
}
//求逆矩阵
Matrix getReverse() {
if (n && m && n != m) {
return {};
}
double *t = GetMatrixInverse(this->ptr, this->n);
Matrix matrix(n, n);
matrix.setMatrix(t);
delete[]t;
return matrix;
}
//判断是否对称矩阵
bool IsSymMatrix() {
if (!ptr && n != m)
return false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int a = i * m + j;
int b = j * m + i;
if (i != j && ptr[a] != ptr[b]) {
return false;
}
}
}
return true;
}
//判断是否反对称矩阵
bool IsReSymMatrix() {
if (!ptr && n != m)
return false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int a = i * m + j;
int b = j * m + i;
if (i != j && ptr[a] != -ptr[b]) {
return false;
}
}
}
return true;
}
};
int main() {
return 0;
}