-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.c
142 lines (130 loc) · 3.67 KB
/
Matrix.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
#include "Matrix.h"
Matrix *create_matrix(int rows, int cols){
Matrix *new_matrix = (Matrix *) malloc(sizeof(Matrix));
new_matrix->data = (float *) malloc(sizeof(float) * rows * cols);
if(new_matrix == NULL){
printf("Memory Allocation Error !");
exit(EXIT_FAILURE);
}
new_matrix->rows = rows;
new_matrix->cols = cols;
return new_matrix;
}
int linear_index(int x, int y, int cols){
return x * cols + y;
}
void fill_matrix_const(Matrix *matrix, double n){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
int m_size = matrix->rows * matrix->cols;
for(int i = 0; i < m_size; i++){
matrix->data[i] = n;
}
}
double rand_double(double min, double max){
double range = max - min;
double scaled = (double)rand() / RAND_MAX;
return min + scaled * range;
}
void fill_matrix_rand(Matrix *matrix, double min, double max){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
srand(time(NULL));
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
int m_size = matrix->rows * matrix->cols;
for(int i = 0; i < m_size; i++){
matrix->data[i] = rand_double(min, max);
}
}
Matrix *transpose(Matrix* matrix){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
Matrix *n_matrix = create_matrix(matrix->cols, matrix->rows);
for(int i = 0; i < matrix->rows; i++){
for(int j = 0; j < matrix->cols; j++){
n_matrix->data[linear_index(j, i, n_matrix->cols)] = matrix->data[linear_index(i, j, matrix->cols)];
}
}
return n_matrix;
}
void display_matrix(Matrix *matrix){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
printf("\n");
printf("Matrix dimensions are (%d, %d)\n", matrix->rows, matrix->cols);
for(int i = 0; i < matrix->rows; i++){
for(int j = 0; j < matrix->cols; j++){
printf("%f ", matrix->data[linear_index(i, j, matrix->cols)]);
}
printf("\n");
}
printf("\n");
}
void free_matrix(Matrix *matrix){
free(matrix->data);
free(matrix);
}
Matrix * matrix_cpy(Matrix *matrix){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
Matrix *n_matrix = create_matrix(matrix->rows, matrix->cols);
for(int i = 0; i < matrix->rows * matrix->cols; i++){
n_matrix->data[i] = matrix->data[i];
}
return n_matrix;
}
int arg_max(Matrix *matrix){
if(matrix == NULL){
printf("Null pointer!");
exit(EXIT_FAILURE);
}
if (matrix->data == NULL){
printf("Matrix mem is not initialized!");
exit(EXIT_FAILURE);
}
if(matrix->cols != 1){
printf("arg_max expects (M, 1) shaped matrix!");
exit(EXIT_FAILURE);
}
float max = matrix->data[0];
int max_idx = 0;
for(int i = 1; i < matrix->rows; i++){
if(matrix->data[i] > max){
max = matrix->data[i];
max_idx = i;
}
}
return max_idx;
}
void print_shape(Matrix *matrix){
printf("Shape of matrix is (%d, %d)\n", matrix->rows, matrix->cols);
}