lin_alg.h 5.5 KB

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