use named structs in small algebra system.

This commit is contained in:
NunoSempere 2023-09-23 20:55:18 +01:00
parent a4fdbc1e2c
commit 79dfcf79db
2 changed files with 19 additions and 13 deletions

View File

@ -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

View File

@ -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,
};