卷积神经网络常用的矩阵工具类。

封装的很好,各类符号都有重载,傻瓜式用法。

也可以作为二维数组来用,不需要非常麻烦地对内存的创建与销毁。

用法

1
2
3
4
5
6
7
Matrix m(3, 3);		// 定义矩阵变量
m[1][2] = 3; // 赋值
m *= 2; // 乘以int
if (!m.isAll0()) // 全零矩阵
m.fill(0) // 填充0
if (m != m1) // 矩阵比较
m = m1 * m2; // 矩阵相乘

matrix.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
#ifndef METRIX_H
#define METRIX_H

#include <iostream>
#include <memory>

#define EPS 1e-4
#define DIS0(x) (abs(x) <= EPS)
#define NDIS0(x) (abs(x) > EPS)

class Matrix
{
private:
int rows_num = 0, cols_num = 0;
double **p = nullptr;
int bit = 8; // 每个数值占据几bit
int top = 0, left = 0; // 左上角的位置(如果有嵌套矩阵)
int key = 0; // 用来存个key或者id,或许要比较

private:
void initialize(); // 初始化矩阵

public:
Matrix();
Matrix(int, int);
Matrix(int rows, int cols, int val);
Matrix(const Matrix &m);
virtual ~Matrix(); // 析构函数应当是虚函数,除非此类不用做基类

Matrix &operator=(const Matrix &); // 矩阵的复制
Matrix &operator=(double *); // 将数组的值传给矩阵
bool operator==(const Matrix &) const;
bool operator!=(const Matrix &) const;
Matrix &operator+=(const Matrix &); // 矩阵的 += 操作
Matrix operator+(const Matrix &); // 矩阵的 + 操作
Matrix &operator-=(const Matrix &); // -=
Matrix operator-(const Matrix &); // -
Matrix &operator*=(const Matrix &); // *=
Matrix operator*(const Matrix &m) const; // 矩阵相乘
Matrix operator*(double mul) const; // 矩阵乘数字
Matrix &operator*=(double mul);
Matrix &operator<<=(int bit); // 移位操作
Matrix &operator>>=(int bit);
Matrix operator<<(int bit) const;
Matrix operator>>(int bit) const;
double *operator[](int row) const; // 取值操作 m[row][col]
Matrix operator()(int row, int col, int r, int c) const; // 取子矩阵

void fill(double value); // 全部填充相同的值
bool isNull() const; // 是否未初始化
bool isEmpty() const; // 是否是空矩阵
bool isAll0() const; // 是否是零矩阵
bool isRow0(int row) const; // 一整行是否是0
bool isCol0(int col) const; // 一整列是否是0
void swapRows(int, int); // 交换行
bool equal(Matrix& m, int* diffRow, int* diffCol, double *val1, double *val2) const; // 比较矩阵,并返回差异的位置

double determinant() const; // 矩阵的行列式
Matrix T() const; // 矩阵的转置

double point(int i, int j) const;
void set(int i, int j, double val);
void add(int i, int j, double val);
int row() const;
int col() const;
Matrix rowLine(int r) const;
Matrix colLine(int c) const;
Matrix subMatrix(int row, int col, int h, int w) const; // 提取一部分的矩阵
void set(int row, int col, const Matrix &m); // 设置一部分的矩阵
void add(int row, int col, const Matrix &m); // 修改一部分的值
void add(const Matrix &m); // 从左上角起与某个矩阵相加

void setPos(int top, int left);
int getTop() const;
int getLeft() const;
void setKey(int key);
int getKey() const;
Matrix &setBit(int bit);
int getBit() const;

static Matrix solve(const Matrix &, const Matrix &); // 求解线性方程组 Ax=b
static Matrix inverse(Matrix); // 求矩阵的逆矩阵
static Matrix unit(int); // 制造一个单位矩阵
Matrix gaussianEliminate(); // 高斯消元法

void setAllRandom(int mod = 10); // 全部设置为随机数
void print() const; // 矩阵显示
friend std::istream &operator>>(std::istream &, Matrix &); // 实现矩阵的输入
};

#endif // METRIX_H

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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
#include <assert.h>
#include "matrix.h"

using std::cout;
using std::endl;
using std::istream;

void Matrix::initialize() // 初始化矩阵大小
{
p = new double *[rows_num]; // 分配 rows_num 个指针
for (int i = 0; i < rows_num; ++i)
{
p[i] = new double[cols_num]; // 为 p [i] 进行动态内存分配,大小为 cols
}
}

Matrix::Matrix() : Matrix(0, 0, 0)
{
}

//// 声明一个全 0 矩阵
Matrix::Matrix(int rows, int cols) : Matrix(rows, cols, 0)
{
/* rows_num = rows;
cols_num = cols;
initialize();
// 不设置初始值,增加效率 */
}

Matrix::Matrix(int rows, int cols, int val)
{
rows_num = rows;
cols_num = cols;
initialize();
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = val;
}
}
}

/// 拷贝构造函数
Matrix::Matrix(const Matrix &m)
{
if (rows_num != m.rows_num || cols_num != m.cols_num)
{
if (p && rows_num && cols_num)
{
for (int i = 0; i < rows_num; ++i)
{
delete[] p[i];
}
delete[] p;
}

rows_num = m.rows_num;
cols_num = m.cols_num;
initialize();
}

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = m.p[i][j];
}
}
}

// 析构函数
Matrix::~Matrix()
{
if (!p || !rows_num || !cols_num)
return;
for (int i = 0; i < rows_num; ++i)
{
delete[] p[i];
}
delete[] p;
p = nullptr;
}

// 实现矩阵的复制
Matrix &Matrix::operator=(const Matrix &m)
{
if (this == &m)
return *this;

if (rows_num != m.rows_num || cols_num != m.cols_num)
{
if (p && rows_num && cols_num)
{
for (int i = 0; i < rows_num; ++i)
{
delete[] p[i];
}
delete[] p;
}

rows_num = m.rows_num;
cols_num = m.cols_num;
initialize();
}

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = m.p[i][j];
}
}

return *this;
}

// 将数组的值传递给矩阵 (要求矩阵的大小已经被声明过了)
Matrix &Matrix::operator=(double *a)
{
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = *(a + i * cols_num + j);
}
}
return *this;
}

bool Matrix::operator==(const Matrix &m) const
{
if (rows_num != m.rows_num || cols_num != m.cols_num)
return false;
for (int i = 0; i < rows_num; i++)
for (int j = 0; j < cols_num; j++)
if (NDIS0(p[i][j] - m.p[i][j]))
return false;
return true;
}

bool Matrix::operator!=(const Matrix &m) const
{
return !(*this == m);
}

//+= 操作
Matrix &Matrix::operator+=(const Matrix &m)
{
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] += m.p[i][j];
}
}
return *this;
}

//+ 操作
Matrix Matrix::operator+(const Matrix &m)
{
Matrix add(rows_num, cols_num);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
add.p[i][j] = p[i][j] + m.p[i][j];
}
}
return add;
}

// 实现 -=
Matrix &Matrix::operator-=(const Matrix &m)
{
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] -= m.p[i][j];
}
}
return *this;
}

//- 操作
Matrix Matrix::operator-(const Matrix &m)
{
Matrix cut(rows_num, cols_num);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
cut.p[i][j] = p[i][j] - m.p[i][j];
}
}
return cut;
}

// 实现 *=
Matrix &Matrix::operator*=(const Matrix &m)
{
Matrix temp(rows_num, m.cols_num, 0); // 若 C=AB, 则矩阵 C 的行数等于矩阵 A 的行数,C 的列数等于 B 的列数。
for (int i = 0; i < temp.rows_num; i++)
{
for (int j = 0; j < temp.cols_num; j++)
{
for (int k = 0; k < cols_num; k++)
{
temp.p[i][j] += (p[i][k] * m.p[k][j]);
}
}
}
*this = temp;
return *this;
}

// 实现矩阵的乘法
Matrix Matrix::operator*(const Matrix &m) const
{
assert(cols_num == m.rows_num);
Matrix ba_M(rows_num, m.cols_num, 0);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < m.cols_num; j++)
{
for (int k = 0; k < cols_num; k++)
{
ba_M.p[i][j] += (p[i][k] * m.p[k][j]);
}
}
}
return ba_M;
}

Matrix Matrix::operator*(double mul) const
{
if (DIS0(mul - 1))
return *this;
Matrix m(rows_num, cols_num);
if (DIS0(mul))
return m;

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
m.p[i][j] = p[i][j] * mul;
}
}
return m;
}

Matrix &Matrix::operator*=(double mul)
{
if (DIS0(mul - 1))
return *this;

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] *= mul;
}
}
return *this;
}

Matrix &Matrix::operator<<=(int bit)
{
if (!bit)
return *this;

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = (int(p[i][j]+EPS) << bit);
}
}
return *this;
}

Matrix &Matrix::operator>>=(int bit)
{
if (!bit)
return *this;

for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = (int(p[i][j]+EPS) >> bit);
}
}
return *this;
}

Matrix Matrix::operator<<(int bit) const
{
Matrix m(this->rows_num, this->cols_num);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
m.p[i][j] = (int(p[i][j]+EPS) << bit);
}
}
return m;
}

Matrix Matrix::operator>>(int bit) const
{
Matrix m(this->rows_num, this->cols_num);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
m.p[i][j] = (int(p[i][j]+EPS) >> bit);
}
}
return m;
}

double *Matrix::operator[](int row) const
{
return p[row];
}

Matrix Matrix::operator()(int row, int col, int r, int c) const
{
return subMatrix(row, col, r, c);
}

void Matrix::fill(double value)
{
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = value;
}
}
}

bool Matrix::isNull() const
{
return p == nullptr;
}

bool Matrix::isEmpty() const
{
return !(p && rows_num && cols_num);
}

bool Matrix::isAll0() const
{
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
if (NDIS0(p[i][j]))
return false;
}
}
return true;
}

bool Matrix::isRow0(int row) const
{
for (int i = 0; i < cols_num; i++)
{
if (NDIS0(p[row][i]))
return false;
}
return true;
}

bool Matrix::isCol0(int col) const
{
for (int i = 0; i < rows_num; i++)
{
if (NDIS0(p[i][col]))
return false;
}
return true;
}

// 实现行变换
void Matrix::swapRows(int a, int b)
{
a--;
b--;
double *temp = p[a];
p[a] = p[b];
p[b] = temp;
}

/// 比较两个矩阵,并返回不同的位置
/// 相等true,不相等false
bool Matrix::equal(Matrix &m, int *diffRow, int *diffCol, double* val1, double *val2) const
{
if (diffRow)
*diffRow = -1;
if (diffCol)
*diffCol = -1;
if (val1)
*val1 = 0;
if (val2)
*val2 = 0;
if (rows_num != m.rows_num || cols_num != m.cols_num)
return false;
for (int i = 0; i < rows_num; i++)
for (int j = 0; j < cols_num; j++)
if (NDIS0(p[i][j] - m.p[i][j]))
{
if (diffRow)
*diffRow = i;
if (diffCol)
*diffCol = j;
if (val1)
*val1 = p[i][j];
if (val2)
*val2 = m.p[i][j];
return false;
}
return true;
}

// 计算矩阵行列式的值
double Matrix::determinant() const
{
// 为计算行列式做一个备份
double **back_up;
back_up = new double *[rows_num];
for (int i = 0; i < rows_num; i++)
{
back_up[i] = new double[cols_num];
}
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
back_up[i][j] = p[i][j];
}
}

assert(rows_num == cols_num); // 只有方阵才能计算行列式,否则调用中断强制停止程序

double ans = 1;
for (int i = 0; i < rows_num; i++)
{
// 通过行变化的形式,使得矩阵对角线上的主元素不为 0
if (NDIS0(p[i][i]))
{
bool flag = false;
for (int j = 0; (j < cols_num) && (!flag); j++)
{
// 若矩阵的一个对角线上的元素接近于 0 且能够通过行变换使得矩阵对角线上的元素不为 0
if (NDIS0(p[i][j]) && NDIS0(p[j][i]))
{
flag = true;
// 注:进行互换后,p [i][j] 变为 p [j][j],p [j][i] 变为 p [i][i]
// 对矩阵进行行变换
double temp;
for (int k = 0; k < cols_num; k++)
{
temp = p[i][k];
p[i][k] = p[j][k];
p[j][k] = temp;
}
}
}
if (flag)
return 0;
}
}
for (int i = 0; i < rows_num; i++)
{
for (int j = i + 1; j < rows_num; j++)
{
for (int k = i + 1; k < cols_num; k++)
{
p[j][k] -= p[i][k] * (p[j][i] * p[i][i]);
}
}
}
for (int i = 0; i < rows_num; i++)
{
ans *= p[i][i];
}
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
p[i][j] = back_up[i][j];
}
}
return ans;
}

/// 矩阵的转置
Matrix Matrix::T() const
{
Matrix T(cols_num, rows_num);
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
T.set(j, i, point(i, j));
}
}
return T;
}

/// 返回矩阵第 i 行第 j 列的数
double Matrix::point(int i, int j) const
{
return this->p[i][j];
}

void Matrix::set(int i, int j, double val)
{
p[i][j] = val;
}

void Matrix::add(int i, int j, double val)
{
p[i][j] += val;
}

/// 读取矩阵行列数
int Matrix::row() const
{
return rows_num;
}

int Matrix::col() const
{
return cols_num;
}

Matrix Matrix::rowLine(int r) const
{
return subMatrix(r, 0, 1, cols_num);
}

Matrix Matrix::colLine(int c) const
{
return subMatrix(0, c, rows_num, 1);
}

Matrix Matrix::subMatrix(int row, int col, int h, int w) const
{
Matrix m(h, w);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
m.set(i, j, point(i + row, j + col));
}
}
return m;
}

void Matrix::set(int row, int col, const Matrix &m)
{
int h = m.rows_num, w = m.cols_num;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
set(row + i, col + j, m.point(i, j));
}
}
}

void Matrix::add(int row, int col, const Matrix &m)
{
int h = m.rows_num, w = m.cols_num;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
set(row + i, col + j, point(row + i, col + j) + m.point(i, j));
}
}
}

void Matrix::add(const Matrix &m)
{
add(0, 0, m);
}

void Matrix::setPos(int top, int left)
{
this->top = top;
this->left = left;
}

int Matrix::getTop() const
{
return top;
}

int Matrix::getLeft() const
{
return left;
}

void Matrix::setKey(int key)
{
this->key = key;
}

int Matrix::getKey() const
{
return key;
}

Matrix &Matrix::setBit(int bit)
{
this->bit = bit;
return *this;
}

int Matrix::getBit() const
{
return bit;
}

// 解方程 Ax=b
Matrix Matrix::solve(const Matrix &A, const Matrix &b)
{
// 高斯消去法实现 Ax=b 的方程求解
for (int i = 0; i < A.rows_num; i++)
{
if (DIS0(A.p[i][i]))
cout << "error: p[i][i] equals 0" << endl;
for (int j = i + 1; j < A.rows_num; j++)
{
for (int k = i + 1; k < A.cols_num; k++)
{
A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
}
b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
A.p[j][i] = 0;
}
}

// 反向代换
Matrix x(b.rows_num, 1);
x.p[x.rows_num - 1][0] = b.p[x.rows_num - 1][0] / A.p[x.rows_num - 1][x.rows_num - 1];
for (int i = x.rows_num - 2; i >= 0; i--)
{
double sum = 0;
for (int j = i + 1; j < x.rows_num; j++)
{
sum += A.p[i][j] * x.p[j][0];
}
x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
}

return x;
}

// 求矩阵的逆矩阵
Matrix Matrix::inverse(Matrix A)
{
assert(A.rows_num != A.cols_num); // 只有方阵能求逆矩阵

double temp;
Matrix A_B = Matrix(A.rows_num, A.cols_num);
A_B = A; // 为矩阵 A 做一个备份
Matrix B = unit(A.rows_num);
// 选择需要互换的两行选主元
for (int i = 0; i < A.rows_num; i++)
{
if (NDIS0(A.p[i][i]))
{
bool flag = false;
for (int j = 0; (j < A.rows_num) && (!flag); j++)
{
if (NDIS0(A.p[i][j]) && NDIS0(A.p[j][i]))
{
flag = true;
for (int k = 0; k < A.cols_num; k++)
{
temp = A.p[i][k];
A.p[i][k] = A.p[j][k];
A.p[j][k] = temp;
temp = B.p[i][k];
B.p[i][k] = B.p[j][k];
B.p[j][k] = temp;
}
}
}
assert(flag); // 逆矩阵不存在
}
}

// 通过初等行变换将 A 变为上三角矩阵
double temp_rate;
for (int i = 0; i < A.rows_num; i++)
{
for (int j = i + 1; j < A.rows_num; j++)
{
temp_rate = A.p[j][i] / A.p[i][i];
for (int k = 0; k < A.cols_num; k++)
{
A.p[j][k] -= A.p[i][k] * temp_rate;
B.p[j][k] -= B.p[i][k] * temp_rate;
}
A.p[j][i] = 0;
}
}

// 使对角元素均为 1
for (int i = 0; i < A.rows_num; i++)
{
temp = A.p[i][i];
for (int j = 0; j < A.cols_num; j++)
{
A.p[i][j] /= temp;
B.p[i][j] /= temp;
}
}

//std::cout<<"算法可靠性检测,若可靠,输出上三角矩阵"<<std::endl;
// 将已经变为上三角矩阵的 A,变为单位矩阵
for (int i = A.rows_num - 1; i >= 1; i--)
{
for (int j = i - 1; j >= 0; j--)
{
temp = A.p[j][i];
for (int k = 0; k < A.cols_num; k++)
{
A.p[j][k] -= A.p[i][k] * temp;
B.p[j][k] -= B.p[i][k] * temp;
}
}
}

std::cout << "算法可靠性检测,若可靠,输出单位矩阵" << std::endl;
for (int i = 0; i < A.rows_num; i++)
{
for (int j = 0; j < A.cols_num; j++)
{
printf("%7.4lf\t\t", A.p[i][j]);
}
cout << endl;
}
A = A_B; // 还原 A
return B; // 返回该矩阵的逆矩阵
}

// 制造一个单位矩阵
Matrix Matrix::unit(int n)
{
Matrix A(n, n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
A.p[i][j] = 1;
}
else
{
A.p[i][j] = 0;
}
}
}
return A;
}

// 高斯消元法
Matrix Matrix::gaussianEliminate()
{
Matrix Ab(*this);
int rows = Ab.rows_num;
int cols = Ab.cols_num;
int Acols = cols - 1;

int i = 0; // 跟踪行
int j = 0; // 跟踪列
while (i < rows)
{
bool flag = false;
while (j < Acols && !flag)
{
if (NDIS0(Ab.p[i][j]))
{
flag = true;
}
else
{
int max_row = i;
double max_val = 0;
for (int k = i + 1; k < rows; ++k)
{
double cur_abs = Ab.p[k][j] >= 0 ? Ab.p[k][j] : -1 * Ab.p[k][j];
if (cur_abs > max_val)
{
max_row = k;
max_val = cur_abs;
}
}
if (max_row != i)
{
Ab.swapRows(max_row, i);
flag = true;
}
else
{
j++;
}
}
}
if (flag)
{
for (int t = i + 1; t < rows; t++)
{
for (int s = j + 1; s < cols; s++)
{
Ab.p[t][s] = Ab.p[t][s] - Ab.p[i][s] * (Ab.p[t][j] / Ab.p[i][j]);
}
Ab.p[t][j] = 0;
}
}
i++;
j++;
}
return Ab;
}

void Matrix::setAllRandom(int mod)
{
for (int row = 0; row < rows_num; row++)
{
for (int col = 0; col < cols_num; col++)
{
p[row][col] = rand() % mod;
}
}
}

// 矩阵显示
void Matrix::print() const
{
//cout << rows_num <<" "<<cols_num<< endl;// 显示矩阵的行数和列数
for (int i = 0; i < rows_num; i++)
{
for (int j = 0; j < cols_num; j++)
{
cout << p[i][j] << " ";
}
cout << endl;
}
}

// 实现矩阵的输入
istream &operator>>(istream &is, Matrix &m)
{
for (int i = 0; i < m.rows_num; i++)
{
for (int j = 0; j < m.cols_num; j++)
{
is >> m.p[i][j];
}
}
return is;
}