From 79dfcf79db6734e3a672f8a8c39650b7a8ecc5ee Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 23 Sep 2023 20:55:18 +0100 Subject: [PATCH] use named structs in small algebra system. --- README.md | 8 +++++++- squiggle.c | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 289c183..73041d6 100644 --- a/README.md +++ b/README.md @@ -341,4 +341,10 @@ It emits one warning about something I already took care of, so by default I've - [x] Have some more complicated & realistic example - [x] Add summarization functions: 90% ci (or all c.i.?) - [x] Link to the examples in the examples section. -- [x] Add a few functions for doing simple algebra on normals, and lognormals? +- [x] Add a few functions for doing simple algebra on normals, and lognormals + - [x] Add prototypes + - [x] Use named structs + - [ ] Test results + - [ ] Provide example + - [ ] Add to headers + - [ ] Move to own file diff --git a/squiggle.c b/squiggle.c index 517591d..a5b11bf 100644 --- a/squiggle.c +++ b/squiggle.c @@ -437,44 +437,44 @@ struct c_i get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* se } // Do algebra over lognormals and normals -struct normal_parameters { +typedef struct { double mean; double std; -}; +} normal_parameters; -struct lognormal_parameters { +struct { double logmean; double logstd; -}; +} lognormal_parameters; -struct normal_parameters algebra_sum_normals(struct normal_parameters a, struct normal_parameters b) +normal_parameters algebra_sum_normals(normal_parameters a, normal_parameters b) { - struct normal_parameters result = { + normal_parameters result = { .mean = a.mean + b.mean, .std = sqrt((a.std * a.std) + (b.std * b.std)), }; return result; } -struct normal_parameters algebra_shift_normal(struct normal_parameters a, double shift) +normal_parameters algebra_shift_normal(normal_parameters a, double shift) { - struct normal_parameters result = { + normal_parameters result = { .mean = a.mean + shift, .std = a.std, }; return result; } -struct lognormal_parameters algebra_product_lognormals(struct lognormal_parameters a, struct lognormal_parameters b) +lognormal_parameters algebra_product_lognormals(lognormal_parameters a, lognormal_parameters b) { - struct lognormal_parameters result = { + lognormal_parameters result = { .logmean = a.logmean + b.logmean, .logstd = sqrt((a.logstd * a.logstd) + (b.logstd * b.logstd)), }; return result; } -struct lognormal_parameters algebra_scale_lognormal(struct lognormal_parameters a, double k) +lognormal_parameters algebra_scale_lognormal(lognormal_parameters a, double k) { - struct lognormal_parameters result = { + lognormal_parameters result = { .logmean = a.logmean + k, .logstd = a.logstd, };