squiggle.c/squiggle.h

51 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 sample_unit_uniform(uint32_t* seed);
float sample_unit_normal(uint32_t* seed);
// Composite distribution sampling functions
float sample_uniform(float from, float to, uint32_t* seed);
float sample_normal(float mean, float sigma, uint32_t* seed);
float sample_lognormal(float logmean, float logsigma, uint32_t* seed);
float sample_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);
float array_mean(float* array, int length);
float array_std(float* array, int length);
// Mixture function
float sample_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