squiggle.c/squiggle.h

58 lines
2.2 KiB
C
Raw Normal View History

#ifndef SQUIGGLEC
#define SQUIGGLEC
2023-06-26 17:44:41 +00:00
// uint32_t header
#include <stdint.h>
2023-06-26 17:44:41 +00:00
// Macros
#define EXIT_ON_ERROR 0
#define MAX_ERROR_LENGTH 500
#define PROCESS_ERROR(...) \
do { \
if (EXIT_ON_ERROR) { \
printf("@, in %s (%d)", __FILE__, __LINE__); \
exit(1); \
} else { \
char error_msg[MAX_ERROR_LENGTH]; \
snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", __FILE__, __LINE__); \
struct box error = { .empty = 1, .error_msg = error_msg }; \
return error; \
} \
} while (0)
2023-06-26 17:44:41 +00:00
// Pseudo Random number generator
uint32_t xorshift32(uint32_t* seed);
2023-06-26 17:44:41 +00:00
// Distribution & sampling functions
float rand_0_to_1(uint32_t* seed);
float rand_float(float max, uint32_t* seed);
float unit_normal(uint32_t* seed);
float random_uniform(float from, float to, uint32_t* seed);
float random_normal(float mean, float sigma, uint32_t* seed);
float random_lognormal(float logmean, float logsigma, uint32_t* seed);
float random_to(float low, float high, uint32_t* seed);
2023-06-26 17:44:41 +00:00
// Array helpers
float array_sum(float* array, int length);
void array_cumsum(float* array_to_sum, float* array_cumsummed, int length);
2023-06-26 17:44:41 +00:00
// Mixture function
float mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed);
2023-06-26 17:44:41 +00:00
// Box
struct box {
int empty;
float content;
char* error_msg;
};
// Inverse cdf
struct box inverse_cdf_float(float cdf(float), float p);
struct box inverse_cdf_box(struct box cdf_box(float), float p);
// Samplers from cdf
struct box sampler_box_cdf(struct box cdf(float), uint32_t* seed);
struct box sampler_float_cdf(float cdf(float), uint32_t* seed);
#endif