From 6247fbfb7bcc3d396f7a29a96b8d6e90ed057849 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 16 Jul 2023 22:58:20 +0200 Subject: [PATCH] simplify PROCESS_ERROR macro --- README.md | 13 +++++- examples/04_sample_from_cdf_simple/example | Bin 22144 -> 22184 bytes examples/05_sample_from_cdf_beta/example | Bin 22176 -> 22208 bytes examples/05_sample_from_cdf_beta/example.c | 4 +- squiggle.c | 45 +++++++++++---------- squiggle.h | 16 ++------ 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 9924e15..b312736 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A self-contained C99 library that provides a subset of [Squiggle](https://www.sq - Because if you can implement something in C, you can implement it anywhere else - Because it can be made faster if need be, e.g., with a multi-threading library like OpenMP, or by adding more algorithmic complexity -## The core scheme +## The core strategy Have some basic building blocks, like , and return samplers. Use previous samplers to . Then use the final sampler to produce an array of samples. @@ -26,6 +26,13 @@ You can follow some example usage in the examples/ folder 4. In the fourth example, we define some simple cdfs, and we draw samples from those cdfs. We see that this approach is slower than using the built-in samplers, e.g., the normal sampler. 5. In the fifth example, we define the cdf for the beta distribution, and we draw samples from it. +## Related projects + +- [Squiggle](https://www.squiggle-language.com/) +- [SquigglePy](https://github.com/rethinkpriorities/squigglepy) +- [time to botec](https://github.com/NunoSempere/time-to-botec) +- [simple squiggle](https://nunosempere.com/blog/2022/04/17/simple-squiggle/) + ## To do list - [ ] Have some more complicated & realistic example @@ -34,9 +41,11 @@ You can follow some example usage in the examples/ folder - Schema: a function which takes a sample and manipulates it, - and at the end, an array of samples. - Explain boxes - - Explain individual examples + - [x] Explain individual examples - Explain nested functions - [ ] Publish online +- [ ] Support all distribution functions in +- [ ] Support all distribution functions in , and do so efficiently ## Done diff --git a/examples/04_sample_from_cdf_simple/example b/examples/04_sample_from_cdf_simple/example index ab05dacc039e0c9e66970ef8d345233a110a471d..4bfb751103d1d53c77c643f044189a47f6c42fc6 100755 GIT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QL6@SQ| zE0f~uaS@1JmqJ_k8)cHY9ecqZ!|%KpnS8vggqM{iTmKl}lsK6v>(ktvJ6f`W9)82FiNARgvO1 z3au7x74HDM9(yzR8ww*{M4O4RkDmwo#e3`^AIHA;-u_K)_Db<@V;{-=GIx8W%#VZ~ zXZP76UbH5~auV`vajI2~G76$Kh}|y>1-+b_TXr94-oN+xT@CvW?LNHgnfiv%vM1sw zFYsmZoxW{vzU!kbM%i=dMUC)>A3RG#&ZoxWXem#qkP~8ej=QVaf({E-$maj~Ka^jt pkdqbd`++Y=6o=wMODn&oRN)C#>l2Q9BROPoZ`FEwmUmRG`VT8hHL3sr diff --git a/examples/05_sample_from_cdf_beta/example b/examples/05_sample_from_cdf_beta/example index 071bd691fe55410009320000e66bcf4a724cc15d..5948358d92bf6a14c2e3d4ac7c304da30900cb14 100755 GIT binary patch delta 4873 zcmZu#3vg3a8os$rYacWxg_b16mJ5WER%jaDD-;?H&8h_!{>Y9Ko~6y z@IQzrlfQXyb*it}`s&_}$1E3Pu2`~mEZx_=DQcHtW<|6h$nCk4(gh(H#QvZjg2Cq$ zMUmTQEDRIlLHt+-o-27T<(@gWy8rhNj$u!|M<`<%>n1n4h^S@D@6Co4LP&B-BF^zAT9 z+GL$Q#MXjr?`vuRyD_>Ma^Ma?>KG77F6-=JR&s*bdzkGMTe5uw{Btti%gWCK|$ri->yd5x98+pUdFhoI4p5m`l6@DH5-;$^=sMB;jGz-|#&t zPl|bCxM6fAOVJ6!)_^YeIBLdPXYu^eWpS@#Kg^6%_VBLJGoN@Pl}^P0UaBs$A8MB1fAovw z*J*h_B(b*4XZGGEcG#!-?g%(eqf7$&EEc?p4dnXvUz8RF4#dJ4Qb?V}>}Q%sF?)&( zhUWN?_m_KuH-{Z+&n?6zbS;)jM9Cp}=W=o*9g=SUE!lrPxIRG;+5(N!sDYsTxW|9) zAPPX`ik<*AA6r%3i(B)l35{5?UjzxMKM|_^Sy5C*2!OnbYzBhG5p0m?s=?nn0?w75 z_8H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ywzyZ zR_$84RVA(t{6v^|wEx&(MZ-qDroS*v5r{+GsaQ-7t z&Z(LI#N4_$4_DW;x$d&?bha;p27MFTKJ>-%B%<@s6Ag;S$9K=?X-J4^EBaMWl(0C3 zjx5X5M^Y4JSW#9i)bjLCWqFB_YK+j6^jMiwUwWI0w)D2PwDK5%ucBGAei66Ht8%S$ PE>^p=t4O2AO0E9|$*YsD diff --git a/examples/05_sample_from_cdf_beta/example.c b/examples/05_sample_from_cdf_beta/example.c index 7fe0fe3..ce023ca 100644 --- a/examples/05_sample_from_cdf_beta/example.c +++ b/examples/05_sample_from_cdf_beta/example.c @@ -42,7 +42,7 @@ struct box incbeta(float a, float b, float x) * 3. This notice may not be removed or altered from any source distribution. */ if (x < 0.0 || x > 1.0) { - PROCESS_ERROR("x out of bounds [0, 1], in function incbeta"); + return PROCESS_ERROR("x out of bounds [0, 1], in function incbeta"); } /*The continued fraction converges nicely for x < (a+1)/(a+b+2)*/ @@ -102,7 +102,7 @@ struct box incbeta(float a, float b, float x) } } - PROCESS_ERROR("More loops needed, did not converge, in function incbeta"); + return PROCESS_ERROR("More loops needed, did not converge, in function incbeta"); } struct box cdf_beta(float x) diff --git a/squiggle.c b/squiggle.c index 7d020e2..13bf4ed 100644 --- a/squiggle.c +++ b/squiggle.c @@ -6,20 +6,9 @@ #include #include -#define EXIT_ON_ERROR 0 #define MAX_ERROR_LENGTH 500 -#define PROCESS_ERROR(...) \ - do { \ - if (EXIT_ON_ERROR) { \ - printf("@, in %s (%d)", __FILE__, __LINE__); \ - exit(1); \ - } else { \ - char error_msg[MAX_ERROR_LENGTH]; \ - snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", __FILE__, __LINE__); \ - struct box error = { .empty = 1, .error_msg = error_msg }; \ - return error; \ - } \ - } while (0) +#define EXIT_ON_ERROR 0 +#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) // PI constant const float PI = 3.14159265358979323846; // M_PI in gcc gnu99 @@ -139,6 +128,18 @@ struct box { char* error_msg; }; +struct box process_error(const char* error_msg, int should_exit, char* file, int line){ + if(should_exit){ + printf("@, in %s (%d)", file, line); + exit(1); + }else{ + char error_msg[MAX_ERROR_LENGTH]; + snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", file, line); + struct box error = { .empty = 1, .error_msg = error_msg }; + return error; + } +} + // Inverse cdf at point // Two versions of this function: // - raw, dealing with cdfs that return floats @@ -176,7 +177,7 @@ struct box inverse_cdf_float(float cdf(float), float p) } if (!interval_found) { - PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); + return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); } else { int convergence_condition = 0; @@ -205,7 +206,7 @@ struct box inverse_cdf_float(float cdf(float), float p) struct box result = { .empty = 0, .content = low }; return result; } else { - PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); + return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); } } } @@ -228,12 +229,12 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) // 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); + return PROCESS_ERROR(cdf_low.error_msg); } struct box cdf_high = cdf_box(high); if (cdf_high.empty) { - PROCESS_ERROR(cdf_low.error_msg); + return PROCESS_ERROR(cdf_low.error_msg); } int low_condition = (cdf_low.content < p); @@ -248,7 +249,7 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) } if (!interval_found) { - PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); + return PROCESS_ERROR("Interval containing the target value not found, in function inverse_cdf"); } else { int convergence_condition = 0; @@ -263,7 +264,7 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) } else { struct box cdf_mid = cdf_box(mid); if (cdf_mid.empty) { - PROCESS_ERROR(cdf_mid.error_msg); + return PROCESS_ERROR(cdf_mid.error_msg); } float mid_sign = cdf_mid.content - p; if (mid_sign < 0) { @@ -281,19 +282,19 @@ struct box inverse_cdf_box(struct box cdf_box(float), float p) struct box result = { .empty = 0, .content = low }; return result; } else { - PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); + return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); } } } // Sampler based on inverse cdf and randomness function -struct box sampler_box_cdf(struct box cdf(float), uint32_t* seed) +struct box sampler_cdf_box(struct box cdf(float), uint32_t* seed) { float p = rand_0_to_1(seed); struct box result = inverse_cdf_box(cdf, p); return result; } -struct box sampler_float_cdf(float cdf(float), uint32_t* seed) +struct box sampler_cdf_float(float cdf(float), uint32_t* seed) { float p = rand_0_to_1(seed); struct box result = inverse_cdf_float(cdf, p); diff --git a/squiggle.h b/squiggle.h index e75b64b..ba703c6 100644 --- a/squiggle.h +++ b/squiggle.h @@ -33,20 +33,10 @@ struct box { }; // Macros to handle errors -#define EXIT_ON_ERROR 0 #define MAX_ERROR_LENGTH 500 -#define PROCESS_ERROR(...) \ - do { \ - if (EXIT_ON_ERROR) { \ - printf("@, in %s (%d)", __FILE__, __LINE__); \ - exit(1); \ - } else { \ - char error_msg[MAX_ERROR_LENGTH]; \ - snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", __FILE__, __LINE__); \ - struct box error = { .empty = 1, .error_msg = error_msg }; \ - return error; \ - } \ - } while (0) +#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);