From edb58bdc1d83a1604bc4ed9a7695333609a8651a Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 3 Dec 2023 18:25:35 +0000 Subject: [PATCH] refactor: struct box => box. Through typedef. --- examples/more/00_example_template/example | Bin 27344 -> 27344 bytes examples/more/01_sample_from_cdf/example | Bin 27624 -> 27624 bytes examples/more/01_sample_from_cdf/example.c | 4 +- examples/more/02_sample_from_cdf_beta/example | Bin 27608 -> 27608 bytes .../more/02_sample_from_cdf_beta/example.c | 22 +++--- examples/more/03_ci_beta/example | Bin 27384 -> 27384 bytes examples/more/04_nuclear_war/example | Bin 27464 -> 27464 bytes examples/more/05_burn_10kg_fat/example | Bin 27424 -> 27424 bytes examples/more/06_nuclear_recovery/example | Bin 27712 -> 27712 bytes examples/more/07_algebra/example | Bin 27344 -> 27344 bytes .../more/08_algebra_and_conversion/example | Bin 27344 -> 27344 bytes examples/more/09_ergonomic_algebra/example | Bin 27344 -> 27344 bytes .../more/10_twitter_thread_example/example | Bin 27496 -> 27496 bytes .../11_billion_lognormals_paralell/example | Bin 27376 -> 27376 bytes .../more/12_time_to_botec_parallel/example | Bin 27544 -> 27544 bytes examples/more/13_parallelize_min/example | Bin 27440 -> 27440 bytes .../more/14_check_confidence_interval/example | Bin 27344 -> 27344 bytes squiggle_more.c | 63 +++++++----------- squiggle_more.h | 14 ++-- 19 files changed, 44 insertions(+), 59 deletions(-) diff --git a/examples/more/00_example_template/example b/examples/more/00_example_template/example index 820272dd4f58c074907317e694b0738d12d5c7af..01a31ff084dc5c79912615c090cb58bd3c5e7084 100755 GIT binary patch delta 103 zcmca`mGQz=#tj+FA_m*Nu0PnhKrX>1ZOQ+=c@aT|NsB_C diff --git a/examples/more/01_sample_from_cdf/example b/examples/more/01_sample_from_cdf/example index 093bbd9cf1fc88e1c8d6678bbf21757049c0474b..2a1377680d5bb0f5adfb2cd08343253158ba7021 100755 GIT binary patch delta 101 zcmaEHo$Ew9VvL{s(&V9` pHzNbXOWFVb|MSbcFfe%Z#;6o{G{51<2mpz*Vv6T&{%!i!6ab#JDYF0o diff --git a/examples/more/01_sample_from_cdf/example.c b/examples/more/01_sample_from_cdf/example.c index 8f17790..f56dd4e 100644 --- a/examples/more/01_sample_from_cdf/example.c +++ b/examples/more/01_sample_from_cdf/example.c @@ -40,7 +40,7 @@ double cdf_normal_0_1(double x) // Some testers void test_inverse_cdf_double(char* cdf_name, double cdf_double(double)) { - struct box result = inverse_cdf_double(cdf_double, 0.5); + box result = inverse_cdf_double(cdf_double, 0.5); if (result.empty) { printf("Inverse for %s not calculated\n", cdf_name); exit(1); @@ -54,7 +54,7 @@ void test_and_time_sampler_double(char* cdf_name, double cdf_double(double), uin printf("\nGetting some samples from %s:\n", cdf_name); clock_t begin = clock(); for (int i = 0; i < NUM_SAMPLES; i++) { - struct box sample = sampler_cdf_double(cdf_double, seed); + box sample = sampler_cdf_double(cdf_double, seed); if (sample.empty) { printf("Error in sampler function for %s", cdf_name); } else { diff --git a/examples/more/02_sample_from_cdf_beta/example b/examples/more/02_sample_from_cdf_beta/example index 7ed3878e51956df97a5526877840c8f0177b1fcb..3fb45c1bed3a54e6bfff1b8aefcee70a0a93436e 100755 GIT binary patch delta 103 zcmca{o$04sc7EXlpC9Cn2n4XMK-TD(_~_dpM2Be rp`kY;1H((%|NsB<%eyczc=X1o6nHeh;m8O8iL+vg=WTv%`PL8sgTN^< diff --git a/examples/more/02_sample_from_cdf_beta/example.c b/examples/more/02_sample_from_cdf_beta/example.c index 46105f5..1449e8d 100644 --- a/examples/more/02_sample_from_cdf_beta/example.c +++ b/examples/more/02_sample_from_cdf_beta/example.c @@ -10,7 +10,7 @@ #define TINY_BETA 1.0e-30 // Incomplete beta function -struct box incbeta(double a, double b, double x) +box incbeta(double a, double b, double x) { // Descended from , // @@ -47,11 +47,11 @@ struct box incbeta(double a, double b, double x) /*The continued fraction converges nicely for x < (a+1)/(a+b+2)*/ if (x > (a + 1.0) / (a + b + 2.0)) { - struct box symmetric_incbeta = incbeta(b, a, 1.0 - x); + box symmetric_incbeta = incbeta(b, a, 1.0 - x); if (symmetric_incbeta.empty) { return symmetric_incbeta; // propagate error } else { - struct box result = { + box result = { .empty = 0, .content = 1 - symmetric_incbeta.content }; @@ -94,7 +94,7 @@ struct box incbeta(double a, double b, double x) /*Check for stop.*/ if (fabs(1.0 - cd) < STOP_BETA) { - struct box result = { + box result = { .empty = 0, .content = front * (f - 1.0) }; @@ -105,13 +105,13 @@ struct box incbeta(double a, double b, double x) return PROCESS_ERROR("More loops needed, did not converge, in function incbeta"); } -struct box cdf_beta(double x) +box cdf_beta(double x) { if (x < 0) { - struct box result = { .empty = 0, .content = 0 }; + box result = { .empty = 0, .content = 0 }; return result; } else if (x > 1) { - struct box result = { .empty = 0, .content = 1 }; + box result = { .empty = 0, .content = 1 }; return result; } else { double successes = 1, failures = (2023 - 1945); @@ -120,9 +120,9 @@ struct box cdf_beta(double x) } // Some testers -void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(double)) +void test_inverse_cdf_box(char* cdf_name, box cdf_box(double)) { - struct box result = inverse_cdf_box(cdf_box, 0.5); + box result = inverse_cdf_box(cdf_box, 0.5); if (result.empty) { printf("Inverse for %s not calculated\n", cdf_name); exit(1); @@ -131,12 +131,12 @@ void test_inverse_cdf_box(char* cdf_name, struct box cdf_box(double)) } } -void test_and_time_sampler_box(char* cdf_name, struct box cdf_box(double), uint64_t* seed) +void test_and_time_sampler_box(char* cdf_name, box cdf_box(double), uint64_t* seed) { printf("\nGetting some samples from %s:\n", cdf_name); clock_t begin = clock(); for (int i = 0; i < NUM_SAMPLES; i++) { - struct box sample = sampler_cdf_box(cdf_box, seed); + box sample = sampler_cdf_box(cdf_box, seed); if (sample.empty) { printf("Error in sampler function for %s", cdf_name); } else { diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example index c014e3d152ec17c815a2abc08ba922f7c1d8e998..43c47356464c614874eb24556946a4c19dfc1a74 100755 GIT binary patch delta 103 zcmexymGQ?_#tj+FA~$!J@U5%&Kh5xVbL9O4S)0ysNzK??!>pvk$hLXDz9ti6;N+8r r4-IV@85my5{{R144M&C(NSqZ@JYn-~BUuXo>z*kp delta 103 zcmexymGQ?_#tj+FBBGpT$4kDbW&5A2%h=rZ=xJ|H6W`_%aPmu& rhlaL{3=A)2|NsBbFYm&@;L#hSQsB}2h9koXB+iN{p0N41sjn#j>INyl delta 103 zcmX?cjq$`a#tj+FBKJxUbBDQJnP+ps@Z=uW`$g7M8KgGXFgxipifrC*q{+k>Kl!D} rLql&y28Nfi|NsBzmv>=c@aT#tj+FB6aT~Hhx-|d@6nZw(7fo5=0r@*MHkw!yKf`$hP^sfhH4U;N+i1 r4-IV@85my5{{R144M&C(NSqZ@JYh4piKsaMA@?ao delta 103 zcmZ2*jd8&>#tj+FBEfTSE@$FucVawb*Zg6|Wxr|SeF~dvn1gf~MK+%|&}3qapZwG4 rp`kY;1H((%|NsB<%eyczc=X1o6nHeh;m8O8iL+vg=WXUT5j6(@I)o;% diff --git a/examples/more/06_nuclear_recovery/example b/examples/more/06_nuclear_recovery/example index 7844d96cc7a2427e768527d2f2766f77bac4e314..1c9db110c6e3e70e566bb048e2d8df3a77ad8f99 100755 GIT binary patch delta 103 zcmX?bgYm!(#tj+FBC;`?wTv#`oh@#|5Lskk5u30@xp8w1bCVt;+vfj`$t#T> q8rm{4Fuaug|NlR~ybA+^M{kTufk*QjjtnP|I4h=j!sgw^qUHe6rzN`p delta 103 zcmca`mGQz=#tj+FBIh?bo6Tjc5dFH>WBE=0@b1FJicFhpn3;4LMK;$PXfiRzPhM&C r(9oNaf#Id>|NsB_=c@aT}n1G$15n) delta 103 zcmca`mGQz=#tj+FB4UY0w>~TjPu(nfse0d^Uo+PjNT+SCVZNlpD6-k#K$D3vesZPJ rLql&y28Nfi|NsBzmv>=c@aT>}n1G>N_bj diff --git a/examples/more/10_twitter_thread_example/example b/examples/more/10_twitter_thread_example/example index 422c236301bb67142e4b5338a413b4a97abf34ef..35a2a73cc93651201f1f8b7a483fe5be542c4146 100755 GIT binary patch delta 103 zcmaEHjq$}b#tj+FBHLek7|Wm7>e#p-C*o#wPr*|yfmNGpn3;4L**4c3XfiPdPF`vB r(9o8Vf#Id>|NsB_^{1AEh_fFf-{gifpbo&}3qapS;rO rp`kY;1H((%|NsB<%eyczc=X1o6nHeh;m8O8iL+vg=WX6?yw)55=AS9G diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example index 7ca1dcbecdcf77379af52fd6012488ed16611b62..8caec3ec8f7674f85711c0a7672a5cf753e70868 100755 GIT binary patch delta 110 zcmexxmGQ$>#tj+FBKij}OkzBBmcb`YYGvPt)-1!?gHJctFdOMGvTa_kugSz1IQgdG yLql6e28Nfi|NsBzmv>=c@aT#tj+FA}%Snv)+puN6Sv$x%83Ci&Zae<;yqMFdOMGifmr5ugSz1Kl!HN yLql&y28Nfi|NsBzmv>=c@aT#tj+FB1eMRCLBpF_qoobc)&ca!fm0y$mh*9%t^Y8Y@6>JXfiPdPUbXz zXlTpG!0=M`|NsB|@-7Sv9=$Ot1s=_BI5M0-;;fkB37e%&LNr`V7>W~f3vyE94fTvo rjLc!Y+{C;}pn$P4Odu__9K<((@{8h&Qj1G-N`PV}2Aem>JmmubI>0_~ delta 177 zcmbPnopHu>#tj+FBK@t?XYe~V%yBYRpO|NsB_H_Ny8{k3=RU)@nywYi2lNRN?i^Lax}CdR2kZ;VQTNAnww3@4B{E2enDW^Pk8by-7(;>6s7oYeT-%)I#g YwD`i({F2Pnyb?WQ6BE=c@aTXuT6hv6+D;Y4SMnQamkzqg{GTpn1gf}MK+(;*JNUhpZwGC rp`kY;1H((%|NsB<%eyczc=X1o6nHeh;m8O8iL+vg=WXUTK57mCxnd~q diff --git a/squiggle_more.c b/squiggle_more.c index 720fdcc..e4d7ca0 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -11,14 +11,14 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples) { - // Division terminology: - // a = b * quotient + reminder - // a = (a/b)*b + (a%b) + // Terms of the division: + // a = b * quotient + reminder + // a = b * (a/b) + (a%b) // dividend: a // divisor: b - // quotient = a / b - // reminder = a % b - // "divisor's multiple" := (a/b)*b + // quotient = a/b + // reminder = a%b + // "divisor's multiple" := b*(a/b) // now, we have n_samples and n_threads // to make our life easy, each thread will have a number of samples of: a/b (quotient) @@ -26,11 +26,9 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ // to possibly do by Jorge: improve so that the remainder is included in the threads int quotient = n_samples / n_threads; - /* int remainder = n_samples % n_threads; // not used, comment to avoid lint warning */ int divisor_multiple = quotient * n_threads; uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); - // printf("UINT64_MAX: %lu\n", UINT64_MAX); srand(1); for (uint64_t i = 0; i < n_threads; i++) { seeds[i] = malloc(sizeof(uint64_t)); @@ -53,7 +51,6 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ for (i = 0; i < n_threads; i++) { int lower_bound_inclusive = i * quotient; int upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below, - // printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound); for (int j = lower_bound_inclusive; j < upper_bound_not_inclusive; j++) { results[j] = sampler(seeds[i]); } @@ -71,9 +68,6 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ } /* Get confidence intervals, given a sampler */ -// Not in core yet because I'm not sure how much I like the struct -// and the built-in 100k samples -// to do: add n to function parameters and document typedef struct ci_t { double low; @@ -89,9 +83,7 @@ static void swp(int i, int j, double xs[]) static int partition(int low, int high, double xs[], int length) { - // To understand this function: - // - see the note after gt variable definition - // - go to commit 578bfa27 and the scratchpad/ folder in it, which has printfs sprinkled throughout + // Note: the scratchpad/ folder in commit 578bfa27 has printfs sprinkled throughout int pivot = low + floor((high - low) / 2); double pivot_value = xs[pivot]; swp(pivot, high, xs); @@ -145,9 +137,6 @@ ci array_get_90_ci(double xs[], int n) ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed) { double* xs = malloc(n * sizeof(double)); - /*for (int i = 0; i < n; i++) { - xs[i] = sampler(seed); - }*/ sampler_parallel(sampler, xs, 16, n); ci result = array_get_ci(interval, xs, n); free(xs); @@ -159,9 +148,6 @@ ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed) } /* Algebra manipulations */ -// here I discover named structs, -// which mean that I don't have to be typing -// struct blah all the time. #define NORMAL90CONFIDENCE 1.6448536269514727 @@ -221,13 +207,13 @@ ci convert_lognormal_params_to_ci(lognormal_params y) #define EXIT_ON_ERROR 0 #define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) -struct box { +typedef struct box_t { int empty; double content; char* error_msg; -}; +} box; -struct box process_error(const char* error_msg, int should_exit, char* file, int line) +box process_error(const char* error_msg, int should_exit, char* file, int line) { if (should_exit) { printf("@, in %s (%d)", file, line); @@ -235,7 +221,7 @@ struct box process_error(const char* error_msg, int should_exit, char* file, int } else { char error_msg[MAX_ERROR_LENGTH]; snprintf(error_msg, MAX_ERROR_LENGTH, "@, in %s (%d)", file, line); // NOLINT: We are being carefull here by considering MAX_ERROR_LENGTH explicitly. - struct box error = { .empty = 1, .error_msg = error_msg }; + box error = { .empty = 1, .error_msg = error_msg }; return error; } } @@ -244,7 +230,7 @@ struct box process_error(const char* error_msg, int should_exit, char* file, int // Version #1: // - input: (cdf: double => double, p) // - output: Box(number|error) -struct box inverse_cdf_double(double cdf(double), double p) +box inverse_cdf_double(double cdf(double), double p) { // given a cdf: [-Inf, Inf] => [0,1] // returns a box with either @@ -299,7 +285,7 @@ struct box inverse_cdf_double(double cdf(double), double p) } if (convergence_condition) { - struct box result = { .empty = 0, .content = low }; + box result = { .empty = 0, .content = low }; return result; } else { return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); @@ -310,7 +296,7 @@ struct box inverse_cdf_double(double cdf(double), double p) // Version #2: // - input: (cdf: double => Box(number|error), p) // - output: Box(number|error) -struct box inverse_cdf_box(struct box cdf_box(double), double p) +box inverse_cdf_box(box cdf_box(double), double p) { // given a cdf: [-Inf, Inf] => Box([0,1]) // returns a box with either @@ -326,12 +312,12 @@ struct box inverse_cdf_box(struct box cdf_box(double), double 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); + box cdf_low = cdf_box(low); if (cdf_low.empty) { return PROCESS_ERROR(cdf_low.error_msg); } - struct box cdf_high = cdf_box(high); + box cdf_high = cdf_box(high); if (cdf_high.empty) { return PROCESS_ERROR(cdf_low.error_msg); } @@ -361,7 +347,7 @@ struct box inverse_cdf_box(struct box cdf_box(double), double p) // if ((width < 1e-8) || mid_not_new){ convergence_condition = 1; } else { - struct box cdf_mid = cdf_box(mid); + box cdf_mid = cdf_box(mid); if (cdf_mid.empty) { return PROCESS_ERROR(cdf_mid.error_msg); } @@ -378,7 +364,7 @@ struct box inverse_cdf_box(struct box cdf_box(double), double p) } if (convergence_condition) { - struct box result = { .empty = 0, .content = low }; + box result = { .empty = 0, .content = low }; return result; } else { return PROCESS_ERROR("Search process did not converge, in function inverse_cdf"); @@ -389,22 +375,22 @@ struct box inverse_cdf_box(struct box cdf_box(double), double p) /* Sample from an arbitrary cdf */ // Before: invert an arbitrary cdf at a point // Now: from an arbitrary cdf, get a sample -struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed) +box sampler_cdf_box(box cdf(double), uint64_t* seed) { double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_box(cdf, p); + box result = inverse_cdf_box(cdf, p); return result; } -struct box sampler_cdf_double(double cdf(double), uint64_t* seed) +box sampler_cdf_double(double cdf(double), uint64_t* seed) { double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_double(cdf, p); + box result = inverse_cdf_double(cdf, p); return result; } -double sampler_cdf_danger(struct box cdf(double), uint64_t* seed) +double sampler_cdf_danger(box cdf(double), uint64_t* seed) { double p = sample_unit_uniform(seed); - struct box result = inverse_cdf_box(cdf, p); + box result = inverse_cdf_box(cdf, p); if (result.empty) { exit(1); } else { @@ -413,7 +399,6 @@ double sampler_cdf_danger(struct box cdf(double), uint64_t* seed) } /* array print: potentially useful for debugging */ - void array_print(double xs[], int n) { printf("["); diff --git a/squiggle_more.h b/squiggle_more.h index 947b164..2f88349 100644 --- a/squiggle_more.h +++ b/squiggle_more.h @@ -32,23 +32,23 @@ lognormal_params convert_ci_to_lognormal_params(ci x); ci convert_lognormal_params_to_ci(lognormal_params y); /* Error handling */ -struct box { +typedef struct box_t { int empty; double content; char* error_msg; -}; +} box; #define MAX_ERROR_LENGTH 500 #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); +box process_error(const char* error_msg, int should_exit, char* file, int line); void array_print(double* array, int length); /* Inverse cdf */ -struct box inverse_cdf_double(double cdf(double), double p); -struct box inverse_cdf_box(struct box cdf_box(double), double p); +box inverse_cdf_double(double cdf(double), double p); +box inverse_cdf_box(box cdf_box(double), double p); /* Samplers from cdf */ -struct box sampler_cdf_double(double cdf(double), uint64_t* seed); -struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed); +box sampler_cdf_double(double cdf(double), uint64_t* seed); +box sampler_cdf_box(box cdf(double), uint64_t* seed); #endif