-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrices.c
169 lines (101 loc) · 3.58 KB
/
matrices.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
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int n_rows;
int n_cols;
float data[]; // gonna use that trick i saw on youtube to make the data contiguous :)
}matrix;
matrix* create_matrix(int n_rows, int n_cols){
int n_elements = n_rows*n_cols;
int size = sizeof(matrix) + n_elements*sizeof(float);
matrix* matrix = malloc(size);
matrix -> n_rows = n_rows;
matrix -> n_cols = n_cols;
return matrix;
}
matrix* create_and_fill_matrix(float* data, int n_rows, int n_cols){
matrix* matrix = create_matrix(n_rows, n_cols);
int n_elements = n_rows*n_cols;
for (int i=0; i<n_elements; i++){
(matrix -> data)[i] = data[i];
}
return matrix;
}
float add(float a, float b){
return a+b;
}
int get_flat_idx(matrix* matrix, int row_idx, int col_idx){
return matrix -> n_cols * row_idx + col_idx;
}
void print_matrix(matrix* matrix){
for (int i=0; i<(matrix -> n_rows); i++){
for (int j=0; j<(matrix -> n_cols); j++){
printf("%f ", (matrix -> data)[get_flat_idx(matrix, i, j)]);
}
printf("\n");
}
}
matrix* element_wise(matrix* matrix1, matrix* matrix2, float (*op) (float, float) ){
int n_rows1 = matrix1 -> n_rows;
int n_rows2 = matrix2 -> n_rows;
int n_cols1 = matrix1 -> n_cols;
int n_cols2 = matrix2 -> n_cols;
if (n_rows1 != n_rows2 || n_cols1 != n_cols2){
printf(" not equal size ):<\n");
}
matrix* matrix_out = create_matrix(n_rows1, n_cols1);
for (int i=0; i<n_rows1; i++){
for (int j=0; j<n_cols1; j++){
int flat_idx = get_flat_idx(matrix1, i, j);
float value1 = (matrix1 -> data)[flat_idx];
float value2 = (matrix2 -> data)[flat_idx];
float result = op(value1, value2);
(matrix_out -> data)[flat_idx] = result;
}
}
return matrix_out;
}
matrix* get_section(matrix* matrix_in, int row_low, int row_high, int col_low, int col_high){
int n_rows_out = row_high-row_low;
int n_cols_out = col_high-col_low;
matrix* matrix_out = create_matrix(n_rows_out, n_cols_out);
float* data_in = matrix_in -> data;
float* data_out = matrix_out -> data;
for (int i=0;i<n_rows_out;i++){
int i_shifted = i+row_low;
for (int j=0; j<n_cols_out; j++){
int j_shifted = j+col_low;
int flat_idx_in = get_flat_idx(matrix_in, i_shifted, j_shifted);
int flat_idx_out = get_flat_idx(matrix_out, i, j);
data_out[flat_idx_out] = data_in[flat_idx_in];
}
}
return matrix_out;
}
matrix* transpose(matrix* matrix_in){
int n_rows_old = matrix_in -> n_rows;
int n_cols_old = matrix_in -> n_cols;
matrix* transposed = create_matrix(n_cols_old, n_rows_old);
float* data_in = matrix_in -> data;
float* data_out = transposed -> data;
for (int i=0;i<n_rows_old;i++){
for (int j=0;j<n_cols_old;j++){
int flat_idx_old = get_flat_idx(matrix_in, i, j);
int flat_idx_new = get_flat_idx(transposed, j, i);
data_out[flat_idx_new] = data_in[flat_idx_old];
}
}
return transposed;
}
int main(){
// int a[6] = {1,2,3,4,5,6};
float arr[6] = {1,2,3,4,5,6};
matrix* matrix1 = create_and_fill_matrix(arr,2,3);
float arr2[6] = {1,5,3,4,5,6};
matrix* matrix2 = create_and_fill_matrix(arr2,2,3);
matrix* sum = element_wise(matrix1, matrix2, add);
matrix* section = get_section(matrix1, 0,2,1,3);
print_matrix(matrix1);
printf("\n");
print_matrix(transpose(matrix1));
}