squiggle.c/squiggle.h

50 lines
1.5 KiB
C

#ifndef SQUIGGLEC
#define SQUIGGLEC
// uint32_t header
#include <stdint.h>
// Pseudo Random number generator
uint32_t xorshift32(uint32_t* seed);
// Basic 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);
// Composite distribution sampling functions
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);
// Array helpers
float array_sum(float* array, int length);
void array_cumsum(float* array_to_sum, float* array_cumsummed, int length);
// Mixture function
float mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed);
// Box
struct box {
int empty;
float content;
char* error_msg;
};
// Macros to handle errors
#define MAX_ERROR_LENGTH 500
#define EXIT_ON_ERROR 0
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
struct box process_error(const char* error_msg, int should_exit, char* file, int line);
// 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_cdf_float(float cdf(float), uint32_t* seed);
struct box sampler_cdf_box(struct box cdf(float), uint32_t* seed);
#endif