diff --git a/src/collections.h b/src/collections.h index 39e6ec7c..9ed57a16 100644 --- a/src/collections.h +++ b/src/collections.h @@ -37,8 +37,8 @@ VECTOR_INIT_NUMERIC(int32_array, int32_t) VECTOR_INIT_NUMERIC(uint32_array, uint32_t) VECTOR_INIT_NUMERIC(int64_array, int64_t) VECTOR_INIT_NUMERIC(uint64_array, uint64_t) -VECTOR_INIT_NUMERIC(float_array, float) -VECTOR_INIT_NUMERIC(double_array, double) +VECTOR_INIT_NUMERIC_FLOAT(float_array, float) +VECTOR_INIT_NUMERIC_FLOAT(double_array, double) VECTOR_INIT(char_array, char) VECTOR_INIT(uchar_array, unsigned char) diff --git a/src/vector_math.h b/src/vector_math.h index c39014e6..81cdd138 100644 --- a/src/vector_math.h +++ b/src/vector_math.h @@ -113,18 +113,6 @@ } \ } \ \ - static inline void name##_log(name *vector, type c, size_t n) { \ - for (int i = 0; i < n; i++) { \ - vector->a[i] += log(vector->a[i]); \ - } \ - } \ - \ - static inline void name##_exp(name *vector, type c, size_t n) { \ - for (int i = 0; i < n; i++) { \ - vector->a[i] += exp(vector->a[i]); \ - } \ - } \ - \ static inline type name##_sum(name *vector, size_t n) { \ type result = 0; \ for (int i = 0; i < n; i++) { \ @@ -141,23 +129,6 @@ return result; \ } \ \ - static inline type name##_log_sum(name *vector, size_t n) { \ - type result = 0; \ - for (int i = 0; i < n; i++) { \ - result += log(vector->a[i]); \ - } \ - return result; \ - } \ - \ - static inline type name##_log_sum_exp(name *vector, size_t n) { \ - type max = name##_max(vector, n); \ - type result = 0; \ - for (int i = 0; i < n; i++) { \ - result += exp(vector->a[i] - max); \ - } \ - return max + log(result); \ - } \ - \ static inline void name##_add_vector(name *v1, name *v2, size_t n) { \ for (int i = 0; i < n; i++) { \ v1->a[i] += v2->a[i]; \ @@ -191,4 +162,38 @@ } + +#define VECTOR_INIT_NUMERIC_FLOAT(name, type) \ + VECTOR_INIT_NUMERIC(name, type) \ + \ + static inline void name##_log(name *vector, type c, size_t n) { \ + for (int i = 0; i < n; i++) { \ + vector->a[i] += log(vector->a[i]); \ + } \ + } \ + \ + static inline void name##_exp(name *vector, type c, size_t n) { \ + for (int i = 0; i < n; i++) { \ + vector->a[i] += exp(vector->a[i]); \ + } \ + } \ + \ \ + static inline type name##_log_sum(name *vector, size_t n) { \ + type result = 0; \ + for (int i = 0; i < n; i++) { \ + result += log(vector->a[i]); \ + } \ + return result; \ + } \ + \ + static inline type name##_log_sum_exp(name *vector, size_t n) { \ + type max = name##_max(vector, n); \ + type result = 0; \ + for (int i = 0; i < n; i++) { \ + result += exp(vector->a[i] - max); \ + } \ + return max + log(result); \ + } + + #endif \ No newline at end of file