squiggle.c/squiggle.h

54 lines
1.6 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
// Pseudo Random number generator
uint32_t xorshift32(uint32_t* seed);
2023-06-26 17:44:41 +00:00
// 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);
2023-06-26 17:44:41 +00:00
2023-07-23 08:09:34 +00:00
float sample_gamma(float alpha, uint32_t* seed);
float sample_beta(float a, float b, 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-07-22 17:36:43 +00:00
float array_mean(float* array, int length);
float array_std(float* array, int length);
2023-06-26 17:44:41 +00:00
// Mixture function
float sample_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;
};
// Macros to handle errors
#define MAX_ERROR_LENGTH 500
2023-07-16 20:58:20 +00:00
#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