-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
executable file
·349 lines (298 loc) · 6.71 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
============================================================================
Name : matrix.c
Author : Julian Fietkau
Copyright : Julian Fietkau
Description : matrix datatype and standard functionality
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "stack.h"
#include <string.h>
#include "help.h"
/*
* getter for value (x,y) of committed matrix
*/
double getValue(matrix * matrix, int x, int y) {
if ((x < 0) || (y < 0)) {
perror("Matrix index starts @ 0!\n");
return 0.0;
}
if (x > matrix->rows) {
perror("out of bounds!");
printf("[%d][%d]\n",x,y);
return 0.0;
}
if (y > matrix->columns) {
perror("out of bounds!\n");
printf("[%d][%d]\n",x,y);
return 0.0;
}
return matrix->values[(x * (matrix->columns)) + y];
}
/*
* setter for value (x,y) of committed matrix
*/
int setValue(matrix * matrix, double value, int x, int y) {
if ((x < 0) || (y < 0)) {
perror("Matrix index starts @ 0!\n");
printf("[%d][%d]\n",x,y);
return 0;
}
if (x > matrix->rows) {
perror("out of bounds!\n");
printf("[%d][%d]\n",x,y);
return 0;
}
if (y > matrix->columns) {
perror("out of bounds!\n");
return 0;
}
matrix->values[(matrix->columns * x) + y] = value;
return 1;
}
double absolut(double value){
if( value > 0) return value;
if( value < 0) return -1*value;
if( value == 0) return 0;
}
/*
* Defines equality for matrices.
* Matrix is individual via size and by values.
*/
int equals(matrix * m1, matrix * m2){
if(m1->columns != m2->columns){
return 0;
}
if(m1->rows != m2->rows){
return 0;
}
int x = 0; int y = 0; float E = 0.0000000001;
while(x < m1->rows){
while(y < m1->columns){
// Comparing 2 double values, by compare the diff with a super-small number E
if(!(absolut(getValue(m1,x,y) - getValue(m2,x,y)) <= E)){
return 0;
}
y++;
}
y = 0;
x++;
}
return 1;
}
/*
* create a matrix with dimention row x column
*/
void createMatrix(matrix * m1, int rows, int column) {
m1->rows = rows;
m1->columns = column;
double * array = malloc(m1->rows * m1->columns * sizeof(double));
int i = 0;
for(i = 0; i < m1->rows * m1->columns; i++){ // Array init with zeros
array[i] = 0.0;
}
if (array == NULL ) {
perror("createMatrix() : out of memory");
}
m1->values = array;
}
/*
* destroy matrix and free() the allocated memory
*/
int destroyMatrix(matrix * m1) {
free(m1->values);
return 1;
}
/*
* prints matrix on an natural way
*/
void printMatrix(matrix * m1) {
if(0){
perror("Matrix is empty.");
}
printf("\n");
int x = 0;
int y = 0;
while (x < m1->rows) {
while (y < m1->columns) {
printf("%f", getValue(m1, x, y));
if ((y + 1 <= m1->columns)) {
printf(" \t ");
}
y++;
}
printf("\n");
y = 0;
x++;
}
printf("\n");
}
/*
* prints matrix as row
*/
void printMatrixRow(matrix * m1) {
printf("[");
int x = 0;
int y = 0;
while (x < m1->rows) {
while (y < m1->columns) {
printf("%f", getValue(m1, x, y));
if ((y + 1 < m1->columns)) {
printf(", ");
}
y++;
}
if( x + 1 < m1->rows){
printf("; ");
}
y = 0;
x++;
}
printf("]\n");
}
/*
* Matrix multiplication implementation without parallelism
* Check for : (a x b) * (b x c)
* Calculation: Sum( ROW * COLUMN )
*/
int multiplication(matrix * result, matrix * m1, matrix * m2) {
if (m1->columns != m2->rows) {
perror("Spaltenanzahl m1 muss gleich Zeilenanzahl von m2 sein!\n");
return 0;
}
//printf("--> result with %d x %d :", m1->rows, m2->columns);
createMatrix(result, m1->rows, m2->columns);
int x, y, m;
for (x = 0; x < result->rows; x++) {
for (y = 0; y < result->columns; y++) {
for (m = 0; m < m1->columns; m++) {
double temp = getValue(result,x,y) + getValue(m1,x,m) * getValue(m2,m,y);
setValue(result,temp,x,y);
}
}
}
return 1;
}
/*
* Generate matrix out of a given String
* Matrix must look like: [double,double;...;double,double]
* Hint: Because realloc() copy all data to make a new entity of our datastructure,
* it is more efficient to iterate our data twice and malloc the calculated size .
* (n * n) > 2 * n
*/
int parse_matrix(matrix * returnMatrix, char * stringData) {
int i = 1;
int values = 0;
int xSize = 1;
int ySize = 0;
node * stack = NULL;
init(stack);
if (stringData == NULL ) {
perror("string is empty!\n");
}
if (stringData[0] != '[') {
perror("matrix starts with [\n");
}
if (stringData[1] == ']') {
perror("matrix is empty!\n");
}
/*
* Iterate string, parse values and stack 'em.
*/
while (i < strlen(stringData) - 1) {
// Numbers, Columns separated by ,
if (stringData[i] == ',') {
i++;
if (ySize == 0) {
xSize++;
}
continue;
}
// Rows separated by ;
if (stringData[i] == ';') {
i++;
ySize++;
continue;
}
// Converting string vales into real double
if (isANumber(stringData[i])) {
char * numberString = calloc(16, sizeof(char)); // Double has max. of 15 digites + '.'
int iterateNumberString = 0;
while (isPartOfNumber(stringData[i])) { // itererate the whole number_string
numberString[iterateNumberString] = stringData[i];
iterateNumberString++;
i++;
}
double number = atof(numberString); // Converting with c_standard function atof()
stack = push(stack, number); // notice the number for allocation
values++;
continue;
}
perror("Matrix contains restricted sign(s)!\n");
break;
}
if (stringData[i] != ']') {
perror("matrix ! ends with ]\n");
}
/*
* Allocate matrix and fill with numbers
*/
//printf("INPUT: Allocate matrix with dimension of %d x %d\n", ySize + 1, xSize);
returnMatrix->rows = ySize + 1;
returnMatrix->columns = xSize;
createMatrix(returnMatrix, returnMatrix->rows, returnMatrix->columns);
int x = returnMatrix->rows-1, y = returnMatrix->columns-1;
double value;
while (x >= 0) {
while (y >= 0) {
stack = pop(stack, &value); // remember the number (LIFO)
setValue(returnMatrix,value,x,y); // save data at his specific place
y--;
}
y = returnMatrix->columns-1;
x--;
}
return 1;
}
/*
* generate identity matrix for problem size n
*/
int createIdentityMatrix(matrix * returnMatrix, int n){
createMatrix(returnMatrix,n,n);
int x = 0;
while(x < n){
if(!setValue(returnMatrix,1.0,x,x)){
return 0;
}
x++;
}
return 1;
}
/*
* Generate a random number
*/
double randomDouble(){
return ((double) 10*rand()/(double)RAND_MAX);
}
/*
* Generate a n x m matrix with random numbers
*/
int createRandomMatrix(matrix * returnMatrix, int n, int m){
createMatrix(returnMatrix,n,m);
int x = 0;
int y = 0;
while(y < m){
while(x < n){
if(!setValue(returnMatrix,randomDouble(),x,y)){
return 0;
}
x++;
}
x = 0;
y++;
}
return 1;
}