-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
450 lines (369 loc) · 11.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "matrix.h"
double randomNormal(double mu, double sigma) {
double U1, U2, W, mult;
static double X1, X2;
static int call = 0;
if (call == 1) {
call = !call;
return (mu + sigma * (double) X2);
}
do {
U1 = -1 + ((double) rand() / RAND_MAX) * 2;
U2 = -1 + ((double) rand() / RAND_MAX) * 2;
W = pow(U1, 2) + pow(U2, 2);
} while (W >=1 || W == 0);
mult = sqrt((-2 * log(W)) / W);
X1 = U1 * mult;
X2 = U2 * mult;
call = !call;
return (mu + sigma * (double) X1);
}
matrix * randnMatrix(int rows, int cols, double mu, double sigma) {
if (rows <= 0 || cols <= 0) return NULL;
matrix *m = (matrix*)malloc(sizeof(matrix));
m->rows = rows;
m->cols = cols;
m->data = (double*)malloc(rows * cols * sizeof(double));
int i;
for (i = 0; i < rows*cols; i++){
m->data[i] = randomNormal(mu, sigma);
}
return m;
}
matrix * newMatrix(int rows, int cols) {
if (rows <= 0 || cols <= 0) return NULL;
matrix *m = (matrix*)malloc(sizeof(matrix));
m->rows = rows;
m->cols = cols;
m->data = (double*)malloc(rows * cols * sizeof(double));
int i;
for (i = 0; i < rows*cols; i++){
m->data[i] = 0.0;
}
return m;
}
int deleteMatrix(matrix * mtx) {
if (mtx == NULL) return -1;
assert (mtx->data);
free(mtx->data);
free(mtx);
return 0;
}
// 按行存储矩阵
#define ELEM(mtx, row, col) \
mtx->data[(row-1) * mtx->cols + (col-1)]
matrix * copyMatrix(matrix * mtx) {
if (mtx == NULL) return NULL;
matrix * cp = newMatrix(mtx->rows, mtx->cols);
memcpy(cp->data, mtx->data, mtx->rows * mtx->cols * sizeof(double));
return cp;
}
int setElement(matrix * mtx, int row, int col, double val) {
if (mtx == NULL) return -1;
assert(mtx->data);
if (row <= 0 || row > mtx->rows || col <= 0 || col > mtx->cols) return -2;
ELEM(mtx, row, col) = val;
return 0;
}
int getElement(matrix * mtx, int row, int col, double *val) {
if (mtx == NULL) return -1;
assert (mtx->data);
if (row <= 0 || row > mtx->rows || col <=0 || col > mtx->cols) return -2;
*val = ELEM(mtx, row, col);
return 0;
}
int nRows(matrix * mtx) {
if (mtx == NULL) return -1;
return mtx->rows;
}
int nCols(matrix * mtx) {
if (mtx == NULL) return -1;
return mtx->cols;
}
int printMatrix(matrix * mtx) {
if (mtx == NULL) return -1;
int row, col;
for (row = 1; row <= mtx->rows; row++) {
for (col = 1; col <= mtx->cols; col++) {
printf("% G ", ELEM(mtx, row, col));
}
printf("\n");
}
}
int transpose(matrix * in, matrix * out) {
if (in == NULL || out == NULL) return -1;
if (in->rows != out->cols || in->cols != out->rows) return -2;
int row, col;
for (row = 1; row <= in->rows; row++)
for (col = 1; col <= in->cols; col++)
ELEM(out, col, row) = ELEM(in, row, col);
return 0;
}
matrix *transpose1(matrix * in) {
if (in == NULL) return NULL;
matrix *out = newMatrix(in->cols, in->rows);
int row, col;
for (row = 1; row <= in->rows; row++)
for (col = 1; col <= in->cols; col++)
ELEM(out, col, row) = ELEM(in, row, col);
return out;
}
int swap(double *e1, double *e2) {
if (e1 == NULL || e2 == NULL) return -1;
double temp;
temp = *e1;
*e1 = *e2;
*e2 = temp;
return 0;
}
int transposeSelf(matrix * mtx) {
if (mtx == NULL) return -1;
int old_rows, old_cols, index, old_index;
old_rows = mtx->rows;
old_cols = mtx->cols;
//交换行列数
mtx->rows = old_cols;
mtx->cols = old_rows;
int row, col, r, c;
for (row = 1; row <= mtx->rows; row++) {
for (col = 1; col <= mtx->cols; col++) {
// 当前矩阵(即转置后的矩阵)元素位置row,col,step = new_cols
index = mtx->cols * (row - 1) + col;
// 当前元素对应的原矩阵中元素的初始位置col,row,step = mtx->cols
old_index = old_cols * (col - 1) + row;
//printf("index: %d\told_index: %d\n", index, old_index);
// 寻找原始矩阵对应元素的实际位置
while (index > old_index) {
r = old_index % mtx->cols == 0 ?
(old_index / mtx->cols) :
(old_index / mtx->cols + 1);
c = old_index - mtx->cols * (r - 1);
old_index = old_cols * (c - 1) + r;
//printf("index: %d\told_index: %d\n", index, old_index);
}
// 恰好在应该放置的位置,不用交换
if (index == old_index) continue;
swap(&(mtx->data[index-1]), &(mtx->data[old_index-1]));
//swap(&(ELEM(mtx, row, col)), &(ELEM(mtx, c ,r))); // ERROR: 不能对宏取地址
//printf("index: %d\n", index);
//printMatrix(mtx);
}
}
return 0;
}
int sum(matrix * mtx1, matrix * mtx2, matrix * sum) {
if (mtx1 == NULL || mtx2 == NULL || sum == NULL) return -1;
if (mtx1->rows != mtx2->rows ||
mtx1->rows != sum->rows ||
mtx1->cols != mtx2->cols ||
mtx1->cols != sum->cols)
return -2;
int row, col;
for (row = 1; row <= mtx1->rows; row++)
for (col = 1; col <= mtx1->cols; col++)
ELEM(sum, row, col) = ELEM(mtx1, row, col) + ELEM(mtx2, row, col);
return 0;
}
matrix *sum1(matrix * mtx1, matrix * mtx2) {
if (mtx1 == NULL || mtx2 == NULL) return NULL;
if (mtx1->rows != mtx2->rows || mtx1->cols != mtx2->cols) return NULL;
matrix *sum = newMatrix(mtx1->rows, mtx1->cols);
int row, col;
for (row = 1; row <= mtx1->rows; row++)
for (col = 1; col <= mtx1->cols; col++)
ELEM(sum, row, col) = ELEM(mtx1, row, col) + ELEM(mtx2, row, col);
return sum;
}
int minus(matrix *m1, matrix *m2, matrix *result) {
if (m1 == NULL || m2 == NULL || result == NULL) return -1;
if (m1->rows != m2->rows ||
m1->rows != result->rows ||
m1->cols != m2->cols ||
m1->cols != result->cols)
return -2;
int row, col;
for (row = 1; row <= result->rows; row++)
for (col = 1; col <= result->cols; col++)
ELEM(result, row, col) = ELEM(m1, row, col) - ELEM(m2, row, col);
return 0;
}
matrix *minus1(matrix *m1, matrix *m2) {
if (m1 == NULL || m2 == NULL) return NULL;
if (m1->rows != m2->rows || m1->cols != m2->cols) return NULL;
matrix * result = newMatrix(m1->rows, m1->cols);
int row, col;
for (row = 1; row <= result->rows; row++)
for (col = 1; col <= result->cols; col++)
ELEM(result, row, col) = ELEM(m1, row, col) - ELEM(m2, row, col);
return result;
}
int sumSelf(matrix * mtx, double *sum) {
if (mtx == NULL || sum == NULL) return -1;
*sum = 0.0;
int row, col;
for (row = 1; row <= mtx->rows; row++)
for (col = 1; col <= mtx->cols; col++)
*sum = *sum + ELEM(mtx, row, col);
return 0;
}
double sumSelf1(matrix * mtx) {
if (mtx == NULL) return -1;
double sum = 0.0;
int row, col;
for (row = 1; row <= mtx->rows; row++)
for (col = 1; col <= mtx->cols; col++)
sum += ELEM(mtx, row, col);
return sum;
}
int product(matrix * mtx1, matrix * mtx2, matrix * prod) {
if (mtx1 == NULL || mtx2 == NULL || prod == NULL) return -1;
if (mtx1->cols != mtx2->rows ||
mtx1->rows != prod->rows ||
mtx2->cols != prod->cols)
return -2;
int row, col, k;
for (row = 1; row <= mtx1->rows; row++) {
for (col = 1; col <= mtx2->cols; col++) {
double val = 0.0;
for (k = 1; k <= mtx2->rows; k++) {
val += ELEM(mtx1, row, k) * ELEM(mtx2, k, col);
}
ELEM(prod, row, col) = val;
}
}
return 0;
}
matrix *product1(matrix * mtx1, matrix * mtx2) {
if (mtx1 == NULL || mtx2 == NULL) return NULL;
if (mtx1->cols != mtx2->rows) return NULL;
matrix *prod = newMatrix(mtx1->rows, mtx2->cols);
int row, col, k;
for (row = 1; row <= prod->rows; row++) {
for (col = 1; col <= prod->cols; col++) {
double val = 0.0;
for (k = 1; k <= mtx2->rows; k++) {
val += ELEM(mtx1, row, k) * ELEM(mtx2, k, col);
}
ELEM(prod, row, col) = val;
}
}
return prod;
}
int dotProduct(matrix *v1, matrix *v2, double * prod) {
if (v1 == NULL || v2 == NULL || prod == NULL) return -1;
if (v1->cols != 1 || v2->cols != 1) return -2;
if (v1->rows != v2->rows) return -3;
*prod = 0.0;
int i;
for (i = 1; i <= v1->rows; i++)
*prod += ELEM(v1, i, 1) * ELEM(v2, i, 1);
return 0;
}
int scalarProduct(matrix *m1, matrix *m2, matrix *m) {
if (m1 == NULL || m2 == NULL || m == NULL) return -1;
if (m1->rows != m2->rows ||
m1->rows != m->rows ||
m1->cols != m2->cols ||
m1->cols != m->cols)
return -2;
int row, col;
for (row = 1; row <= m->rows; row++)
for (col = 1; col <= m->cols; col++)
ELEM(m, row, col) = ELEM(m1, row, col) * ELEM(m2, row, col);
return 0;
}
matrix *scalarProduct1(matrix *m1, matrix *m2) {
if (m1 == NULL || m2 == NULL) return NULL;
if (m1->rows != m2->rows || m1->cols != m2->cols) return NULL;
matrix *m = newMatrix(m1->rows, m1->cols);
int row, col;
for (row = 1; row <= m->rows; row++)
for (col = 1; col <= m->cols; col++)
ELEM(m, row, col) = ELEM(m1, row, col) * ELEM(m2, row, col);
return m;
}
int identity(matrix * mtx) {
if (mtx == NULL || mtx->rows != mtx->cols) return -1;
int row, col;
for (row = 1; row <= mtx->rows; row++)
for (col = 1; col <= mtx->cols; col++)
if (row == col)
ELEM(mtx, row, col) = 1.0;
else
ELEM(mtx, row, col) = .0;
return 0;
}
int funcMatrix(matrix *mtx, double (*func)(double x)) {
if (mtx == NULL) return -1;
int row, col;
double elem;
for (row = 1; row <= mtx->rows; row++) {
for (col = 1; col <= mtx->cols; col++) {
getElement(mtx, row, col, &elem);
elem = func(elem);
setElement(mtx, row, col, elem);
}
}
}
matrix *funcMatrix1(matrix *mtx, double (*func)(double x)) {
if (mtx == NULL) return NULL;
matrix *new = copyMatrix(mtx);
int row, col;
double elem;
for (row = 1; row <= new->rows; row++) {
for (col = 1; col <= new->cols; col++) {
getElement(new, row, col, &elem);
elem = func(elem);
setElement(new, row, col, elem);
}
}
return new;
}
int multiplyMatrix(matrix *mtx, double factor) {
if (mtx == NULL) return -1;
int row, col;
double elem;
for (row = 1; row <= mtx->rows; row++) {
for (col = 1; col <= mtx->cols; col++) {
getElement(mtx, row, col, &elem);
elem *= factor;
setElement(mtx, row, col, elem);
}
}
return 0;
}
// 返回一个向量的最大元素下标
int maxVector(matrix *vector) {
if (vector == NULL || vector->cols != 1) return -1;
int row, max_index;
double elem, max;
max = -INFINITY;
for (row = 1; row <= vector->rows; row++) {
getElement(vector, row, 1, &elem);
if (elem > max) {
max = elem;
max_index = row;
}
}
return max_index;
}
// 返回一个向量的最大元素下标
int minVector(matrix *vector) {
if (vector == NULL || vector->cols != 1) return -1;
int row, min_index;
double elem, min;
min = INFINITY;
for (row = 1; row <= vector->rows; row++) {
getElement(vector, row, 1, &elem);
if (elem < min) {
min = elem;
min_index = row;
}
}
return min_index;
}