-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixHelpers.cpp
79 lines (68 loc) · 1.96 KB
/
matrixHelpers.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
/*
* File: matrixHelpers.cpp
* Author: zhakov
*
* Created on 5 Декабрь 2012 г., 22:48
*/
#include "matrixHelpers.h"
#include "stdio.h"
#include "math.h"
matrixHelpers::matrixHelpers() {
}
matrixHelpers::matrixHelpers(const matrixHelpers& orig) {
}
matrixHelpers::~matrixHelpers() {
}
int matrixHelpers::printVector(double * matrix, int size) {
for (int i = 0; i < size; i++) {
printf("%.4f ", matrix[i]);
}
printf("\n");
}
int matrixHelpers::printMatrix(double **matrix, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%.4f ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
/**
* Функция проверяет результат:
* умножает матрицу на вектор и сравнивает с результатом
*
* @param pMatrix
* @param pVector
* @param pResult
* @param Size
* @return
*/
int matrixHelpers::testSolvingResult(double** pMatrix, double* pVector, double* pResult, int Size) {
/* Buffer for storing the vector, that is a result of multiplication
of the linear system matrix by the vector of unknowns */
double* pRightPartVector;
// Flag, that shows wheather the right parts vectors are identical or not
int equal = 0;
double Accuracy = 1.e-4; // Comparison accuracy
pRightPartVector = new double [Size];
for (int i = 0; i < Size; i++) {
pRightPartVector[i] = 0;
for (int j = 0; j < Size; j++) {
pRightPartVector[i] += pMatrix[i][j] * pResult[j];
}
}
for (int i = 0; i < Size; i++) {
if (fabs(pRightPartVector[i] - pVector[i]) > Accuracy) {
equal = 1;
}
}
if (equal == 1) {
printf("The result of the algorithm is NOT correct."
"Check your code.\n");
} else {
printf("The result of the algorithm is correct.\n");
}
delete [] pRightPartVector;
return 0;
}