aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index ba0a090..d20bc26 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -218,6 +218,26 @@ static struct {
{100,363.73937555556347},
};
+double trapz(const double *y, double dx, size_t n)
+{
+ /* Returns the integral of `y` using the trapezoidal rule assuming a
+ * constant grid spacing `dx`.
+ *
+ * See https://en.wikipedia.org/wiki/Trapezoidal_rule. */
+ size_t i;
+ double sum = 0.0;
+
+ if (n < 2) return 0.0;
+
+ sum = y[0];
+ for (i = 1; i < n-1; i++) {
+ sum += 2*y[i];
+ }
+ sum += y[n-1];
+
+ return sum*dx/2.0;
+}
+
void get_path_length(double *pos1, double *pos2, double R, double *l1, double *l2)
{
/* Returns the path length inside and outside a circle of radius `R` for a