From 37510bc23ab8ba5266998c15fb48c869ef7ca796 Mon Sep 17 00:00:00 2001 From: tlatorre Date: Fri, 31 Aug 2018 10:05:03 -0500 Subject: rotate and translate the path in path_init to speed things up --- src/misc.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 74c729e..6a67d58 100644 --- a/src/misc.c +++ b/src/misc.c @@ -2,6 +2,30 @@ #include #include /* for size_t */ +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 + * math.isclose() function. + * + * See https://www.python.org/dev/peps/pep-0485/. */ + return fabs(a-b) <= fmax(rel_tol*fmax(fabs(a),fabs(b)),abs_tol); +} + +int allclose(double *a, double *b, size_t n, double rel_tol, double abs_tol) +{ + /* Returns 1 if all the elements of a and b are "close". This algorithm is + * taken from Python's math.isclose() function. + * + * See https://www.python.org/dev/peps/pep-0485/. */ + size_t i; + + for (i = 0; i < n; i++) { + if (!isclose(a[i],b[i],rel_tol,abs_tol)) return 0; + } + + return 1; +} + double logsumexp(double *a, size_t n) { /* Returns the log of the sum of the exponentials of the array `a`. -- cgit