scratchpad formatting pass

This commit is contained in:
NunoSempere 2023-07-16 17:33:58 +02:00
parent 487de4a731
commit 65756a359b

View File

@ -1,6 +1,6 @@
#include <float.h> // FLT_MAX, FLT_MIN #include <float.h> // FLT_MAX, FLT_MIN
#include <limits.h> // INT_MAX #include <limits.h> // INT_MAX
#include <math.h> // erf, sqrt #include <math.h> // erf, sqrt
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -106,10 +106,10 @@ struct box incbeta(float a, float b, float x)
if (symmetric_incbeta.empty) { if (symmetric_incbeta.empty) {
return symmetric_incbeta; // propagate error return symmetric_incbeta; // propagate error
} else { } else {
struct box result = { struct box result = {
.empty = 0, .empty = 0,
.content = 1 - symmetric_incbeta.content .content = 1 - symmetric_incbeta.content
}; };
return result; return result;
} }
} }
@ -149,10 +149,10 @@ struct box incbeta(float a, float b, float x)
/*Check for stop.*/ /*Check for stop.*/
if (fabs(1.0 - cd) < STOP_BETA) { if (fabs(1.0 - cd) < STOP_BETA) {
struct box result = { struct box result = {
.empty = 0, .empty = 0,
.content = front * (f - 1.0) .content = front * (f - 1.0)
}; };
return result; return result;
} }
} }
@ -175,7 +175,7 @@ struct box cdf_beta(float x)
} }
// Inverse cdf at point // Inverse cdf at point
// Two versions of this function: // Two versions of this function:
// - raw, dealing with cdfs that return floats // - raw, dealing with cdfs that return floats
// - box, dealing with cdfs that return a box. // - box, dealing with cdfs that return a box.
@ -218,7 +218,7 @@ struct box inverse_cdf_float(float cdf(float), float p)
float mid = (high + low) / 2; float mid = (high + low) / 2;
int mid_not_new = (mid == low) || (mid == high); int mid_not_new = (mid == low) || (mid == high);
// float width = high - low; // float width = high - low;
// if ((width < 1e-8) || mid_not_new){ // if ((width < 1e-8) || mid_not_new){
if (mid_not_new) { if (mid_not_new) {
convergence_condition = 1; convergence_condition = 1;
} else { } else {
@ -235,12 +235,11 @@ struct box inverse_cdf_float(float cdf(float), float p)
} }
if (convergence_condition) { if (convergence_condition) {
struct box result = {.empty = 0, .content = low}; struct box result = { .empty = 0, .content = low };
return result; return result;
} else { } else {
PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
} }
} }
} }
@ -260,15 +259,15 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p)
while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) {
// ^ Using FLT_MIN and FLT_MAX is overkill // ^ Using FLT_MIN and FLT_MAX is overkill
// but it's also the *correct* thing to do. // but it's also the *correct* thing to do.
struct box cdf_low = cdf_box(low); struct box cdf_low = cdf_box(low);
if(cdf_low.empty){ if (cdf_low.empty) {
PROCESS_ERROR(cdf_low.error_msg); PROCESS_ERROR(cdf_low.error_msg);
} }
struct box cdf_high=cdf_box(high); struct box cdf_high = cdf_box(high);
if(cdf_high.empty){ if (cdf_high.empty) {
PROCESS_ERROR(cdf_low.error_msg); PROCESS_ERROR(cdf_low.error_msg);
} }
int low_condition = (cdf_low.content < p); int low_condition = (cdf_low.content < p);
int high_condition = (p < cdf_high.content); int high_condition = (p < cdf_high.content);
@ -295,10 +294,10 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p)
// if ((width < 1e-8) || mid_not_new){ // if ((width < 1e-8) || mid_not_new){
convergence_condition = 1; convergence_condition = 1;
} else { } else {
struct box cdf_mid = cdf_box(mid); struct box cdf_mid = cdf_box(mid);
if(cdf_mid.empty){ if (cdf_mid.empty) {
PROCESS_ERROR(cdf_mid.error_msg); PROCESS_ERROR(cdf_mid.error_msg);
} }
float mid_sign = cdf_mid.content - p; float mid_sign = cdf_mid.content - p;
if (mid_sign < 0) { if (mid_sign < 0) {
low = mid; low = mid;
@ -312,12 +311,11 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p)
} }
if (convergence_condition) { if (convergence_condition) {
struct box result = {.empty = 0, .content = low}; struct box result = { .empty = 0, .content = low };
return result; return result;
} else { } else {
PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); PROCESS_ERROR("Search process did not converge, in function inverse_cdf");
} }
} }
} }
@ -371,7 +369,8 @@ float sampler_normal_0_1(uint32_t* seed)
} }
// Some testers // Some testers
void test_inverse_cdf_float(char* cdf_name, float cdf_float(float)){ void test_inverse_cdf_float(char* cdf_name, float cdf_float(float))
{
struct box result = inverse_cdf_float(cdf_float, 0.5); struct box result = inverse_cdf_float(cdf_float, 0.5);
if (result.empty) { if (result.empty) {
printf("Inverse for %s not calculated\n", cdf_name); printf("Inverse for %s not calculated\n", cdf_name);
@ -379,9 +378,9 @@ void test_inverse_cdf_float(char* cdf_name, float cdf_float(float)){
} else { } else {
printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content); printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content);
} }
} }
void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(float)){ void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(float))
{
struct box result = inverse_cdf_box(cdf_box, 0.5); struct box result = inverse_cdf_box(cdf_box, 0.5);
if (result.empty) { if (result.empty) {
printf("Inverse for %s not calculated\n", cdf_name); printf("Inverse for %s not calculated\n", cdf_name);
@ -389,10 +388,10 @@ void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(float)){
} else { } else {
printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content); printf("Inverse of %s at %f is: %f\n", cdf_name, 0.5, result.content);
} }
} }
void test_and_time_sampler_float(char* cdf_name, float cdf_float(float), uint32_t* seed){ void test_and_time_sampler_float(char* cdf_name, float cdf_float(float), uint32_t* seed)
{
printf("\nGetting some samples from %s:\n", cdf_name); printf("\nGetting some samples from %s:\n", cdf_name);
clock_t begin = clock(); clock_t begin = clock();
for (int i = 0; i < NUM_SAMPLES; i++) { for (int i = 0; i < NUM_SAMPLES; i++) {
@ -408,7 +407,8 @@ void test_and_time_sampler_float(char* cdf_name, float cdf_float(float), uint32_
printf("Time spent: %f\n", time_spent); printf("Time spent: %f\n", time_spent);
} }
void test_and_time_sampler_box(char* cdf_name, struct box cdf_box(float), uint32_t* seed){ void test_and_time_sampler_box(char* cdf_name, struct box cdf_box(float), uint32_t* seed)
{
printf("\nGetting some samples from %s:\n", cdf_name); printf("\nGetting some samples from %s:\n", cdf_name);
clock_t begin = clock(); clock_t begin = clock();
for (int i = 0; i < NUM_SAMPLES; i++) { for (int i = 0; i < NUM_SAMPLES; i++) {
@ -426,41 +426,41 @@ void test_and_time_sampler_box(char* cdf_name, struct box cdf_box(float), uint32
int main() int main()
{ {
// Test inverse cdf float // Test inverse cdf float
test_inverse_cdf_float("cdf_uniform_0_1", cdf_uniform_0_1); test_inverse_cdf_float("cdf_uniform_0_1", cdf_uniform_0_1);
test_inverse_cdf_float("cdf_squared_0_1", cdf_squared_0_1); test_inverse_cdf_float("cdf_squared_0_1", cdf_squared_0_1);
test_inverse_cdf_float("cdf_normal_0_1", cdf_normal_0_1); test_inverse_cdf_float("cdf_normal_0_1", cdf_normal_0_1);
// Test inverse cdf box // Test inverse cdf box
test_inverse_cdf_box("cdf_beta", cdf_beta); test_inverse_cdf_box("cdf_beta", cdf_beta);
// Testing samplers // Testing samplers
// set randomness seed // set randomness seed
uint32_t* seed = malloc(sizeof(uint32_t)); uint32_t* seed = malloc(sizeof(uint32_t));
*seed = 1000; // xorshift can't start with 0 *seed = 1000; // xorshift can't start with 0
// Test float sampler // Test float sampler
test_and_time_sampler_float("cdf_uniform_0_1", cdf_uniform_0_1, seed); test_and_time_sampler_float("cdf_uniform_0_1", cdf_uniform_0_1, seed);
test_and_time_sampler_float("cdf_squared_0_1", cdf_squared_0_1, seed); test_and_time_sampler_float("cdf_squared_0_1", cdf_squared_0_1, seed);
test_and_time_sampler_float("cdf_normal_0_1", cdf_normal_0_1, seed); test_and_time_sampler_float("cdf_normal_0_1", cdf_normal_0_1, seed);
// Get some normal samples using a previous approach // Get some normal samples using a previous approach
printf("\nGetting some samples from sampler_normal_0_1\n"); printf("\nGetting some samples from sampler_normal_0_1\n");
clock_t begin_2 = clock(); clock_t begin_2 = clock();
for (int i = 0; i < NUM_SAMPLES; i++) { for (int i = 0; i < NUM_SAMPLES; i++) {
float normal_sample = sampler_normal_0_1(seed); float normal_sample = sampler_normal_0_1(seed);
printf("%f\n", normal_sample); printf("%f\n", normal_sample);
} }
clock_t end_2 = clock(); clock_t end_2 = clock();
float time_spent_2 = (float)(end_2 - begin_2) / CLOCKS_PER_SEC; float time_spent_2 = (float)(end_2 - begin_2) / CLOCKS_PER_SEC;
printf("Time spent: %f\n", time_spent_2); printf("Time spent: %f\n", time_spent_2);
// Test box sampler // Test box sampler
test_and_time_sampler_box("cdf_beta", cdf_beta, seed); test_and_time_sampler_box("cdf_beta", cdf_beta, seed);
free(seed); free(seed);
return 0; return 0;
} }