-
Notifications
You must be signed in to change notification settings - Fork 0
/
2DZ_MatrixLNorm.c
138 lines (116 loc) · 3.82 KB
/
2DZ_MatrixLNorm.c
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
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int numberOfRows, numberOfColumns;
double** _Data;
}Matrix;
double get_elem(void* _matr, int row, int col)
{
Matrix* _mat = _matr;
//ïðîâåðêè íà îøèáêó èíäåêñàöèè
if( row > _mat->numberOfRows || col > _mat-> numberOfRows || row < 1 || col < 1 )
{
printf("\nUnable to access [%i+1,%i+1]", row, col);
return 0;
}
else
{
return _mat->_Data[row-1][col-1]; //â ìàòåìàòèêå íóìåðàöèÿ èíäåêñîâ íà÷èíàåòñÿ ñ åäåíèöû;
}
}
void set_elem(void* _matr, int row, int col, double elem)
{
Matrix* _mat = _matr;
//ïðîâåðêè íà îøèáêó èíäåêñàöèè
if( row > _mat->numberOfRows || col > _mat-> numberOfRows || row < 1 || col < 1 )
{
printf("\nUnable to access [%i,%i]", row, col);
return;
}
else
{
_mat->_Data[row-1][col-1] = elem;
}
}
void nullify_matrix(void* _matr)
{
if(_matr == NULL) return;
Matrix* _tempMatrix = _matr;
for(int i = 1; i < _tempMatrix->numberOfRows+1; i++)
{
for(int j = 1; j < _tempMatrix->numberOfColumns+1; j++)
{
set_elem(_tempMatrix, i, j, 0);
}
}
}
Matrix* _readMatrixFromFile(const char* _name)
{
Matrix* _returnMat = malloc(sizeof(Matrix));
FILE *_h;
_h = fopen(_name, "r");
if(_h == NULL)
{
printf("Could not open %s!\n", _name);
return NULL;
}
int numberOfNonzeroElements;
fscanf(_h, "%i %i %i", &_returnMat->numberOfRows, &_returnMat->numberOfColumns, &numberOfNonzeroElements);
_returnMat->_Data = (double**)malloc(_returnMat->numberOfRows * sizeof(double*));
for(int i = 0; i < _returnMat->numberOfRows; i++)
{
_returnMat->_Data[i] = (double*)malloc(_returnMat->numberOfColumns * sizeof(double));
}
nullify_matrix(_returnMat);
for(int i = 0; i < numberOfNonzeroElements; i++)
{
int row, col;
double value;
fscanf(_h, "%i %i %lf", &row, &col, &value);
set_elem(_returnMat, row, col, value);
}
return _returnMat;
}
double Max(double* _numbers, int howMany)
{
double max = 0;
for(int i = 0; i < howMany; i++)
{
if(_numbers[i] > max) max = _numbers[i];
}
return max;
}
double AbsSum(double* _numbers, int howMany)
{
double Sum = 0;
for(int i = 0; i < howMany; i++)
{
if(_numbers[i] < 0) Sum -= _numbers[i];
else Sum += _numbers[i];
}
return Sum;
}
double FindLNorma(void* _matr)
{
if(_matr == NULL) return -1;
Matrix* _tempMatrix = _matr;
int numberOfRows = _tempMatrix->numberOfRows;
double* _sums = malloc(numberOfRows * sizeof(double));
for(int i = 0; i < numberOfRows; i++)
{
_sums[i] = AbsSum(_tempMatrix->_Data[i], _tempMatrix->numberOfColumns);
}
return Max(_sums, numberOfRows);
}
int main(int argc, char *argv[])
{
for(int i = 1; i < argc; i++)
{
Matrix* tempMatr = _readMatrixFromFile(argv[i]);
double tempNorm = FindLNorma(tempMatr);
if(tempNorm != -1) printf("LNorm of matrix %s: %f\n", argv[i], tempNorm);
free(tempMatr);
tempMatr = NULL;
}
}