-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
292 lines (230 loc) · 6.76 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Vinicius Freitas 20210026803
// Natanael Neto 20190038391
#include "matrix.h"
Matrix create_matrix(int *data, int n_rows, int n_cols){
Matrix matriz;
matriz.data = data;
matriz.n_rows = n_rows;
matriz.n_cols = n_cols;
matriz.stride_rows = n_cols;
matriz.stride_cols = 1;
matriz.offset = 0;
return matriz;
}
Matrix zeros_matrix(int n_rows, int n_cols){
int *p = malloc(n_rows * n_cols * sizeof(int));
int size = n_rows * n_cols;
for(int i = 0; i < size; i++){
p[i] = 0;
}
return create_matrix(p, n_rows, n_cols);
}
Matrix full_matrix(int n_rows, int n_cols, int value){
int *p = malloc(n_rows * n_cols * sizeof(int));
int size = n_rows * n_cols;
for(int i = 0; i < size; i++){
p[i] = value;
}
return create_matrix(p , n_rows, n_cols);
}
Matrix i_matrix(int n){
int *p = malloc(n * n * sizeof(int));
int size = n * n;
for(int i = 0; i < size; i++){
p[i] = 0;
}
int count = 0;
p[0] = 1;
for(int i = 0; i < size; i++){
count++;
if(count == n+1){
count = 0;
p[i+1] = 1;
}
}
return create_matrix(p, n, n);
}
Matrix tile_matrix(Matrix matrix, int reps){
int *p = malloc(sizeof(int) * matrix.n_rows * matrix.n_cols * reps);
int size = matrix.n_rows * (matrix.n_cols * reps);
int condition = 0;
int aux = 0;
int count = 0;
for(int i = 0; i < size; i++){
p[i] = matrix.data[aux + count];
aux++;
condition++;
if(aux == matrix.stride_rows){
aux = 0;
if(condition == (matrix.stride_rows * reps)){
count += matrix.stride_rows;
condition = 0;
}
}
}
return create_matrix(p, matrix.n_rows, (matrix.n_cols * reps));
}
int get_element(Matrix matrix, int ri, int ci){
if(ri >= matrix.n_rows){
printf("Index out of range.\n");
return 0;
}
if(ci >= matrix.n_cols){
printf("Index out of range.\n");
return 0;
}else{
int element = matrix.data[((ri * matrix.n_cols) + ci)];
return element;
}
}
void put_element(Matrix matrix, int ri, int ci, int elem){
if(ri >= matrix.n_rows){
printf("Index out of range.\n");
return 0;
}
if(ci >= matrix.n_cols){
printf("Index out of range.\n");
return 0;
}else{
matrix.data[((ri * matrix.n_cols) + ci)] = elem;
}
}
void print_matrix(Matrix matrix){
int size = matrix.n_rows * matrix.n_cols;
int column = 0;
for(int i = 0; i < size; i++){
column++;
if(matrix.data[i] >= 10 || matrix.data[i] < 0){
printf("%d ", matrix.data[i]);
}
else{
printf("%d ", matrix.data[i]);
}
if(column == matrix.stride_rows){
column = 0;
printf("\n");
}
}
}
Matrix transpose(Matrix matrix){
int size = matrix.n_cols * matrix.n_rows;
int *p = malloc(sizeof(int) * size);
int aux = 0;
int proximo = 0;
for(int i = 0; i < size; i++){
p[aux] = matrix.data[i];
if(aux >= (size - matrix.n_rows)){
proximo++;
aux = proximo;
}else{
aux += matrix.n_rows;
}
}
return create_matrix(p, matrix.n_cols, matrix.n_rows);
}
Matrix reshape(Matrix matrix, int new_n_rows, int new_n_cols){
int size = matrix.n_rows * matrix.n_cols;
if((new_n_rows * new_n_cols) > size){
printf("Reshape failed, you are trying to reshape the original matrix into a larger one.\n");
}
else{
matrix.n_rows = new_n_rows;
matrix.n_cols = new_n_cols;
matrix.stride_rows = matrix.n_cols;
matrix.stride_cols = 1;
}
return matrix;
}
Matrix slice(Matrix a_matrix, int rs, int re, int cs, int ce){
a_matrix.n_rows = (re - rs);
a_matrix.n_cols = (ce - cs);
a_matrix.data[a_matrix.n_cols * a_matrix.n_rows];
a_matrix.offset = (a_matrix.stride_rows * rs)+ cs;
int stop = a_matrix.offset + ((a_matrix.n_rows - 1) * a_matrix.stride_rows) + (a_matrix.n_cols - 1);
int skip = a_matrix.stride_rows - a_matrix.n_cols;
int count = 1;
int index = 0;
for(int i = a_matrix.offset; i < stop + 1; i++){
a_matrix.data[index] = a_matrix.data[i];
if(count == a_matrix.n_cols){
i += skip;
count = 0;
}
count++;
index++;
}
a_matrix.stride_rows = a_matrix.n_cols;
return a_matrix;
}
int min(Matrix matrix){
int size = matrix.n_rows * matrix.n_cols;
int min_value = 99999;
for(int i = 0; i < size; i++){
if (matrix.data[i] < min_value){
min_value = matrix.data[i];
}
}
return min_value;
}
int max(Matrix matrix){
int size = matrix.n_rows * matrix.n_cols;
int max_value = -99999;
for(int i = 0; i < size; i++){
if (matrix.data[i] > max_value){
max_value = matrix.data[i];
}
}
return max_value;
}
int argmin(Matrix matrix){
int size = matrix.n_rows * matrix.n_cols;
int min_value = 99999;
for(int i = 0; i < size; i++){
if (matrix.data[i] < min_value){
min_value = i;
}
}
return min_value;
}
int argmax(Matrix matrix){
int size = matrix.n_rows * matrix.n_cols;
int max_value = -99999;
for(int i = 0; i < size; i++){
if (matrix.data[i] > max_value){
max_value = i;
}
}
return max_value;
}
Matrix add(Matrix matrix_1, Matrix matrix_2){
int *p = malloc(sizeof(int) * matrix_1.n_rows * matrix_1.n_cols);
int size = matrix_1.n_rows * matrix_1.n_cols;
for(int i = 0; i < size; i++){
p[i] = matrix_1.data[i] + matrix_2.data[i];
}
return create_matrix(p, matrix_1.n_rows, matrix_1.n_cols);
}
Matrix sub(Matrix matrix_1, Matrix matrix_2){
int *p = malloc(sizeof(int) * matrix_1.n_rows * matrix_1.n_cols);
int size = matrix_1.n_rows * matrix_1.n_cols;
for(int i = 0; i < size; i++){
p[i] = matrix_1.data[i] - matrix_2.data[i];
}
return create_matrix(p, matrix_1.n_rows, matrix_1.n_cols);
}
Matrix division(Matrix matrix_1, Matrix matrix_2){
int *p = malloc(sizeof(int) * matrix_1.n_rows * matrix_1.n_cols);
int size = matrix_1.n_rows * matrix_1.n_cols;
for(int i = 0; i < size; i++){
p[i] = matrix_1.data[i] / matrix_2.data[i];
}
return create_matrix(p, matrix_1.n_rows, matrix_1.n_cols);
}
Matrix mul(Matrix matrix_1, Matrix matrix_2){
int *p = malloc(sizeof(int) * matrix_1.n_rows * matrix_1.n_cols);
int size = matrix_1.n_rows * matrix_1.n_cols;
for(int i = 0; i < size; i++){
p[i] = matrix_1.data[i] * matrix_2.data[i];
}
return create_matrix(p, matrix_1.n_rows, matrix_1.n_cols);
}