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 <limits.h> // INT_MAX
#include <math.h> // erf, sqrt
#include <float.h> // FLT_MAX, FLT_MIN
#include <limits.h> // INT_MAX
#include <math.h> // erf, sqrt
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -106,10 +106,10 @@ struct box incbeta(float a, float b, float x)
if (symmetric_incbeta.empty) {
return symmetric_incbeta; // propagate error
} else {
struct box result = {
.empty = 0,
.content = 1 - symmetric_incbeta.content
};
struct box result = {
.empty = 0,
.content = 1 - symmetric_incbeta.content
};
return result;
}
}
@ -149,10 +149,10 @@ struct box incbeta(float a, float b, float x)
/*Check for stop.*/
if (fabs(1.0 - cd) < STOP_BETA) {
struct box result = {
.empty = 0,
.content = front * (f - 1.0)
};
struct box result = {
.empty = 0,
.content = front * (f - 1.0)
};
return result;
}
}
@ -175,7 +175,7 @@ struct box cdf_beta(float x)
}
// Inverse cdf at point
// Two versions of this function:
// Two versions of this function:
// - raw, dealing with cdfs that return floats
// - 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;
int mid_not_new = (mid == low) || (mid == high);
// float width = high - low;
// if ((width < 1e-8) || mid_not_new){
// if ((width < 1e-8) || mid_not_new){
if (mid_not_new) {
convergence_condition = 1;
} else {
@ -235,12 +235,11 @@ struct box inverse_cdf_float(float cdf(float), float p)
}
if (convergence_condition) {
struct box result = {.empty = 0, .content = low};
return result;
struct box result = { .empty = 0, .content = low };
return result;
} else {
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)) {
// ^ Using FLT_MIN and FLT_MAX is overkill
// but it's also the *correct* thing to do.
struct box cdf_low = cdf_box(low);
if(cdf_low.empty){
PROCESS_ERROR(cdf_low.error_msg);
}
struct box cdf_low = cdf_box(low);
if (cdf_low.empty) {
PROCESS_ERROR(cdf_low.error_msg);
}
struct box cdf_high=cdf_box(high);
if(cdf_high.empty){
PROCESS_ERROR(cdf_low.error_msg);
}
struct box cdf_high = cdf_box(high);
if (cdf_high.empty) {
PROCESS_ERROR(cdf_low.error_msg);
}
int low_condition = (cdf_low.content < p);
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){
convergence_condition = 1;
} else {
struct box cdf_mid = cdf_box(mid);
if(cdf_mid.empty){
PROCESS_ERROR(cdf_mid.error_msg);
}
struct box cdf_mid = cdf_box(mid);
if (cdf_mid.empty) {
PROCESS_ERROR(cdf_mid.error_msg);
}
float mid_sign = cdf_mid.content - p;
if (mid_sign < 0) {
low = mid;
@ -312,12 +311,11 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p)
}
if (convergence_condition) {
struct box result = {.empty = 0, .content = low};
return result;
struct box result = { .empty = 0, .content = low };
return result;
} else {
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
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);
if (result.empty) {
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 {
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);
if (result.empty) {
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 {
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);
clock_t begin = clock();
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);
}
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);
clock_t begin = clock();
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()
{
// Test inverse cdf float
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_normal_0_1", cdf_normal_0_1);
// Test inverse cdf float
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_normal_0_1", cdf_normal_0_1);
// Test inverse cdf box
test_inverse_cdf_box("cdf_beta", cdf_beta);
// Testing samplers
// Test inverse cdf box
test_inverse_cdf_box("cdf_beta", cdf_beta);
// Testing samplers
// set randomness seed
uint32_t* seed = malloc(sizeof(uint32_t));
*seed = 1000; // xorshift can't start with 0
// Test float sampler
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_normal_0_1", cdf_normal_0_1, seed);
// Get some normal samples using a previous approach
printf("\nGetting some samples from sampler_normal_0_1\n");
// Test float sampler
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_normal_0_1", cdf_normal_0_1, seed);
// Get some normal samples using a previous approach
printf("\nGetting some samples from sampler_normal_0_1\n");
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);
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;
printf("Time spent: %f\n", time_spent_2);
// Test box sampler
test_and_time_sampler_box("cdf_beta", cdf_beta, seed);
// Test box sampler
test_and_time_sampler_box("cdf_beta", cdf_beta, seed);
free(seed);
free(seed);
return 0;
}