Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 1 | #ifndef LIN_ALG_H |
| 2 | # define LIN_ALG_H |
| 3 | |
| 4 | |
| 5 | # ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | # endif // ifdef __cplusplus |
| 8 | |
| 9 | # include "types.h" |
| 10 | |
| 11 | |
| 12 | /* VECTOR FUNCTIONS ----------------------------------------------------------*/ |
| 13 | |
| 14 | # ifndef EMBEDDED |
| 15 | |
| 16 | /* copy vector a into output (Uses MALLOC)*/ |
| 17 | c_float* vec_copy(c_float *a, |
| 18 | c_int n); |
| 19 | # endif // ifndef EMBEDDED |
| 20 | |
| 21 | /* copy vector a into preallocated vector b */ |
| 22 | void prea_vec_copy(const c_float *a, |
| 23 | c_float *b, |
| 24 | c_int n); |
| 25 | |
| 26 | /* copy integer vector a into preallocated vector b */ |
| 27 | void prea_int_vec_copy(const c_int *a, |
| 28 | c_int *b, |
| 29 | c_int n); |
| 30 | |
| 31 | /* set float vector to scalar */ |
| 32 | void vec_set_scalar(c_float *a, |
| 33 | c_float sc, |
| 34 | c_int n); |
| 35 | |
| 36 | /* set integer vector to scalar */ |
| 37 | void int_vec_set_scalar(c_int *a, |
| 38 | c_int sc, |
| 39 | c_int n); |
| 40 | |
| 41 | /* add scalar to vector*/ |
| 42 | void vec_add_scalar(c_float *a, |
| 43 | c_float sc, |
| 44 | c_int n); |
| 45 | |
| 46 | /* multiply scalar to vector */ |
| 47 | void vec_mult_scalar(c_float *a, |
| 48 | c_float sc, |
| 49 | c_int n); |
| 50 | |
| 51 | /* c = a + sc*b */ |
| 52 | void vec_add_scaled(c_float *c, |
| 53 | const c_float *a, |
| 54 | const c_float *b, |
| 55 | c_int n, |
| 56 | c_float sc); |
| 57 | |
| 58 | /* ||v||_inf */ |
| 59 | c_float vec_norm_inf(const c_float *v, |
| 60 | c_int l); |
| 61 | |
| 62 | /* ||Sv||_inf */ |
| 63 | c_float vec_scaled_norm_inf(const c_float *S, |
| 64 | const c_float *v, |
| 65 | c_int l); |
| 66 | |
| 67 | /* ||a - b||_inf */ |
| 68 | c_float vec_norm_inf_diff(const c_float *a, |
| 69 | const c_float *b, |
| 70 | c_int l); |
| 71 | |
| 72 | /* mean of vector elements */ |
| 73 | c_float vec_mean(const c_float *a, |
| 74 | c_int n); |
| 75 | |
| 76 | # if EMBEDDED != 1 |
| 77 | |
| 78 | /* Vector elementwise reciprocal b = 1./a (needed for scaling)*/ |
| 79 | void vec_ew_recipr(const c_float *a, |
| 80 | c_float *b, |
| 81 | c_int n); |
| 82 | # endif // if EMBEDDED != 1 |
| 83 | |
| 84 | /* Inner product a'b */ |
| 85 | c_float vec_prod(const c_float *a, |
| 86 | const c_float *b, |
| 87 | c_int n); |
| 88 | |
| 89 | /* Elementwise product a.*b stored in c*/ |
| 90 | void vec_ew_prod(const c_float *a, |
| 91 | const c_float *b, |
| 92 | c_float *c, |
| 93 | c_int n); |
| 94 | |
| 95 | # if EMBEDDED != 1 |
| 96 | |
| 97 | /* Elementwise sqrt of the vector elements */ |
| 98 | void vec_ew_sqrt(c_float *a, |
| 99 | c_int n); |
| 100 | |
| 101 | /* Elementwise max between each vector component and max_val */ |
| 102 | void vec_ew_max(c_float *a, |
| 103 | c_int n, |
| 104 | c_float max_val); |
| 105 | |
| 106 | /* Elementwise min between each vector component and max_val */ |
| 107 | void vec_ew_min(c_float *a, |
| 108 | c_int n, |
| 109 | c_float min_val); |
| 110 | |
| 111 | /* Elementwise maximum between vectors c = max(a, b) */ |
| 112 | void vec_ew_max_vec(const c_float *a, |
| 113 | const c_float *b, |
| 114 | c_float *c, |
| 115 | c_int n); |
| 116 | |
| 117 | /* Elementwise minimum between vectors c = min(a, b) */ |
| 118 | void vec_ew_min_vec(const c_float *a, |
| 119 | const c_float *b, |
| 120 | c_float *c, |
| 121 | c_int n); |
| 122 | |
| 123 | # endif // if EMBEDDED != 1 |
| 124 | |
| 125 | |
| 126 | /* MATRIX FUNCTIONS ----------------------------------------------------------*/ |
| 127 | |
| 128 | /* multiply scalar to matrix */ |
| 129 | void mat_mult_scalar(csc *A, |
| 130 | c_float sc); |
| 131 | |
| 132 | /* Premultiply matrix A by diagonal matrix with diagonal d, |
| 133 | i.e. scale the rows of A by d |
| 134 | */ |
| 135 | void mat_premult_diag(csc *A, |
| 136 | const c_float *d); |
| 137 | |
| 138 | /* Premultiply matrix A by diagonal matrix with diagonal d, |
| 139 | i.e. scale the columns of A by d |
| 140 | */ |
| 141 | void mat_postmult_diag(csc *A, |
| 142 | const c_float *d); |
| 143 | |
| 144 | |
| 145 | /* Matrix-vector multiplication |
| 146 | * y = A*x (if plus_eq == 0) |
| 147 | * y += A*x (if plus_eq == 1) |
| 148 | * y -= A*x (if plus_eq == -1) |
| 149 | */ |
| 150 | void mat_vec(const csc *A, |
| 151 | const c_float *x, |
| 152 | c_float *y, |
| 153 | c_int plus_eq); |
| 154 | |
| 155 | |
| 156 | /* Matrix-transpose-vector multiplication |
| 157 | * y = A'*x (if plus_eq == 0) |
| 158 | * y += A'*x (if plus_eq == 1) |
| 159 | * y -= A'*x (if plus_eq == -1) |
| 160 | * If skip_diag == 1, then diagonal elements of A are assumed to be zero. |
| 161 | */ |
| 162 | void mat_tpose_vec(const csc *A, |
| 163 | const c_float *x, |
| 164 | c_float *y, |
| 165 | c_int plus_eq, |
| 166 | c_int skip_diag); |
| 167 | |
| 168 | |
| 169 | # if EMBEDDED != 1 |
| 170 | |
| 171 | /** |
| 172 | * Infinity norm of each matrix column |
| 173 | * @param M Input matrix |
| 174 | * @param E Vector of infinity norms |
| 175 | * |
| 176 | */ |
| 177 | void mat_inf_norm_cols(const csc *M, |
| 178 | c_float *E); |
| 179 | |
| 180 | /** |
| 181 | * Infinity norm of each matrix row |
| 182 | * @param M Input matrix |
| 183 | * @param E Vector of infinity norms |
| 184 | * |
| 185 | */ |
| 186 | void mat_inf_norm_rows(const csc *M, |
| 187 | c_float *E); |
| 188 | |
| 189 | /** |
| 190 | * Infinity norm of each matrix column |
| 191 | * Matrix M is symmetric upper-triangular |
| 192 | * |
| 193 | * @param M Input matrix (symmetric, upper-triangular) |
| 194 | * @param E Vector of infinity norms |
| 195 | * |
| 196 | */ |
| 197 | void mat_inf_norm_cols_sym_triu(const csc *M, |
| 198 | c_float *E); |
| 199 | |
| 200 | # endif // EMBEDDED != 1 |
| 201 | |
| 202 | /** |
| 203 | * Compute quadratic form f(x) = 1/2 x' P x |
| 204 | * @param P quadratic matrix in CSC form (only upper triangular) |
| 205 | * @param x argument float vector |
| 206 | * @return quadratic form value |
| 207 | */ |
| 208 | c_float quad_form(const csc *P, |
| 209 | const c_float *x); |
| 210 | |
| 211 | |
| 212 | # ifdef __cplusplus |
| 213 | } |
| 214 | # endif // ifdef __cplusplus |
| 215 | |
| 216 | #endif // ifndef LIN_ALG_H |