This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigen.c
182 lines (144 loc) · 4.99 KB
/
eigen.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
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
FILE *openInputFile(char *const *argv) {
FILE *input = fopen(argv[1], "r");
assert(input != NULL);
return input;
}
void getDimension(FILE *input, int *dimension) {
int readStatus;
readStatus = fread(dimension, sizeof(int), 2, input);
assert(readStatus == 2);
}
double *getVector(int size) {
double *vector = (double *) calloc(size, sizeof(double));
const double *end = vector + size;
double *current = vector;
for (; current != end; current++)
*current = rand();
return vector;
}
void readline(FILE *input, double *currentRow, int size) {
int n = fread(currentRow, sizeof(double), size, input);
assert(n == size);
}
void swap(double **a, double **b) {
double *c = *a;
*a = *b;
*b = c;
}
double max(double a, double b) {
if (a > b)
return a;
return b;
}
double dotProduct_(double *currentRow, double *vector, int size) {
double *currentRowPointer = currentRow;
double *currentCellInVector = vector;
double *currentRowEnd = currentRow + size;
double sum = 0;
for (; currentRowPointer != currentRowEnd; currentRowPointer++, currentCellInVector++) {
sum += *currentRowPointer * (*currentCellInVector);
}
return sum;
}
double calcVectorDivisor(double *vector, int size) {
double *vectorEnd = vector + size;
double *vectorPointer = vector;
double sum = 0;
for (; vectorPointer != vectorEnd; vectorPointer++) {
sum += vectorPointer[0]*vectorPointer[0];
}
return sqrt(sum);
}
void normalizeVector(double *newVectorPointer, int vectorSize) {
double vectorDivisor = 1;
double *currentNewVectorPointer = newVectorPointer;
double *NewVectorEnd = newVectorPointer + vectorSize;
vectorDivisor = calcVectorDivisor(newVectorPointer, vectorSize);
for (; currentNewVectorPointer != NewVectorEnd; currentNewVectorPointer++) {
*currentNewVectorPointer /= vectorDivisor;
}
}
double scanColumnAndWriteToNewVector(FILE *input, double *vector, double *newVector, int vectorSize,
double *currentRow) {
/*@todo problem with reading from file probably*/
double largestDiff = 0;
double *oldVectorEnd = vector + vectorSize;
double *oldVectorPointer = vector;
double *newVectorPointer = newVector;
for (; oldVectorPointer != oldVectorEnd; oldVectorPointer++, newVectorPointer++) {
double cellNewValue;
readline(input, currentRow, vectorSize);
cellNewValue = dotProduct_(currentRow, vector, vectorSize);
*newVectorPointer = cellNewValue;
}
normalizeVector(newVector, vectorSize);
for(oldVectorPointer=vector,newVectorPointer=newVector;oldVectorPointer != oldVectorEnd;oldVectorPointer++, newVectorPointer++)
largestDiff = max(fabs(*oldVectorPointer - *newVectorPointer), largestDiff);
return largestDiff;
}
void resetLine( FILE *input) {
int readStatus;
int dontcare[2];
rewind(input);
readStatus = fread(dontcare, sizeof(int), 2, input);
assert(readStatus == 2);
}
double *iterateVector(FILE *input, double epsilon, double *vector, int vectorSize) {
double *newVector = (double *) calloc(vectorSize, sizeof(double));
double *currentRow = calloc(vectorSize,
sizeof(double));/* it's true that it should be inside scan, but it's better perf wise*/
double largestDiff;
do {
largestDiff = scanColumnAndWriteToNewVector(input, vector, newVector, vectorSize, currentRow);
swap(&newVector, &vector);
resetLine(input);
} while (largestDiff > epsilon);
swap(&newVector, &vector);
free(currentRow);
free(vector);
return newVector;
}
FILE *openOutputFile(char **argv) {
FILE *outputFile = fopen(argv[2], "w");
assert(outputFile != NULL);
return outputFile;
}
void writeToFile(FILE *output, const int *dimension, const double *bk) {
int numOfWrites = 0;
int matrixSize = dimension[0] * dimension[1];
numOfWrites = fwrite(dimension, sizeof(int), 2, output);
assert(numOfWrites == 2);
numOfWrites = fwrite(bk, sizeof(double), matrixSize, output);
assert(numOfWrites == matrixSize);
}
int main(int argc, char *argv[]) {
clock_t start;
FILE *input, *output;
int matrixDimension[2];
const double epsilon = 0.00001;
double *b0;/* iteration vector*/
double *bk;
int vectorDimension[2]={0,0};
assert(argc == 3);
srand(time(NULL));
start=clock();
input = openInputFile(argv);
getDimension(input, matrixDimension);
vectorDimension[1]=matrixDimension[0];
vectorDimension[0]=1;
b0 = getVector(matrixDimension[0]);
bk = iterateVector(input, epsilon, b0, matrixDimension[0]);
/* @todo write the output file*/
output = openOutputFile(argv);
writeToFile(output, vectorDimension, bk);
free(bk);
fclose(input);
fclose(output);
printf("%f ",((double)(clock()-start) / CLOCKS_PER_SEC));
return 0;
}