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
/
cov.c
120 lines (85 loc) · 2.92 KB
/
cov.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
double rowMean(const double *arr, int n) {
double sum = 0;
int i;
for (i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
double fillCovDiffInCell(double row1[], double row2[], double rowMeanArr1, double rowMeanArr2, int columnSpace) {
int i;
double sum = 0;
for (i = 0; i < columnSpace; i++) {
sum = sum + (row1[i] - rowMeanArr1) * (row2[i] - rowMeanArr2);
}
return sum;
}
void covarianceMatrix(double **inputMatrix, int rowSpace, int columnSpace, FILE *outputFile) {
int p;
int j;
int i;
int writeNum=0;
double *rowMeansArray = (double *) malloc(rowSpace * sizeof(double));
double *row = calloc(rowSpace, sizeof(double));
for (i = 0; i < (rowSpace); i++) {
rowMeansArray[i] = rowMean(inputMatrix[i], columnSpace);
}
for (p = 0; p < (rowSpace); p++) {
for (j = 0; j < (rowSpace); j++) {
row[j] = fillCovDiffInCell(inputMatrix[p], inputMatrix[j], rowMeansArray[p],
rowMeansArray[j], columnSpace);
}
writeNum=fwrite(row, sizeof(double), rowSpace, outputFile);
assert(writeNum==rowSpace);
}
free(row);
free(rowMeansArray);
}
int main(int argc, char *argv[]) {
int matrixDimension[2];
int numberOfParameters = 0;
int rowLength = 0;
int columnLength;
double **matrix;
int i;
int matrixRow;
FILE *outputFile;
int writeStatus=0;
int outputMatrixDimension[2] = {0, 0};
time_t start = clock();
FILE *file = fopen(argv[1], "r");
assert(file != NULL);
numberOfParameters = fread(matrixDimension, sizeof(int), 2, file);
assert(numberOfParameters == 2);
rowLength = matrixDimension[1];
columnLength = matrixDimension[0];
matrix = (double **) malloc(rowLength * sizeof(double *));
for (i = 0; i < rowLength; i++) {
matrix[i] = (double *) malloc(columnLength * sizeof(double));
matrixRow = fread(matrix[i], sizeof(double), columnLength, file); /* Filling Matrix[i]*/
assert(matrixRow == columnLength);
}
fclose(file);
/* Write outputMatrix to File*/
outputFile = fopen(argv[2], "w");
assert(outputFile != NULL);
outputMatrixDimension[1] = rowLength;
outputMatrixDimension[0] = rowLength;
writeStatus=fwrite(outputMatrixDimension,sizeof(int),1,outputFile);
assert(writeStatus==1);
writeStatus=fwrite(outputMatrixDimension,sizeof(int),1,outputFile);
assert(writeStatus==1);
/* Function Standardize Given Matrix. Uses Covariance function.*/
covarianceMatrix(matrix, rowLength, columnLength, outputFile);
fclose(outputFile);
for (i = 0; i < rowLength; i++) {
free(matrix[i]);
}
(void) argc;/*we don't want any warnings*/
free(matrix);
printf("%f", ((double) (clock() - start) / CLOCKS_PER_SEC));
return 0;
}