forked from mribri999/MRSignalsSeqs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvx_matrix.c
407 lines (295 loc) · 8.05 KB
/
cvx_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
#include <stdlib.h>
#include <stdio.h>
#include "cvx_matrix.h"
int cvxmat_alloc(cvx_mat *mat, int rows, int cols)
{
mat->vals = malloc(rows * cols * sizeof(double));
if (mat->vals == NULL) {
fprintf(stderr, "*** ERROR: Out-of-memory rows = %d cols = %d ***\n", rows, cols);
return 0;
}
mat->rows = rows;
mat->cols = cols;
mat->N = rows*cols;
// Slows us slightly, but much safer
for (int i = 0; i < mat->N; i++) {
mat->vals[i] = 0.0;
}
return 1;
}
double cvxmat_get(cvx_mat *mat, int row, int col)
{
return mat->vals[mat->cols * row + col];
}
void cvxmat_set(cvx_mat *mat, int row, int col, double val)
{
mat->vals[mat->cols * row + col] = val;
}
void cvxmat_EWinvert(cvx_mat *mat)
{
for (int i = 0; i < mat->N; i++) {
mat->vals[i] = 1.0/mat->vals[i];
}
}
void cvxmat_EWmultIP(cvx_mat *mat0, cvx_mat *mat1)
{
for (int i = 0; i < mat0->N; i++) {
mat0->vals[i] *= mat1->vals[i];
}
}
void cvxmat_subractMat(cvx_mat *mat0, cvx_mat *mat1, cvx_mat *mat2)
{
for (int i = 0; i < mat0->N; i++) {
mat0->vals[i] = mat1->vals[i] - mat2->vals[i];
}
}
void cvxmat_multMatMat(cvx_mat *C, cvx_mat *A, cvx_mat *B)
{
double sum;
for (int i = 0; i < A->rows; i++) {
for (int j = 0; j < B->cols; j++) {
sum = 0.0;
for (int k = 0; k < A->cols; k++) {
//sum += A[i,k] * B[k,j]
sum += A->vals[k + A->cols * i] * B->vals[j + B->cols * k];
}
C->vals[j + C->cols * i] = sum;
}
}
}
void cvxmat_inplace_transpose(cvx_mat *A)
{
double *new_vals;
double *temp_free = A->vals;
new_vals = malloc(A->N * sizeof(double));
for (int i = 0; i < A->rows; i++) {
for (int j = 0; j < A->cols; j++) {
new_vals[A->cols * i + j] = A->vals[A->cols * j + i];
}
}
A->vals = new_vals;
free(temp_free);
}
void cvxmat_multAtA(cvx_mat *C, cvx_mat *A)
{
cvxmat_inplace_transpose(A);
double sum;
for (int i = 0; i < A->rows; i++) {
for (int j = 0; j < A->cols; j++) {
sum = 0.0;
for (int k = 0; k < A->cols; k++) {
//sum += A[i,k] * B[k,j]
sum += A->vals[A->cols * i + k] * A->vals[A->cols * j + k];
// sum += A->vals[A->cols * k + i] * A->vals[A->cols * k + j];
}
C->vals[j + C->cols * i] = sum;
}
}
}
void cvxmat_multAx(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
double sum;
for (int i = 0; i < A->rows; i++) {
sum = 0.0;
for (int j = 0; j < A->cols; j++) {
sum += A->vals[A->cols * i + j] * x->vals[j];
}
b->vals[i] = sum;
}
}
void cvxmat_multAx2(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
double sum;
double *Apos = &(A->vals[0]);
double *bpos = &(b->vals[0]);
double *xpos;
int cols = A->cols;
for (int i = 0; i < A->rows; i++) {
xpos = &(x->vals[0]);
sum = 0.0;
for (int j = 0; j < cols; j++) {
sum += (*Apos++) * (*xpos++);
}
*bpos = sum;
*bpos++;
}
}
void cvxmat_multAx22(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
int size = A->rows;
double *Apos1 = &(A->vals[0]);
double *Apos2 = &(A->vals[size]);
double *ypos = &(b->vals[0]);
for(int i=0; i<size/2; i++)
{
double ytemp1 = 0;
double ytemp2 = 0;
double *xpos = &(x->vals[0]);
for(int j=0; j<size; j++)
{
ytemp1 += (*Apos1++) * (*xpos);
ytemp2 += (*Apos2++) * (*xpos);
xpos++;
}
*ypos = ytemp1;
ypos++;
*ypos = ytemp2;
ypos++;
// skip next row
Apos1 += size;
Apos2 += size;
}
}
void cvxmat_multAx23(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
int size = A->rows;
double *Apos1 = &(A->vals[0]);
double *Apos2 = &(A->vals[size]);
double *ypos = &(b->vals[0]);
for(int i=0; i<size/2; i++)
{
register double ytemp1 = 0;
register double ytemp2 = 0;
register double *xpos = &(x->vals[0]);
int blocksize = size / 8;
double x0 = xpos[0];
double x1 = xpos[1];
for(int j=0; j<blocksize; j++)
{
ytemp1 += x0 * (Apos1[0]);
ytemp2 += x0 * (Apos2[0]);
x0 = xpos[2];
ytemp1 += x1 * (Apos1[1]);
ytemp2 += x1 * (Apos2[1]);
x1 = xpos[3];
ytemp1 += x0 * (Apos1[2]);
ytemp2 += x0 * (Apos2[2]);
x0 = xpos[4];
ytemp1 += x1 * (Apos1[3]);
ytemp2 += x1 * (Apos2[3]);
x1 = xpos[5];
ytemp1 += x0 * (Apos1[4]);
ytemp2 += x0 * (Apos2[4]);
x0 = xpos[6];
ytemp1 += x1 * (Apos1[5]);
ytemp2 += x1 * (Apos2[5]);
x1 = xpos[7];
xpos+=8;
ytemp1 += x0 * (Apos1[6]);
ytemp2 += x0 * (Apos2[6]);
x0 = xpos[0];
ytemp1 += x1 * (Apos1[7]);
Apos1+=8;
ytemp2 += x1 * (Apos2[7]);
x1 = xpos[1];
Apos2+=8;
}
ypos[0] = ytemp1;
ypos[1] = ytemp2;
ypos+=2;
// skip next row
Apos1 += size;
Apos2 += size;
}
}
void cvxmat_multAx4(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
int size = A->rows;
register double ytemp1 = 0;
register double ytemp2 = 0;
register double *xpos = &(x->vals[0]);
int blocksize = size / 8;
double x0 = xpos[0];
double x1 = xpos[1];
double *ypos = &(b->vals[0]);
double *Apos1 = &(A->vals[0]);
double *Apos2 = &(A->vals[size]);
for(int j=0; j<blocksize; j++)
{
ytemp1 += x0 * (Apos1[0]);
ytemp2 += x0 * (Apos2[0]);
x0 = xpos[2];
ytemp1 += x1 * (Apos1[1]);
ytemp2 += x1 * (Apos2[1]);
x1 = xpos[3];
ytemp1 += x0 * (Apos1[2]);
ytemp2 += x0 * (Apos2[2]);
x0 = xpos[4];
ytemp1 += x1 * (Apos1[3]);
ytemp2 += x1 * (Apos2[3]);
x1 = xpos[5];
ytemp1 += x0 * (Apos1[4]);
ytemp2 += x0 * (Apos2[4]);
x0 = xpos[6];
ytemp1 += x1 * (Apos1[5]);
ytemp2 += x1 * (Apos2[5]);
x1 = xpos[7];
xpos+=8;
ytemp1 += x0 * (Apos1[6]);
ytemp2 += x0 * (Apos2[6]);
x0 = xpos[0];
ytemp1 += x1 * (Apos1[7]);
Apos1+=8;
ytemp2 += x1 * (Apos2[7]);
x1 = xpos[1];
Apos2+=8;
}
ypos[0] = ytemp1;
ypos[1] = ytemp2;
ypos+=2;
// skip next row
Apos1 += size;
Apos2 += size;
}
void cvxmat_multAtx(cvx_mat *b, cvx_mat *A, cvx_mat *x)
{
double sum;
for (int i = 0; i < A->cols; i++) {
sum = 0.0;
for (int j = 0; j < A->rows; j++) {
sum += A->vals[A->rows * j + i] * x->vals[j];
}
b->vals[i] = sum;
}
}
void cvxmat_subractMatMult1(cvx_mat *mat0, double v0, cvx_mat *mat1, cvx_mat *mat2)
{
for (int i = 0; i < mat0->N; i++) {
mat0->vals[i] = v0 * mat1->vals[i] - mat2->vals[i];
}
}
void cvxmat_updateG(cvx_mat *G, double relax, cvx_mat *xbar)
{
// G=p*xbar+(1-p)*G;
for (int i = 0; i < G->N; i++) {
G->vals[i] = relax * xbar->vals[i] + (1.0-relax) * G->vals[i];
}
}
void cvxmat_setvals(cvx_mat *mat, double val)
{
for (int i = 0; i < mat->N; i++) {
mat->vals[i] = val;
}
}
int copyNewMatrix(cvx_mat *mat_in, cvx_mat *mat_out)
{
cvxmat_alloc(mat_out, mat_in->rows, mat_in->cols);
int N = mat_out->rows * mat_out->cols;
for (int i = 0; i < N; i++) {
mat_out->vals[i] = mat_in->vals[i];
}
return 1;
}
/*
int main (void)
{
printf ("In matrix.c main function\n");
cvx_mat mat;
allocMatrix(&mat, 100, 100);
printf ("Matrix rows = %d, cols = %d\n", mat.rows, mat.cols);
cvx_mat mat2;
allocMatrix(&mat2, 1000000000, 1000000000);
printf ("Matrix rows = %d, cols = %d\n", mat2.rows, mat2.cols);
return 0;
}
*/