QPALM 1.2.5
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
lin_alg.c
Go to the documentation of this file.
1/**
2 * @file lin_alg.c
3 * @author Ben Hermans
4 * @brief Linear algebra with vectors.
5 * @details Common operations, such as vector products, infinity norm, elementwise
6 * add/product/division/max etc. are included in this file.
7 */
8
9#include <qpalm/lin_alg.h>
10
11c_float* vec_copy(const c_float *a, size_t n) {
12 c_float *b;
13 size_t i;
14
15 b = qpalm_malloc(n * sizeof(c_float));
16
17 for (i = 0; i < n; i++) {
18 b[i] = a[i];
19 }
20
21 return b;
22}
23
24void prea_vec_copy(const c_float *a, c_float *b, size_t n) {
25 size_t i;
26
27 for (i = 0; i < n; i++) {
28 b[i] = a[i];
29 }
30}
31
32void prea_int_vec_copy(const c_int *a, c_int *b, size_t n) {
33 size_t i;
34
35 for (i = 0; i < n; i++) {
36 b[i] = a[i];
37 }
38}
39
40void vec_set_scalar(c_float *a, c_float sc, size_t n) {
41 size_t i;
42
43 for (i = 0; i < n; i++) {
44 a[i] = sc;
45 }
46}
47
48void vec_set_scalar_int(c_int *a, c_int sc, size_t n) {
49 size_t i;
50
51 for (i = 0; i < n; i++) {
52 a[i] = sc;
53 }
54}
55
56void vec_self_mult_scalar(c_float *a, c_float sc, size_t n) {
57 size_t i;
58
59 for (i = 0; i < n; i++) {
60 a[i] *= sc;
61 }
62}
63
64void vec_mult_scalar(const c_float *a, c_float sc, c_float *b, size_t n) {
65 size_t i;
66
67 for (i = 0; i < n; i++) {
68 b[i] = sc*a[i];
69 }
70}
71
72c_float vec_prod(const c_float *a, const c_float *b, size_t n) {
73 c_float prod = 0.0;
74 size_t i = 0;
75
76 if(n >= 4) {
77 for (; i <= n-4; i+=4) {
78 prod += (a[i]*b[i] + a[i+1]*b[i+1] + a[i+2]*b[i+2] + a[i+3]*b[i+3]);
79 }
80 }
81 for (; i < n; i++) {
82 prod += a[i] * b[i];
83 }
84
85 return prod;
86}
87
88c_float vec_norm_two(const c_float *a, size_t n) {
89 return c_sqrt(vec_prod(a, a, n));
90}
91
92void vec_ew_prod(const c_float *a, const c_float *b, c_float *c, size_t n) {
93 size_t i;
94
95 for (i = 0; i < n; i++) {
96 c[i] = a[i] * b[i];
97 }
98}
99
100
101void vec_ew_div(const c_float *a, const c_float *b, c_float *c, size_t n) {
102 size_t i;
103
104 for (i = 0; i < n; i++) {
105 c[i] = a[i] / b[i];
106 }
107}
108
109
110void vec_add_scaled(const c_float *a, const c_float *b, c_float *c, c_float sc, size_t n) {
111 size_t i;
112
113 for (i = 0; i < n; i++) {
114 c[i] = a[i] + sc * b[i];
115 }
116}
117
118void vec_mult_add_scaled(c_float *a, const c_float *b, c_float sc1, c_float sc2, size_t n) {
119 size_t i;
120
121 for (i = 0; i < n; i++) {
122 a[i] = sc1*a[i] + sc2*b[i];
123 }
124}
125
126c_float vec_norm_inf(const c_float *a, size_t n) {
127 register size_t j = 0;
128 register c_float s0 = 0.;
129 register c_float s1 = 0.;
130 register c_float s2 = 0.;
131 register c_float s3 = 0.;
132 register c_float max0 = 0.;
133 register c_float max1 = 0.;
134 register c_float max2 = 0.;
135 register c_float max3 = 0.;
136 const size_t block_size = 4;
137 const size_t block_len = n >> 2;
138 const size_t remaining = n % block_size; /*Initializing four blocks for
139 * efficient implementation of
140 * inner product using registers */
141
142 while (j < block_len * block_size) {
143 s0 = c_absval(a[j]); max0 = s0 > max0 ? s0 : max0;
144 s1 = c_absval(a[j+1]); max1 = s1 > max1 ? s1 : max1;
145 s2 = c_absval(a[j+2]); max2 = s2 > max2 ? s2 : max2;
146 s3 = c_absval(a[j+3]); max3 = s3 > max3 ? s3 : max3;
147 j+=4;
148 }
149
150 max0 = max0 > max1 ? max0 : max1;
151 max0 = max0 > max2 ? max0 : max2;
152 max0 = max0 > max3 ? max0 : max3;
153 j = block_size * block_len;
154 /*Taking contribution from the last terms
155 * that were not included in the block*/
156 switch (remaining) {
157 case 3:
158 max0 = max0 > c_absval(a[j + 2]) ? max0 : c_absval(a[j + 2]);
159 max0 = max0 > c_absval(a[j + 1]) ? max0 : c_absval(a[j + 1]);
160 max0 = max0 > c_absval(a[j]) ? max0 : c_absval(a[j]);
161 break;
162 case 2:
163 max0 = max0 > c_absval(a[j + 1]) ? max0 : c_absval(a[j + 1]);
164 max0 = max0 > c_absval(a[j]) ? max0 : c_absval(a[j]);
165 break;
166 case 1:
167 max0 = max0 > c_absval(a[j]) ? max0 : c_absval(a[j]);
168 break;
169 case 0:
170 break;
171 }
172 return max0;
173
174}
175
176void vec_ew_recipr(const c_float *a, c_float *b, size_t n) {
177 size_t i;
178
179 for (i = 0; i < n; i++) {
180 b[i] = (c_float)1.0 / a[i];
181 }
182}
183
184void vec_ew_max_vec(const c_float *a, const c_float *b, c_float *c, size_t n) {
185 size_t i;
186
187 for (i = 0; i < n; i++) {
188 c[i] = c_max(a[i], b[i]);
189 }
190}
191
192void vec_ew_min_vec(const c_float *a, const c_float *b, c_float *c, size_t n) {
193 size_t i;
194
195 for (i = 0; i < n; i++) {
196 c[i] = c_min(a[i], b[i]);
197 }
198}
199
200void vec_ew_mid_vec(const c_float *a, const c_float *bmin, const c_float *bmax, c_float *c, size_t n) {
201 size_t i;
202
203 for (i = 0; i < n; i++) {
204 c[i] = c_max(bmin[i], c_min(a[i], bmax[i]));
205 }
206}
207
208void vec_ew_sqrt(const c_float *a, c_float *b, size_t n) {
209 size_t i;
210
211 for (i = 0; i < n; i++) {
212 b[i] = c_sqrt(a[i]);
213 }
214}
215
216/* MATRIX FUNCTIONS ----------------------------------------------------------*/
217
218/* Moved to solver_interface.c*/
219
void * qpalm_malloc(size_t size)
Definition global_opts.c:7
#define c_sqrt
square root
ladel_int c_int
type for integer numbers
Definition global_opts.h:42
ladel_double c_float
type for floating point numbers
Definition global_opts.h:41
#define c_max(a, b)
maximum of two values
Definition global_opts.h:96
#define c_absval(x)
absolute value
Definition global_opts.h:92
#define c_min(a, b)
minimum of two values
void vec_add_scaled(const c_float *a, const c_float *b, c_float *c, c_float sc, size_t n)
Scaled addition of one vector to another vector, .
Definition lin_alg.c:110
c_float * vec_copy(const c_float *a, size_t n)
Copy vector a into output.
Definition lin_alg.c:11
void vec_ew_min_vec(const c_float *a, const c_float *b, c_float *c, size_t n)
Elementwise minimum between vectors, .
Definition lin_alg.c:192
void vec_mult_scalar(const c_float *a, c_float sc, c_float *b, size_t n)
Mulitply vector with a constant scale factor and store in a different vector.
Definition lin_alg.c:64
void vec_set_scalar(c_float *a, c_float sc, size_t n)
Fill float vector with a scalar value.
Definition lin_alg.c:40
void vec_ew_max_vec(const c_float *a, const c_float *b, c_float *c, size_t n)
Elementwise maximum between vectors, .
Definition lin_alg.c:184
void vec_ew_div(const c_float *a, const c_float *b, c_float *c, size_t n)
Elementwise division, .
Definition lin_alg.c:101
void vec_set_scalar_int(c_int *a, c_int sc, size_t n)
Fill int vector with a scalar value.
Definition lin_alg.c:48
void vec_mult_add_scaled(c_float *a, const c_float *b, c_float sc1, c_float sc2, size_t n)
Scaled addition of one vector to another vector, both being scaled, .
Definition lin_alg.c:118
void vec_self_mult_scalar(c_float *a, c_float sc, size_t n)
Mulitply vector with a constant scale factor.
Definition lin_alg.c:56
void vec_ew_mid_vec(const c_float *a, const c_float *bmin, const c_float *bmax, c_float *c, size_t n)
Elementwise mid between vectors, .
Definition lin_alg.c:200
void vec_ew_prod(const c_float *a, const c_float *b, c_float *c, size_t n)
Elementwise product, .
Definition lin_alg.c:92
c_float vec_prod(const c_float *a, const c_float *b, size_t n)
Inner product between two vectors, .
Definition lin_alg.c:72
c_float vec_norm_two(const c_float *a, size_t n)
2-norm of a vector, .
Definition lin_alg.c:88
void prea_vec_copy(const c_float *a, c_float *b, size_t n)
Copy vector a into preallocated vector b.
Definition lin_alg.c:24
c_float vec_norm_inf(const c_float *a, size_t n)
Infinity norm of a vector, .
Definition lin_alg.c:126
void prea_int_vec_copy(const c_int *a, c_int *b, size_t n)
Copy integer vector a into preallocated vector b.
Definition lin_alg.c:32
void vec_ew_recipr(const c_float *a, c_float *b, size_t n)
Elementwise reciprocal .
Definition lin_alg.c:176
void vec_ew_sqrt(const c_float *a, c_float *b, size_t n)
Elementwise square root, .
Definition lin_alg.c:208
Linear algebra with vectors.