-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeterminant.cpp
220 lines (203 loc) · 4.41 KB
/
Determinant.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
#include "Determinant.h"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
#define eps 1e-6
//默认构造函数
Determinant::Determinant() = default;
//构造函数(深拷贝)
Determinant::Determinant(int order, double** nData) {
this->order = order;
data = new double* [order];
for (int i = 0; i < order; i++)
{
data[i] = new double[order];
for (int j = 0; j < order; j++)
{
data[i][j] = nData[i][j];
}
}
}
//构造函数(手动输入)
Determinant::Determinant(int order) :order(order) {
data = new double* [order];
cout << "请输入行列式:" << endl;
for (int i = 0; i < order; i++)
{
data[i] = new double[order];
for (int j = 0; j < order; j++)
{
cin >> data[i][j];
}
}
}
//复制构造函数(深拷贝)
Determinant::Determinant(const Determinant& obj) {
order = obj.order;
data = new double* [order];
for (int i = 0; i < order; i++)
{
data[i] = new double[order];
for (int j = 0; j < order; j++)
{
data[i][j] = obj.data[i][j];
}
}
}
//赋值运算符(深拷贝)
Determinant& Determinant::operator=(const Determinant& other)
{
order = other.order;
data = new double* [order];
for (int i = 0; i < order; i++)
{
data[i] = new double[order];
for (int j = 0; j < order; j++)
{
data[i][j] = other.data[i][j];
}
}
return *this;
}
//析构函数
Determinant::~Determinant() {
delete[] data;
}
//递归函数,递归计算代数余子式
double Determinant::cal(double** det, int n)//det-行列式,n:行列式的阶数
{
double detVal = 0;//行列式的值
if (n == 1)//递归终止条件
return det[0][0];
double** tempdet = new double* [n - 1];//用来存储余相应的余子式
for (int i = 0; i < n - 1; i++)
tempdet[i] = new double[n - 1];
for (int i = 0; i < n; i++)//第一重循环,行列式按第一行展开
{
for (int j = 0; j < n - 1; j++)
for (int k = 0; k < n - 1; k++)
{
if (k < i)
tempdet[j][k] = det[j + 1][k];
else
tempdet[j][k] = det[j + 1][k + 1];
}
detVal += det[0][i] * pow(-1.0, i) * cal(tempdet, n - 1);
}
return detVal;
}
//打印行列式
void Determinant::printDet() {
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++) {
cout << setw(12) << data[i][j];
}
cout << endl;
}
}
//计算行列式的值
double Determinant::calculate() {
return cal(data, order);
}
//行列式行倍加变换
void Determinant::rowMultiplyScalar(int r1, double k, int r2) {
//将r1倍乘k加到r2上
for (int i = 0; i < order; i++)
{
data[r2][i] += data[r1][i] * k;
}
}
//判断一个浮点数是否为0
bool isZero(double x) {
if (fabs(x) <= eps)
{
return true;
}
else {
return false;
}
}
//转换为上三角形式
Determinant Determinant::upperTriangle() {
Determinant tmp(*this);
for (int cnt = 0; cnt < order; cnt++)
{
if (isZero(tmp.data[cnt][cnt]))
{
//首行元素为0
bool isAllZero = true;
for (int i = cnt + 1; i < order; i++)
{
if (!isZero(tmp.data[i][cnt])) {
isAllZero = false;
//交换第i行与首行(cnt行)
for (int j = 0; j < order; j++)
{
double buffer = tmp.data[cnt][j];
tmp.data[cnt][j] = tmp.data[i][j];
//两行交换之后,行列式前有一个负号,将负号提到第i行
tmp.data[i][j] = -buffer;
break;
}
}
}
if (isAllZero)
{
continue;
}
}
//将首列除左上角外全部置0
for (int i = cnt + 1; i < order; i++)
{
tmp.rowMultiplyScalar(cnt, -(tmp.data[i][cnt] / tmp.data[cnt][cnt]), i);
}
}
return tmp;
}
//行列式的魔法输入
Determinant DeterminantCin() {
vector<double> container;//临时存储输入
int order = 0; //阶数
double tmp;
//读入第一行数据
cout << "请输入任意行列式:(最后一行输入e终止)" << endl;
do {
cin >> tmp;
container.push_back(tmp);
order++;
} while (cin.get() != '\n');
//列数已经确定,读入后续行
string firstChar;
while (1) {
cin >> firstChar;
if (firstChar == "e")
{
break;
}
else {
//如果firstChat不为e,则读入一行
container.push_back(atof(firstChar.data()));
//此时,下一个读入应该是空格
while (cin.get() != '\n') {
cin >> tmp;
container.push_back(tmp);
}
}
}
int row = container.size() / order;
int cnt = 0;
double** nData = new double* [row];
for (int i = 0; i < row; i++)
{
nData[i] = new double[order];
for (int j = 0; j < order; j++)
{
nData[i][j] = container.at(cnt);
cnt++;
}
}
return Determinant(order, nData);
}