forked from sweetcoffee520/level-adjustment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
288 lines (286 loc) · 4.95 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "Matrix.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
Matrix::Matrix()
{
rowNum = 0;
colNum = 0;
item = NULL;
}
//零矩阵
Matrix::Matrix(int m, int n)
{
if (m < 0 || n < 0)
{
cout << "矩阵大小不能为负\n";
return;
}
rowNum = m;
colNum = n;
item = new double[m*n];
for (int i = 0; i < m*n; i++)
{
item[i] = 0;
}
}
//也可用二维数组初始化
Matrix::Matrix(double* items, int m, int n)
{
rowNum = m;
colNum = n;
item = new double[m*n];
for (int i = 0; i < colNum*rowNum; i++)
{
item[i] = items[i];
}
}
//单位矩阵
Matrix::Matrix(int n)
{
rowNum = colNum = n;
item = new double[n*n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
set(i, j, 1);
else
set(i, j, 0);
}
}
}
Matrix::Matrix(const Matrix &M)
{
colNum = M.colNum;
rowNum = M.rowNum;
//这里不能对指针直接赋值,复制对求逆、转置等操作会影响原矩阵
item = new double[colNum*rowNum];
for (int i = 0; i < colNum*rowNum; i++)
{
item[i] = M.item[i];
}
}
Matrix& Matrix::operator=(const Matrix &M)
{
colNum = M.colNum;
rowNum = M.rowNum;
if (item != NULL) delete[] item;
item = new double[colNum*rowNum];
for (int i = 0; i < colNum*rowNum; i++)
{
this->item[i] = M.item[i];
}
return *this;
}
Matrix::~Matrix()
{
delete[] item;
}
double Matrix::get(int i, int j) const
{
return item[i*colNum + j];
}
void Matrix::set(int i, int j, double value)
{
item[i*colNum + j] = value;
}
void Matrix::RowSwap(int i, int j, double multiply)
{
if (j == -1)
{
for (int k = 0; k < colNum; k++)
{
set(i, k, multiply*get(i, k));
}
}
else
{
for (int k = 0; k < colNum; k++)
{
set(j, k, multiply*get(i, k) + get(j, k));
}
}
}
void Matrix::RowSwap(int i, int j)
{
Matrix _copy = *this;
for (int k = 0; k < colNum; k++)
{
double swap = _copy.get(j, k);
set(j, k, _copy.get(i, k));
set(i, k, swap);
}
}
Matrix Matrix::Trans() const
{
Matrix _copy (this->colNum,this->rowNum);
for (int i = 0; i <this->rowNum; i++)
{
for (int j = 0; j <this->colNum; j++)
{
_copy.set(j, i, this->get(i, j));
}
}
//_copy.rowNum = this->colNum;
//_copy.colNum = this->rowNum;
return _copy;
}
int Matrix::getRowNum() const
{
return rowNum;
}
int Matrix::getColNum() const
{
return colNum;
}
ostream& operator <<(ostream &os, const Matrix &m)
{
for (int i = 0; i < m.rowNum; i++)
{
for (int j = 0; j < m.colNum; j++)
{
os <<std::right<<setw(7)<<setprecision(4)<< m.get(i, j);
if (j == m.colNum - 1) os << "\n";
}
}
return os;
}
Matrix Matrix::operator*(const double f)
{
Matrix _copy = *this;
for (int i = 0; i <rowNum ; i++)
{
for (int j = 0; j < colNum; j++)
{
_copy.set(i, j, get(i, j)*f);
}
}
return _copy;
}
Matrix Matrix::operator-()
{
Matrix _copy = *this;
for (int i = 0;i < colNum*rowNum;i++)
_copy.item[i] = -item[i];
return _copy;
}
Matrix operator*(const double f,const Matrix & m)
{
Matrix _copy = m;
for (int i = 0; i < m.getRowNum(); i++)
{
for (int j = 0; j < m.getColNum(); j++)
{
_copy.set(i, j, m.get(i, j)*f);
}
}
return _copy;
}
Matrix Matrix::operator +(const Matrix &m)
{
if (m.colNum != colNum || m.rowNum != rowNum) return *this;
Matrix _copy = *this;
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
_copy.set(i, j, get(i, j) + m.get(i, j));
}
}
return _copy;
}
Matrix Matrix::operator -(const Matrix &m)
{
if (m.colNum != colNum || m.rowNum != rowNum) return *this;
Matrix _copy = *this;
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
_copy.set(i, j, get(i, j) - m.get(i, j));
}
}
return _copy;
}
Matrix Matrix::operator *(const Matrix &m)
{
if (colNum != m.rowNum)
{
cout << "无法相乘!";
return *this;
}
Matrix _copy(this->rowNum, m.colNum);
for (int i = 0; i < this->rowNum; i++)
{
for (int j = 0; j < m.colNum; j++)
{
double sum = 0;
for (int k = 0; k < m.rowNum; k++)
{
sum += this->get(i, k)*m.get(k, j);
}
_copy.set(i, j, sum);
}
}
return _copy;
}
double Matrix::operator /(const double f)
{
double _copy=0;
if (rowNum == 1 && colNum == 1)
{
_copy = item[0] / f;
return _copy;
}
else cout << "矩阵行数和列数不为一"<<endl;
return _copy;
}
Matrix Matrix::Inverse()
{
Matrix _copy = *this;
Matrix result(colNum);
Matrix error(colNum,colNum);
if (colNum != rowNum)
{
cout << "矩阵不可逆!" << endl;
return *this;
}
for (int i = 0; i < colNum; i++)
{
int MaxRow = i;
double max = fabs(_copy.get(i, i));
for (int j = i; j < rowNum; j++)
{
if (fabs(_copy.get(j, i)) > max)
{
max = fabs(_copy.get(j, i));
MaxRow = j;
}
}
if (max == 0)
{
cout << "不是满秩矩阵";
return error;
}
if (MaxRow != i)
{
result.RowSwap(i, MaxRow);
_copy.RowSwap(i, MaxRow);
}
double r = 1.0 / _copy.get(i, i);
//单位化
_copy.RowSwap(i, -1, r);
result.RowSwap(i, -1, r);
for (int j = 0; j < rowNum; j++)
{
if (j == i) continue;
r = -_copy.get(j, i);
_copy.RowSwap(i, j, r);
result.RowSwap(i, j, r);
}
}
return result;
}