forked from osqp/osqp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlin_alg.c
412 lines (327 loc) · 8.34 KB
/
lin_alg.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
#include "lin_alg.h"
/* VECTOR FUNCTIONS ----------------------------------------------------------*/
void vec_add_scaled(c_float *c,
const c_float *a,
const c_float *b,
c_int n,
c_float sc) {
c_int i;
for (i = 0; i < n; i++) {
c[i] = a[i] + sc * b[i];
}
}
c_float vec_scaled_norm_inf(const c_float *S, const c_float *v, c_int l) {
c_int i;
c_float abs_Sv_i;
c_float max = 0.0;
for (i = 0; i < l; i++) {
abs_Sv_i = c_absval(S[i] * v[i]);
if (abs_Sv_i > max) max = abs_Sv_i;
}
return max;
}
c_float vec_norm_inf(const c_float *v, c_int l) {
c_int i;
c_float abs_v_i;
c_float max = 0.0;
for (i = 0; i < l; i++) {
abs_v_i = c_absval(v[i]);
if (abs_v_i > max) max = abs_v_i;
}
return max;
}
c_float vec_norm_inf_diff(const c_float *a, const c_float *b, c_int l) {
c_float nmDiff = 0.0, tmp;
c_int i;
for (i = 0; i < l; i++) {
tmp = c_absval(a[i] - b[i]);
if (tmp > nmDiff) nmDiff = tmp;
}
return nmDiff;
}
c_float vec_mean(const c_float *a, c_int n) {
c_float mean = 0.0;
c_int i;
for (i = 0; i < n; i++) {
mean += a[i];
}
mean /= (c_float)n;
return mean;
}
void int_vec_set_scalar(c_int *a, c_int sc, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
a[i] = sc;
}
}
void vec_set_scalar(c_float *a, c_float sc, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
a[i] = sc;
}
}
void vec_add_scalar(c_float *a, c_float sc, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
a[i] += sc;
}
}
void vec_mult_scalar(c_float *a, c_float sc, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
a[i] *= sc;
}
}
#ifndef EMBEDDED
c_float* vec_copy(c_float *a, c_int n) {
c_float *b;
c_int i;
b = c_malloc(n * sizeof(c_float));
for (i = 0; i < n; i++) {
b[i] = a[i];
}
return b;
}
#endif // end EMBEDDED
void prea_int_vec_copy(const c_int *a, c_int *b, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
b[i] = a[i];
}
}
void prea_vec_copy(const c_float *a, c_float *b, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
b[i] = a[i];
}
}
void vec_ew_recipr(const c_float *a, c_float *b, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
b[i] = (c_float)1.0 / a[i];
}
}
c_float vec_prod(const c_float *a, const c_float *b, c_int n) {
c_float prod = 0.0;
c_int i; // Index
for (i = 0; i < n; i++) {
prod += a[i] * b[i];
}
return prod;
}
void vec_ew_prod(const c_float *a, const c_float *b, c_float *c, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
c[i] = b[i] * a[i];
}
}
#if EMBEDDED != 1
void vec_ew_sqrt(c_float *a, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
a[i] = c_sqrt(a[i]);
}
}
void vec_ew_max(c_float *a, c_int n, c_float max_val) {
c_int i;
for (i = 0; i < n; i++) {
a[i] = c_max(a[i], max_val);
}
}
void vec_ew_min(c_float *a, c_int n, c_float min_val) {
c_int i;
for (i = 0; i < n; i++) {
a[i] = c_min(a[i], min_val);
}
}
void vec_ew_max_vec(const c_float *a, const c_float *b, c_float *c, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
c[i] = c_max(a[i], b[i]);
}
}
void vec_ew_min_vec(const c_float *a, const c_float *b, c_float *c, c_int n) {
c_int i;
for (i = 0; i < n; i++) {
c[i] = c_min(a[i], b[i]);
}
}
#endif // EMBEDDED != 1
/* MATRIX FUNCTIONS ----------------------------------------------------------*/
/* multiply scalar to matrix */
void mat_mult_scalar(csc *A, c_float sc) {
c_int i, nnzA;
nnzA = A->p[A->n];
for (i = 0; i < nnzA; i++) {
A->x[i] *= sc;
}
}
void mat_premult_diag(csc *A, const c_float *d) {
c_int j, i;
for (j = 0; j < A->n; j++) { // Cycle over columns
for (i = A->p[j]; i < A->p[j + 1]; i++) { // Cycle every row in the column
A->x[i] *= d[A->i[i]]; // Scale by corresponding element
// of d for row i
}
}
}
void mat_postmult_diag(csc *A, const c_float *d) {
c_int j, i;
for (j = 0; j < A->n; j++) { // Cycle over columns j
for (i = A->p[j]; i < A->p[j + 1]; i++) { // Cycle every row i in column j
A->x[i] *= d[j]; // Scale by corresponding element
// of d for column j
}
}
}
void mat_vec(const csc *A, const c_float *x, c_float *y, c_int plus_eq) {
c_int i, j;
if (!plus_eq) {
// y = 0
for (i = 0; i < A->m; i++) {
y[i] = 0;
}
}
// if A is empty
if (A->p[A->n] == 0) {
return;
}
if (plus_eq == -1) {
// y -= A*x
for (j = 0; j < A->n; j++) {
for (i = A->p[j]; i < A->p[j + 1]; i++) {
y[A->i[i]] -= A->x[i] * x[j];
}
}
} else {
// y += A*x
for (j = 0; j < A->n; j++) {
for (i = A->p[j]; i < A->p[j + 1]; i++) {
y[A->i[i]] += A->x[i] * x[j];
}
}
}
}
void mat_tpose_vec(const csc *A, const c_float *x, c_float *y,
c_int plus_eq, c_int skip_diag) {
c_int i, j, k;
if (!plus_eq) {
// y = 0
for (i = 0; i < A->n; i++) {
y[i] = 0;
}
}
// if A is empty
if (A->p[A->n] == 0) {
return;
}
if (plus_eq == -1) {
// y -= A*x
if (skip_diag) {
for (j = 0; j < A->n; j++) {
for (k = A->p[j]; k < A->p[j + 1]; k++) {
i = A->i[k];
y[j] -= i == j ? 0 : A->x[k] * x[i];
}
}
} else {
for (j = 0; j < A->n; j++) {
for (k = A->p[j]; k < A->p[j + 1]; k++) {
y[j] -= A->x[k] * x[A->i[k]];
}
}
}
} else {
// y += A*x
if (skip_diag) {
for (j = 0; j < A->n; j++) {
for (k = A->p[j]; k < A->p[j + 1]; k++) {
i = A->i[k];
y[j] += i == j ? 0 : A->x[k] * x[i];
}
}
} else {
for (j = 0; j < A->n; j++) {
for (k = A->p[j]; k < A->p[j + 1]; k++) {
y[j] += A->x[k] * x[A->i[k]];
}
}
}
}
}
#if EMBEDDED != 1
void mat_inf_norm_cols(const csc *M, c_float *E) {
c_int j, ptr;
// Initialize zero max elements
for (j = 0; j < M->n; j++) {
E[j] = 0.;
}
// Compute maximum across columns
for (j = 0; j < M->n; j++) {
for (ptr = M->p[j]; ptr < M->p[j + 1]; ptr++) {
E[j] = c_max(c_absval(M->x[ptr]), E[j]);
}
}
}
void mat_inf_norm_rows(const csc *M, c_float *E) {
c_int i, j, ptr;
// Initialize zero max elements
for (j = 0; j < M->m; j++) {
E[j] = 0.;
}
// Compute maximum across rows
for (j = 0; j < M->n; j++) {
for (ptr = M->p[j]; ptr < M->p[j + 1]; ptr++) {
i = M->i[ptr];
E[i] = c_max(c_absval(M->x[ptr]), E[i]);
}
}
}
void mat_inf_norm_cols_sym_triu(const csc *M, c_float *E) {
c_int i, j, ptr;
c_float abs_x;
// Initialize zero max elements
for (j = 0; j < M->n; j++) {
E[j] = 0.;
}
// Compute maximum across columns
// Note that element (i, j) contributes to
// -> Column j (as expected in any matrices)
// -> Column i (which is equal to row i for symmetric matrices)
for (j = 0; j < M->n; j++) {
for (ptr = M->p[j]; ptr < M->p[j + 1]; ptr++) {
i = M->i[ptr];
abs_x = c_absval(M->x[ptr]);
E[j] = c_max(abs_x, E[j]);
if (i != j) {
E[i] = c_max(abs_x, E[i]);
}
}
}
}
#endif /* if EMBEDDED != 1 */
c_float quad_form(const csc *P, const c_float *x) {
c_float quad_form = 0.;
c_int i, j, ptr; // Pointers to iterate over
// matrix: (i,j) a element
// pointer
for (j = 0; j < P->n; j++) { // Iterate over columns
for (ptr = P->p[j]; ptr < P->p[j + 1]; ptr++) { // Iterate over rows
i = P->i[ptr]; // Row index
if (i == j) { // Diagonal element
quad_form += (c_float).5 * P->x[ptr] * x[i] * x[i];
}
else if (i < j) { // Off-diagonal element
quad_form += P->x[ptr] * x[i] * x[j];
}
else { // Element in lower diagonal
// part
#ifdef PRINTING
c_eprint("quad_form matrix is not upper triangular");
#endif /* ifdef PRINTING */
return OSQP_NULL;
}
}
}
return quad_form;
}