-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixLib.c
267 lines (215 loc) · 8.37 KB
/
matrixLib.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
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
/**
* @file matrixLib.c
* @description Some vector and matrix functions.
* @assignment Project 1
* @date 28.11.2022
* @author Mehmetcan Bozkuş - mehmetcan.bozkus@stu.fsm.edu.tr
*/
#include <stdio.h> // for printf
#include <stdlib.h> // for malloc and free
#include <math.h> // for sqrt()
#include <time.h> // for random seed
#include "matrixLib.h" // for function prototypes
float *returnVector(int size) {
float *vector = (float *) malloc(size * sizeof(float)); // allocate memory for the vector
if (vector == NULL) // if the memory allocation fails
{
printf("Memory allocation failed!\n");
exit(1);
}
return vector;
}
float **returnMatrix(int row, int col) {
float **matrix = (float **) malloc(row * sizeof(float *)); // allocate memory for the matrix rows
for (int i = 0; i < row; i++) {
matrix[i] = (float *) malloc(col * sizeof(float)); // allocate memory for the matrix columns
}
if (matrix == NULL || matrix[0] == NULL) // memory allocation failed
{
printf("Memory allocation failed!\n");
exit(1);
}
return matrix;
}
void freeMatrix(float **mat, int row) {
for (int i = 0; i < row; i++) {
free(mat[i]); // free the memory of the matrix rows
}
free(mat); // free the memory of the matrix
}
float mean(float *vec, int size) {
float sum = 0;
for (int i = 0; i < size; i++) {
sum += vec[i]; // sum the elements of the vector
}
return sum / size; // return the mean of the vector
}
float correlation(float *vec, float *vec2, int size) {
float cov = covariance(vec, vec2, size); // calculate the covariance of the vectors
float mean1 = mean(vec, size); // calculate the mean of the first vector
float mean2 = mean(vec2, size); // calculate the mean of the second vector
float std1 = 0; // standard deviation of the first vector
float std2 = 0; // standard deviation of the second vector
for (int i = 0; i < size; i++) {
std1 += pow(vec[i] - mean1, 2); // sum the squared differences of the first vector
std2 += pow(vec2[i] - mean2, 2); // sum the squared differences of the second vector
}
std1 = sqrt(std1 / (size - 1)); // calculate the standard deviation of the first vector
std2 = sqrt(std2 / (size - 1)); // calculate the standard deviation of the second vector
return cov / (std1 * std2); // return the correlation of the vectors
}
float covariance(float *vec1, float *vec2, int size) {
float mean1 = mean(vec1, size); // calculate the mean of the first vector
float mean2 = mean(vec2, size); // calculate the mean of the second vector
float sum = 0;
for (int i = 0; i < size; i++) {
sum += (vec1[i] - mean1) * (vec2[i] - mean2); // sum the differences of the vectors
}
return sum / (size - 1); // return the covariance of the vectors
}
float **matrixMultiplication(float **mat1, float **mat2, int row1, int col1, int row2, int col2) {
if (col1 != row2) {
printf("Error: matrixMultiplication: col1 != row2\n"); // check if the matrices can be multiplied
exit(1);
}
float **result = returnMatrix(row1, col2); // allocate memory for the result matrix
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
float sum = 0;
for (int k = 0; k < col1; k++) {
sum += mat1[i][k] * mat2[k][j]; // multiply the matrices
}
result[i][j] = sum; // store the result in the result matrix
}
}
return result; // return the result matrix
}
float **matrixTranspose(float **mat, int row, int col) {
float **transpose = returnMatrix(col, row);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
transpose[j][i] = mat[i][j]; // transpose the matrix
}
}
return transpose;
}
float *rowMeans(float **mat, int row, int col) {
float *means = returnVector(row);
for (int i = 0; i < row; i++) {
means[i] = mean(mat[i], col); // calculate the mean of each row
}
return means;
}
float *columnMeans(float **mat, int row, int col) {
float *means = returnVector(col);
float **transpose = matrixTranspose(mat, row, col); // transpose the matrix to calculate the column means easily
for (int i = 0; i < col; i++) {
means[i] = mean(transpose[i],
row); // calculate the mean of each column actaullay the row means of the transposed matrix
}
freeMatrix(transpose, col); // free the memory of the transposed matrix
return means;
}
float **covarianceMatrix(float **mat, int row, int col) {
float **covMat;
float *means = columnMeans(mat, row, col); // calculate the column means of the matrix
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mat[i][j] -= means[j]; // subtract the column means from the matrix
}
}
float **trans = matrixTranspose(mat, row, col);
covMat = matrixMultiplication(trans, mat, col, row, row,
col); // multiply the matrix with its transposed matrix
for (int i = 0; i < col; i++) {
for (int j = 0; j < col; j++) {
covMat[i][j] /= (row); // divide the elements of the covariance matrix by (row - 1)
}
}
free(means); // free the memory of the column means
freeMatrix(trans, col); // free the memory of the transposed matrix
return covMat;
}
float determinant(float **mat, int row) {
if (row != 3) // check if the matrix is 3x3
{
printf("Error: determinant: row != 3\n");
printf("Because this function is only for 3x3 matrix\n");
exit(1);
}
int col = row + 2; // the number of columns of the augmented matrix
mat = realloc(mat, row * sizeof(float *)); // reallocate memory for the augmented matrix rows
for (int i = 0; i < row; i++) {
mat[i] = realloc(mat[i], col * sizeof(float)); // allocate memory for the augmented matrix
}
for (int i = 0; i < row; i++) {
for (int j = row; j < col; j++) {
mat[i][j] = mat[i][j - row]; // copy the matrix to the right side of the augmented matrix
}
}
float det = 0;
for (int i = 0; i < row; i++) {
float product = 1;
for (int j = 0; j < row; j++) {
int k = row - 1 + j - i; // calculate the index of the element to multiply
product *= mat[j][k]; // multiply the elements of the diagonal
/// printf("i + j: %d,\n", i + j);
}
det += product; // calculate the determinant of the matrix
}
for (int i = 0; i < row; ++i) {
float product = 1;
for (int j = 0; j < row; j++) {
int k = row - 1 + i - j; // calculate the index of the element to multiply
product *= mat[j][k]; // multiply the elements of the diagonal
}
det -= product; // calculate the determinant of the matrix
}
/*
* using the formula of the determinant of the augmented matrix
det += mat[0][0] * mat[1][1] * mat[2][2];
det += mat[0][1] * mat[1][2] * mat[2][3];
det += mat[0][2] * mat[1][3] * mat[2][4];
det -= mat[0][2] * mat[1][1] * mat[2][0];
det -= mat[0][3] * mat[1][2] * mat[2][1];
det -= mat[0][4] * mat[1][3] * mat[2][2];
*/
return det;
}
void printVector(float *vec, int N) {
for (int i = 0; i < N; i++) {
printf("%f ", vec[i]); // print the vector
}
printf("\n"); // print a new line
}
void printMatrix(float **mat, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%f ", mat[i][j]); // print the matrix
}
printf("\n"); // print a new line after each row
}
}
// my functions
float calculateVariance(float *vec, int row) {
float m = mean(vec, row);
float sum = 0;
for (int i = 0; i < row; ++i) {
sum += (float) pow(vec[i] - m, 2);
}
return sum / (float) (row);
}
void fillVector(float *vec, int size, int seed) {
srand(seed);
for (int i = 0; i < size; i++) {
vec[i] = (float) (1 + rand() % 10); // fill the vector with random numbers
}
}
void fillMatrix(float **mat, int row, int col, int seed) {
srand(seed);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mat[i][j] = (float) (1 + rand() % 10); // fill the matrix with random numbers
}
}
}