diff --git a/examples/01_one_sample/example b/examples/01_one_sample/example index 57b6267..90e4a1f 100755 Binary files a/examples/01_one_sample/example and b/examples/01_one_sample/example differ diff --git a/examples/02_many_samples/example b/examples/02_many_samples/example index 1f69607..ba3d735 100755 Binary files a/examples/02_many_samples/example and b/examples/02_many_samples/example differ diff --git a/examples/03_gcc_nested_function/example b/examples/03_gcc_nested_function/example index b912c36..5858d5f 100755 Binary files a/examples/03_gcc_nested_function/example and b/examples/03_gcc_nested_function/example differ diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index 57601d6..51d92ed 100755 Binary files a/scratchpad/scratchpad and b/scratchpad/scratchpad differ diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index d49fe56..3a91fa6 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -4,7 +4,7 @@ #include #include #include -#include +// #include #include #define EXIT_ON_ERROR 0 diff --git a/squiggle.c b/squiggle.c index 0ca98e2..0c6d638 100644 --- a/squiggle.c +++ b/squiggle.c @@ -3,29 +3,29 @@ #include // PI constant -const float PI = M_PI;// 3.14159265358979323846; +const float PI = M_PI; // 3.14159265358979323846; // Pseudo Random number generator -uint32_t xorshift32 -(uint32_t* seed) +uint32_t xorshift32(uint32_t* seed) { - // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" - // See - // https://en.wikipedia.org/wiki/Xorshift - // Also some drama: , + // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" + // See + // https://en.wikipedia.org/wiki/Xorshift + // Also some drama: , - uint32_t x = *seed; - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - return *seed = x; + uint32_t x = *seed; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return *seed = x; } // Distribution & sampling functions -float rand_0_to_1(uint32_t* seed){ - return ((float) xorshift32(seed)) / ((float) UINT32_MAX); +float rand_0_to_1(uint32_t* seed) +{ + return ((float)xorshift32(seed)) / ((float)UINT32_MAX); } float rand_float(float max, uint32_t* seed) @@ -33,7 +33,7 @@ float rand_float(float max, uint32_t* seed) return rand_0_to_1(seed) * max; } -float ur_normal(uint32_t* seed) +float unit_normal(uint32_t* seed) { float u1 = rand_0_to_1(seed); float u2 = rand_0_to_1(seed); @@ -48,7 +48,7 @@ float random_uniform(float from, float to, uint32_t* seed) float random_normal(float mean, float sigma, uint32_t* seed) { - return (mean + sigma * ur_normal(seed)); + return (mean + sigma * unit_normal(seed)); } float random_lognormal(float logmean, float logsigma, uint32_t* seed) @@ -90,24 +90,25 @@ float mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint3 // You can see a simpler version of this function in the git history // or in C-02-better-algorithm-one-thread/ float sum_weights = array_sum(weights, n_dists); - float* cumsummed_normalized_weights = (float*) malloc(n_dists * sizeof(float)); - cumsummed_normalized_weights[0] = weights[0]/sum_weights; + float* cumsummed_normalized_weights = (float*)malloc(n_dists * sizeof(float)); + cumsummed_normalized_weights[0] = weights[0] / sum_weights; for (int i = 1; i < n_dists; i++) { - cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i]/sum_weights; + cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i] / sum_weights; } - float result; - int result_set_flag = 0; - float p = random_uniform(0, 1, seed); - for (int k = 0; k < n_dists; k++) { - if (p < cumsummed_normalized_weights[k]) { - result = samplers[k](seed); - result_set_flag = 1; - break; - } - } - if(result_set_flag == 0) result = samplers[n_dists-1](seed); + float result; + int result_set_flag = 0; + float p = random_uniform(0, 1, seed); + for (int k = 0; k < n_dists; k++) { + if (p < cumsummed_normalized_weights[k]) { + result = samplers[k](seed); + result_set_flag = 1; + break; + } + } + if (result_set_flag == 0) + result = samplers[n_dists - 1](seed); - free(cumsummed_normalized_weights); - return result; + free(cumsummed_normalized_weights); + return result; }