-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
274 lines (234 loc) · 8.96 KB
/
Copy pathmainwindow.cpp
File metadata and controls
274 lines (234 loc) · 8.96 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
matrix.resize(3, QVector<double>(4));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_calculationButton_clicked()
{
matrix.clear();
solution.clear();
// Validate input fields
for (int row = 0; row < 2; ++row) {
for (int col = 0; col < 3; ++col) {
QString valueStr = ui->matrixInputTable->item(row, col)->text();
if (valueStr.isEmpty()) {
QMessageBox::critical(this, "Error", "Please fill in all input fields.");
return;
}
}
}
// Read the matrix values from the input fields
for (int row = 0; row < 3; ++row) {
QVector<double> rowData;
for (int col = 0; col < 4; ++col) {
QString valueStr = ui->matrixInputTable->item(row, col)->text();
bool ok;
double value = valueStr.toDouble(&ok);
if (!ok) {
QMessageBox::critical(this, "Error", "Invalid input at row " + QString::number(row + 1) +
" column " + QString::number(col + 1)) + ", plese use digits";
return;
}
rowData.append(value);
}
matrix.append(rowData);
}
gaussJordanElimination();
displaySolution();
}
/*
void MainWindow::on_calculationButton_clicked()
{
matrix.clear();
solution.clear();
// Read the matrix values from the input fields
for (int row = 0; row < 2; ++row) { // Update the loop condition to 2 instead of 3
QVector<double> rowData;
for (int col = 0; col < 3; ++col) { // Update the loop condition to 3 instead of 4
QString valueStr = ui->matrixInputTable->item(row, col)->text();
bool ok;
double value = valueStr.toDouble(&ok);
if (!ok || valueStr.isEmpty()) {
QMessageBox::critical(this, "Error", "Invalid input at row " + QString::number(row + 1) +
" column " + QString::number(col + 1));
return;
}
rowData.append(value);
}
matrix.append(rowData);
}
// Add an extra column for the augmented part with zeros
for (int row = 0; row < 2; ++row) {
matrix[row].append(0);
}
gaussJordanElimination();
displaySolution();
}
*/
void MainWindow::gaussJordanElimination()
{
int rowCount = matrix.size();
int colCount = matrix[0].size();
for (int pivot = 0; pivot < rowCount; ++pivot) {
// Find the row with the largest pivot element
int maxRow = pivot;
for (int row = pivot + 1; row < rowCount; ++row) {
if (qAbs(matrix[row][pivot]) > qAbs(matrix[maxRow][pivot]))
maxRow = row;
}
// Swap the pivot row with the row with the largest pivot element
if (maxRow != pivot) {
for (int col = 0; col < colCount; ++col) {
double temp = matrix[pivot][col];
matrix[pivot][col] = matrix[maxRow][col];
matrix[maxRow][col] = temp;
}
}
// Normalize the pivot row
double pivotValue = matrix[pivot][pivot];
if(qFuzzyIsNull(pivotValue)) {
//QMessageBox::critical(this, "Error", "Pivot value has been eliminated, unable to compute Gauss-Jordan elimination.");
return;
}
for (int col = pivot; col < colCount; ++col)
matrix[pivot][col] /= pivotValue;
// Eliminate other rows
for (int row = 0; row < rowCount; ++row) {
if (row != pivot) {
double factor = matrix[row][pivot];
for (int col = pivot; col < colCount; ++col)
matrix[row][col] -= factor * matrix[pivot][col];
}
}
}
}
int MainWindow::calculateMatrixRank(const QVector<QVector<double>>& matrix)
{
int rank = 0;
// check for all zero row
for(const QVector<double>& rowData : matrix) {
bool rowAllZero = true;
for (double value : rowData){
if (!qFuzzyIsNull(value)){
rowAllZero = false;
break;
}
}
if (!rowAllZero) rank++;
}
return rank;
}
void MainWindow::displaySolution()
{
// Check the rank of the matrix
int rank = calculateMatrixRank(matrix);
// Extract the solution from the matrix
solution.clear();
for (const QVector<double>& rowData : matrix)
solution.append(rowData.last());
// Check for inconsistent system
bool inconsistent = false;
for (int row = 0; row < matrix.size(); ++row) {
bool allCoefficientsZero = true;
for (int col = 0; col < matrix[row].size() - 1; ++col) {
if (!qFuzzyIsNull(matrix[row][col])) {
allCoefficientsZero = false;
break;
}
}
if (allCoefficientsZero && !qFuzzyIsNull(matrix[row].last())) {
inconsistent = true;
break;
}
}
QString solutionString;
QStringList variableNames = {"x", "y", "z"};
// Display the solution or error message
if (!inconsistent) {
if (rank == 0) {
QMessageBox::critical(this, "Error", "The matrix is of rank 0, and no solution exists.");
for (int i = 0; i < solution.size(); ++i) {
solutionString += variableNames[i] + " = NaN \n";
}
} else if (rank == 1 || rank == 2) {
QMessageBox::critical(this, "Error", "The matrix is of rank " + QString::number(rank) +
", and infinite solutions exist.");
for (int i = 0; i < solution.size(); ++i) {
solutionString += variableNames[i] + " = NaN \n";
}
} else {
for (int i = 0; i < solution.size(); ++i) {
double value = solution[i];
QString valueString;
if (qAbs(value) >= 1e15 || qAbs(value) < 1e-15) {
valueString = QString::number(value, 'e', 5);
} else {
valueString = QString::number(value, 'g', 15);
}
solutionString += variableNames[i] + " = " + valueString + "\n";
}
}
ui->solutionLabel->setText(solutionString);
ui->rankLabel->setText("System Rank: " + QString::number(rank));
} else {
QMessageBox::critical(this, "Error", "The system of equations is inconsistent. No valid solution exists.");
for (int i = 0; i < solution.size(); ++i) {
solutionString += variableNames[i] + " = NaN \n";
}
ui->solutionLabel->setText(solutionString);
ui->rankLabel->setText("System inconsistent");
}
}
/*void MainWindow::displaySolution()
{
// Check the rank of the matrix
int rank = calculateMatrixRank(matrix);
// Extract the solution from the matrix
solution.clear();
for (const QVector<double>& rowData : matrix)
solution.append(rowData.last());
// Check for inconsistent system
bool inconsistent = false;
for (int row = 0; row < matrix.size(); ++row) {
bool allCoefficientsZero = true;
for (int col = 0; col < matrix[row].size() - 1; ++col) {
if (!qFuzzyIsNull(matrix[row][col])) {
allCoefficientsZero = false;
break;
}
}
if (allCoefficientsZero && !qFuzzyIsNull(matrix[row].last())) {
inconsistent = true;
break;
}
}
QString solutionString;
QStringList variableNames = {"x", "y", "z"};
// Display the solution or error message
if (inconsistent) {
QMessageBox::critical(this, "Error", "The system of equations is inconsistent. No valid solution exists.");
ui->rankLabel->setText("System inconsistent");
} else {
if (rank == 0) {
QMessageBox::critical(this, "Error", "The matrix is of rank 0, and no solution exists.");
} else if (rank == 1 || rank == 2) {
QMessageBox::critical(this, "Error", "The matrix is of rank " + QString::number(rank) +
", and infinite solutions exist.");
} else {
for (int i = 0; i < solution.size(); ++i) {
solutionString += variableNames[i] + " = " + (qFuzzyIsNull(solution[i]) ? "NaN" : QString::number(solution[i])) + "\n";
}
}
ui->solutionLabel->setText(solutionString);
ui->rankLabel->setText("System Rank: " + QString::number(rank));
}
}*/