aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2018-08-31 10:33:24 -0500
committertlatorre <tlatorre@uchicago.edu>2018-08-31 10:33:24 -0500
commit83119fe9bb0c9c2a70ea5397aca9968bde7ffa07 (patch)
treefbe13f58f3630f31e36fa8722ff41b7570f019c8 /src/misc.c
parentf213df199092781b50115db9fae5afbbbd9b2551 (diff)
downloadsddm-83119fe9bb0c9c2a70ea5397aca9968bde7ffa07.tar.gz
sddm-83119fe9bb0c9c2a70ea5397aca9968bde7ffa07.tar.bz2
sddm-83119fe9bb0c9c2a70ea5397aca9968bde7ffa07.zip
add interp1d function to do fast interpolation when the x values are evenly spaced
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index 6a67d58..2f02595 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -2,6 +2,22 @@
#include <math.h>
#include <stdlib.h> /* for size_t */
+double interp1d(double x, double *xp, double *yp, size_t n)
+{
+ /* A fast interpolation routine which assumes that the values in `xp` are
+ * evenly spaced.
+ *
+ * If x < xp[0] returns yp[0] and if x > xp[n-1] returns yp[n-1]. */
+ size_t i;
+
+ if (x < xp[0]) return yp[0];
+ if (x > xp[n-1]) return yp[n-1];
+
+ i = (x-xp[0])/(xp[1]-xp[0]);
+
+ return yp[i] + (yp[i+1]-yp[i])*(x-xp[i])/(xp[i+1]-xp[i]);
+}
+
int isclose(double a, double b, double rel_tol, double abs_tol)
{
/* Returns 1 if a and b are "close". This algorithm is taken from Python's