diff options
author | tlatorre <tlatorre@uchicago.edu> | 2018-09-04 16:51:41 -0500 |
---|---|---|
committer | tlatorre <tlatorre@uchicago.edu> | 2018-09-04 16:51:41 -0500 |
commit | 93115467947f00f4714d1358a030267b28acaa7a (patch) | |
tree | ce2fe50c4a96888b2e69d4b611fc81ca8368d5a7 /src/misc.c | |
parent | f06d6c8c5a16f1305a855e640a784983ab2a2474 (diff) | |
download | sddm-93115467947f00f4714d1358a030267b28acaa7a.tar.gz sddm-93115467947f00f4714d1358a030267b28acaa7a.tar.bz2 sddm-93115467947f00f4714d1358a030267b28acaa7a.zip |
add a function to return the kahan sum of an array
For some reason the fit seems to have trouble with the kinetic energy.
Basically, it seems to "converge" even though when you run the minimization
again it finds a better minimum with a lower energy. I think this is likely due
to the fact that for muons the kinetic energy only really affects the range of
the muon and this is subject to error in the numerical integration.
I also thought that maybe it could be due to roundoff error in the likelihood
calculation, so I implemented the Kahan summation to try and reduce that. No
idea if it's actually improving things, but I should benchmark it later to see.
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -2,6 +2,26 @@ #include <math.h> #include <stdlib.h> /* for size_t */ +double kahan_sum(double *x, size_t n) +{ + /* Returns the sum of the elements of `x` using the Kahan summation algorithm. + * + * See https://en.wikipedia.org/wiki/Kahan_summation_algorithm. */ + size_t i; + double sum, c, y, t; + + sum = 0.0; + c = 0.0; + for (i = 0; i < n; i++) { + y = x[i] - c; + t = sum + y; + c = (t - sum) - y; + sum = t; + } + + return sum; +} + double interp1d(double x, double *xp, double *yp, size_t n) { /* A fast interpolation routine which assumes that the values in `xp` are |