squiggle.c/squiggle.h

54 lines
1.6 KiB
C

#ifndef SQUIGGLEC
#define SQUIGGLEC
// uint64_t header
#include <stdint.h>
// Pseudo Random number generator
uint64_t xorshift64(uint64_t* seed);
// Basic distribution sampling functions
float sample_unit_uniform(uint64_t* seed);
float sample_unit_normal(uint64_t* seed);
// Composite distribution sampling functions
float sample_uniform(float start, float end, uint64_t* seed);
float sample_normal(float mean, float sigma, uint64_t* seed);
float sample_lognormal(float logmean, float logsigma, uint64_t* seed);
float sample_to(float low, float high, uint64_t* seed);
float sample_gamma(float alpha, uint64_t* seed);
float sample_beta(float a, float b, uint64_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[])(uint64_t*), float* weights, int n_dists, uint64_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), uint64_t* seed);
struct box sampler_cdf_box(struct box cdf(float), uint64_t* seed);
#endif