-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
267 lines (249 loc) · 5.23 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
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
#include "Matrix.h"
using std::endl;
using std::cout;
using std::istream;
const double EPS = 1e-10;
void Matrix::initialize() {//初始化矩阵大小
for (int i = 0; i < rows_num; ++i) {
p.push_back(vector<double>());
for (int j = 0; j < cols_num; ++j) {
p[i].push_back(0);
}
}
}
//声明一个全0矩阵
Matrix::Matrix(int rows, int cols) :p(NULL)
{
rows_num = rows;
cols_num = cols;
initialize();
}
//声明一个值全部为value的矩阵
Matrix::Matrix(int rows, int cols, double value) :p(NULL)
{
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] = value;
}
}
}
//析构函数
Matrix::~Matrix() {
p.clear();
}
//复制构造
Matrix::Matrix(const Matrix& m) {
p.clear();
rows_num = m.row();
cols_num = m.col();
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::operator=(const Matrix& m)
{
if (this == &m) {
return *this;
}
if (rows_num != m.rows_num || cols_num != m.cols_num) {
p.clear();
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;
}
//+=操作
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 res(rows_num, cols_num);
for (int i = 0; i < rows_num; i++) {
for (int j = 0; j < cols_num; j++) {
res.p[i][j] = p[i][j] - m.p[i][j];
}
}
return res;
}
Matrix Matrix::operator+(const Matrix& m)
{
Matrix res(rows_num, cols_num);
for (int i = 0; i < rows_num; i++) {
for (int j = 0; j < cols_num; j++) {
res.p[i][j] = p[i][j] + m.p[i][j];
}
}
return res;
}
//实现-=
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 temp(rows_num, m.cols_num);//若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 {
Matrix ba_M(rows_num, m.cols_num, 0.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;
}
//矩阵显示
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;
}
cout << endl;
}
//计算矩阵行列式的值
double Matrix::det() {
//为计算行列式做一个备份
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];
}
}
if (rows_num != cols_num) {
std::abort();//只有方阵才能计算行列式,否则调用中断强制停止程序
}
double ans = 1;
for (int i = 0; i < rows_num; i++) {
//通过行变化的形式,使得矩阵对角线上的主元素不为0
if (abs(p[i][i]) <= EPS) {
bool flag = false;
for (int j = 0; (j < cols_num) && (!flag); j++) {
//若矩阵的一个对角线上的元素接近于0且能够通过行变换使得矩阵对角线上的元素不为0
if ((abs(p[i][j]) > EPS) && (abs(p[j][i]) > EPS)) {
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;
}
//返回矩阵第i行第j列的数
double Matrix::at(int i, int j) const {
return this->p[i][j];
}
//制造一个单位矩阵
Matrix Matrix::eye(int n, double k = 1.0) {
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] = k;
}
else {
A.p[i][j] = 0;
}
}
}
return A;
}
//读取矩阵行列数
int Matrix::row() const {
return rows_num;
}
int Matrix::col() const {
return cols_num;
}
//实现矩阵的转置
Matrix Matrix::T(const Matrix& m)
{
int col_size = m.col();
int row_size = m.row();
Matrix mt(col_size, row_size);
for (int i = 0; i < row_size; i++) {
for (int j = 0; j < col_size; j++) {
mt.p[j][i] = m.p[i][j];
}
}
return mt;
}