-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
582 lines (503 loc) · 13 KB
/
main.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
/*
* Least Square Approximation Chart
* @author Aliaksei Korshuk
* @version 1.0
* @since 2021-03-04
*/
#include<iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <fstream>
#include <cstring>
#ifdef WIN32
#define GNUPLOT_NAME "D:\\gnuplot\\bin\\gnuplot -persist"
#else
#define GNUPLOT_NAME "gnuplot -persist"
#endif
using namespace std;
class SquareMatrix;
class IdentityMatrix;
class EliminationMatrix;
class PermutationMatrix;
/**
* Matrix class realisation
*/
class Matrix {
public:
/**
* Default constructor
*/
Matrix(){
this->rows = 0;
this->columns = 0;
this->matrix = nullptr;
}
/**
* Constructor with parameters
*
* @param rows size
* @param columns size
* @param matrix rows*columns matrix
*/
Matrix(int rows, int columns, double** matrix){
this->rows = rows;
this->columns = columns;
double** new_matrix = new double*[rows];
for (int x = 0; x < rows; x++){
new_matrix[x] = new double[columns];
for (int y = 0; y < columns; y++){
new_matrix[x][y] = matrix[x][y];
}
}
this->matrix = new_matrix;
}
/**
* Constructor with parameters
*
* @param rows size
* @param columns size
*/
Matrix(int rows, int columns){
this->rows = rows;
this->columns = columns;
double** new_matrix = new double*[rows];
for (int x = 0; x < rows; x++){
new_matrix[x] = new double[columns];
for (int y = 0; y < columns; y++){
new_matrix[x][y] = 0;
}
}
this->matrix = new_matrix;
}
/**
* Overloading operator <<
*
* @param out Ostream
* @param m Matrix
* @return Ostream
*/
friend ostream & operator << (ostream &out, const Matrix &m){
double** matrix = m.matrix;
for (int x = 0; x < m.rows; x++){
out << matrix[x][0];
for (int y = 1; y < m.columns; y++){
out << " " << matrix[x][y];
}
out << endl;
}
return out;
}
/**
* Overloading operator >>
*
* @param in Istream
* @param m Matrix
* @return Istream
*/
friend istream & operator >> (istream &in, Matrix &m){
in >> m.rows >> m.columns;
double** new_matrix = new double*[m.rows];
for (int x = 0; x < m.rows; x++){
new_matrix[x] = new double[m.columns];
for (int y = 0; y < m.columns; y++){
in >> new_matrix[x][y];
}
}
m.matrix = new_matrix;
return in;
}
/**
* Overloading operator +
*
* @param b Matrix term of addition
* @return Resulting matrix of the operation
*/
Matrix operator+ (const Matrix& b){
if (rows == b.rows && columns == b.columns){
Matrix temp(rows, columns, matrix);
for (int x = 0; x < rows; x++){
for (int y = 0; y < columns; y++){
temp.matrix[x][y]+=b.matrix[x][y];
}
}
return temp;
}
Matrix temp;
cout << "Error: the dimensional problem occurred" << endl;
return temp;
}
/**
* Overloading operator -
*
* @param b Subtrahend Matrix
* @return Resulting matrix of the operation
*/
Matrix operator- (const Matrix& b){
if (rows == b.rows && columns == b.columns){
Matrix temp(rows, columns, matrix);
for (int x = 0; x < rows; x++){
for (int y = 0; y < columns; y++){
temp.matrix[x][y]-=b.matrix[x][y];
}
}
return temp;
}
Matrix temp;
cout << "Error: the dimensional problem occurred" << endl;
return temp;
}
/**
* Overloading operator *
*
* @param b Factor Matrix
* @return Resulting matrix of the operation
*/
Matrix operator* (const Matrix& b){
if (columns == b.rows){
Matrix temp(rows, b.columns);
for (int x = 0; x < rows; x++){
for (int y = 0; y < b.columns; y++){
for(int z=0; z<columns; z++) {
temp.matrix[x][y]+=matrix[x][z]*b.matrix[z][y];
}
}
}
return temp;
}
Matrix temp;
cout << "Error: the dimensional problem occurred" << endl;
return temp;
}
/**
* Calculating transpose matrix
*
* @return Resulting matrix of the operation
*/
Matrix transpose()
{
Matrix temp(columns, rows);
for (int i = 0; i < columns; i++)
for (int j = 0; j < rows; j++)
temp.matrix[i][j] = matrix[j][i];
return temp;
}
public:
int rows; // Amount of rows
int columns; // Amount of columns
double** matrix; // Matrix of size rows*columns
};
/**
* SquareMatrix class realisation
*/
class SquareMatrix:public Matrix {
public:
/**
* Default constructor
*/
SquareMatrix() : Matrix(){
}
/**
* Constructor with parameters
*
* @param rows size
* @param matrix matrix size*size
*/
SquareMatrix(int size, double** matrix) : Matrix(size,size, matrix){
}
/**
* Constructor with parameters
*
* @param size size
*/
SquareMatrix(int size): Matrix(){
this->rows = size;
this->columns = size;
double** new_matrix = new double*[size];
for (int x = 0; x < size; x++){
new_matrix[x] = new double[size];
for (int y = 0; y < size; y++){
new_matrix[x][y] = 0;
}
}
this->matrix = new_matrix;
}
/**
* Overloading operator <<
*
* @param out Ostream
* @param m Matrix
* @return Ostream
*/
friend ostream & operator << (ostream &out, const SquareMatrix &m){
double** matrix = m.matrix;
for (int x = 0; x < m.rows; x++){
out << matrix[x][0];
for (int y = 1; y < m.rows; y++){
out << " " << matrix[x][y];
}
out << endl;
}
return out;
}
/**
* Overloading operator >>
*
* @param in Istream
* @param m Matrix
* @return Istream
*/
friend istream & operator >> (istream &in, SquareMatrix &m){
in >> m.rows;
m.columns = m.rows;
double** new_matrix = new double*[m.rows];
for (int x = 0; x < m.rows; x++){
new_matrix[x] = new double[m.rows];
for (int y = 0; y < m.rows; y++){
in >> new_matrix[x][y];
}
}
m.matrix = new_matrix;
return in;
}
/**
* Overloading operator +
*
* @param b Matrix term of addition
* @return Resulting matrix of the operation
*/
SquareMatrix operator+ (const SquareMatrix& b){
Matrix newMatrix = (Matrix)*this + (Matrix)b;
return *(SquareMatrix*)(&newMatrix);
}
/**
* Overloading operator -
*
* @param b Subtrahend Matrix
* @return Resulting matrix of the operation
*/
SquareMatrix operator- (const SquareMatrix& b){
Matrix newMatrix = (Matrix)*this - (Matrix)b;
return *(SquareMatrix*)(&newMatrix);
}
/**
* Overloading operator *
*
* @param b Factor Matrix
* @return Resulting matrix of the operation
*/
SquareMatrix operator* (const SquareMatrix& b){
Matrix newMatrix = (Matrix)*this * (Matrix)b;
return *(SquareMatrix*)(&newMatrix);
}
/**
* Calculating transpose matrix
*
* @return Resulting matrix of the operation
*/
SquareMatrix transpose()
{
Matrix newMatrix = (Matrix)*this;
newMatrix = newMatrix.transpose();
return *(SquareMatrix*)(&newMatrix);
}
};
/**
* IdentityMatrix class realisation
*/
class IdentityMatrix : public SquareMatrix{
public:
/**
* Constructor with parameters
*
* @param size Size of matrix
*/
IdentityMatrix(int size): SquareMatrix(size){
for (int x = 0; x < size; x++){
this->matrix[x][x] = 1;
}
}
};
/**
* EliminationMatrix class realisation
*/
class EliminationMatrix : public IdentityMatrix{
public:
/**
* Constructor with parameters
*
* @param x Position
* @param y Position
* @param matrix Input matrix
*/
EliminationMatrix(int x, int y, SquareMatrix matrix): IdentityMatrix(matrix.rows){
double value;
double a = matrix.matrix[x - 1][y - 1];
double k = matrix.matrix[y - 1][y - 1];
value = -1 * a / k;
this->matrix[x - 1][y - 1] = value;
}
};
/**
* PermutationMatrix class realisation
*/
class PermutationMatrix : public IdentityMatrix{
public:
/**
* Constructor with parameters
*
* @param x Position
* @param y Position
* @param matrix Input matrix
*/
PermutationMatrix(int x, int y, SquareMatrix matrix): IdentityMatrix(matrix.rows){
double* temp;
temp = this->matrix[x-1];
this->matrix[x-1] = this->matrix[y - 1];
this->matrix[y - 1] = temp;
}
};
/**
* Method that swaps rows by index
* @param x Index
* @param y Index
* @param A SquareMatrix
*/
void swap( int x, int y, SquareMatrix A)
{
double* temp;
temp = A.matrix[x];
A.matrix[x] = A.matrix[y];
A.matrix[y] = temp;
}
/**
* Calculates the determinant after Gaussian elimination
*
* @param A SquareMatrix
* @return determinant
*/
double determinant(SquareMatrix A){
double answer = 1;
for (int i = 0; i < A.rows; i++) answer*=A.matrix[i][i];
return answer;
}
/**
* Method that calculates Inverse Matrix using Gaussian elimination
* @param A SquareMatrix
*/
SquareMatrix inverseMatrix(SquareMatrix A){
SquareMatrix I = IdentityMatrix(A.rows);
int count = 1;
for (int k=0; k<A.rows; k++)
{
int i_max = k;
double v_max = abs(A.matrix[i_max][k]);
for (int i = A.rows-1; i > k; i--)
if (abs(A.matrix[i][k]) > v_max)
v_max = A.matrix[i][k], i_max = i;
if (i_max != k){
swap(k, i_max, A);
swap(k, i_max, I);
count++;
}
for (int i=k+1; i<A.rows; i++)
{
if (A.matrix[i][k] != 0){
EliminationMatrix E(i + 1, k + 1, A);
A = E*A;
I = E*I;
count++;
}
}
}
for (int k=A.rows-1; k>0; k--)
{
for (int i=A.rows-2 - (A.rows - 1 - k); i>=0; i--)
{
if (A.matrix[i][k] != 0){
EliminationMatrix E(i + 1, k + 1, A);
A = E*A;
I = E*I;
count++;
}
}
}
for (int i = 0; i < A.rows; i++){
double temp = 1/A.matrix[i][i];
for (int k = 0; k < I.rows; k++){
I.matrix[i][k]*=temp;
}
A.matrix[i][i] = 1;
}
return I;
}
/**
* Method that solves Least Square Approximation
*
* @param A Matrix
* @param b Matrix
*/
Matrix leastSquareApproximation(Matrix A, Matrix b){
cout << "A:\n" << A;
cout << "A_T*A:\n" << A.transpose()*A;
Matrix temp = A.transpose()*A;
Matrix a_new = inverseMatrix(*(SquareMatrix*)(&temp));
cout << "(A_T*A)^-1:\n" << a_new;
Matrix b_new = A.transpose()*b;
cout << "A_T*b:\n" << b_new;
cout << "x~:\n";
return a_new*b_new;
}
// Driver Code
int main() {
cout << fixed << setprecision(4);
srand(time(0));
int size;
size = 20;
double* t = new double[size];
Matrix b(size, 1);
for (int i = 0; i < size; i++){
t[i] = i+1;
b.matrix[i][0] = rand() % 4 + i;
}
int degree;
degree = 10;
Matrix A(size, degree + 1);
for (int i = 0; i < size; i++){
for (int k = 0; k <= degree; k++){
A.matrix[i][k] = pow(t[i], k);
}
}
Matrix X = leastSquareApproximation(A, b);
cout << X;
FILE* pipe = _popen(GNUPLOT_NAME, "w");
string func;
for (int i = 0; i <= degree; i++){
stringstream ss;
ss << X.matrix[i][0] ;
string s;
ss >> s;
func+= s;
func+= "*x**";
stringstream ss2;
ss2 << i ;
string s2;
ss2 >> s2;
func+= s2;
func+=" + ";
}
func+="0";
cout << func << endl;
int n = func.length();
char char_array[n + 1];
strcpy(char_array, func.c_str());
fprintf(pipe, "set yrange [0:%d]\n", size + 6);
fprintf(pipe, "f(x) = %s\n", char_array);
fprintf(pipe, "plot [0:%d] f(x) title 'appr', '-' using 1:2 title 'exp' with points pointtype 5 pointsize 1\n", size);
for (int i = 0; i < size; i++){
fprintf(pipe, "%f %f\n", t[i], b.matrix[i][0]);
}
fprintf(pipe, "%s\n", "e");
_pclose(pipe);
return 0;
}