reformat squiggle.c, remake examples.

This commit is contained in:
NunoSempere 2023-07-16 21:08:05 +02:00
parent 8f69dd1e58
commit 68e7730f24
6 changed files with 34 additions and 33 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> // #include <sys/types.h>
#include <time.h> #include <time.h>
#define EXIT_ON_ERROR 0 #define EXIT_ON_ERROR 0

View File

@ -3,12 +3,11 @@
#include <stdlib.h> #include <stdlib.h>
// PI constant // PI constant
const float PI = M_PI;// 3.14159265358979323846; const float PI = M_PI; // 3.14159265358979323846;
// Pseudo Random number generator // Pseudo Random number generator
uint32_t xorshift32 uint32_t xorshift32(uint32_t* seed)
(uint32_t* seed)
{ {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works> // See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>
@ -24,8 +23,9 @@ uint32_t xorshift32
// Distribution & sampling functions // Distribution & sampling functions
float rand_0_to_1(uint32_t* seed){ float rand_0_to_1(uint32_t* seed)
return ((float) xorshift32(seed)) / ((float) UINT32_MAX); {
return ((float)xorshift32(seed)) / ((float)UINT32_MAX);
} }
float rand_float(float max, uint32_t* seed) 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; 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 u1 = rand_0_to_1(seed);
float u2 = 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) 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) float random_lognormal(float logmean, float logsigma, uint32_t* seed)
@ -90,10 +90,10 @@ 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 // You can see a simpler version of this function in the git history
// or in C-02-better-algorithm-one-thread/ // or in C-02-better-algorithm-one-thread/
float sum_weights = array_sum(weights, n_dists); float sum_weights = array_sum(weights, n_dists);
float* cumsummed_normalized_weights = (float*) malloc(n_dists * sizeof(float)); float* cumsummed_normalized_weights = (float*)malloc(n_dists * sizeof(float));
cumsummed_normalized_weights[0] = weights[0]/sum_weights; cumsummed_normalized_weights[0] = weights[0] / sum_weights;
for (int i = 1; i < n_dists; i++) { 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; float result;
@ -106,7 +106,8 @@ float mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint3
break; break;
} }
} }
if(result_set_flag == 0) result = samplers[n_dists-1](seed); if (result_set_flag == 0)
result = samplers[n_dists - 1](seed);
free(cumsummed_normalized_weights); free(cumsummed_normalized_weights);
return result; return result;