aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2018-08-31 10:05:03 -0500
committertlatorre <tlatorre@uchicago.edu>2018-08-31 10:05:03 -0500
commit37510bc23ab8ba5266998c15fb48c869ef7ca796 (patch)
tree0b36a60184ef5bebaafeabbdd429cc748be28fca /src/misc.c
parent56e663aa644c5a7c5ca620c3f688cdd7fbefdc7d (diff)
downloadsddm-37510bc23ab8ba5266998c15fb48c869ef7ca796.tar.gz
sddm-37510bc23ab8ba5266998c15fb48c869ef7ca796.tar.bz2
sddm-37510bc23ab8ba5266998c15fb48c869ef7ca796.zip
rotate and translate the path in path_init to speed things up
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c24
1 files changed, 24 insertions, 0 deletions
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 <math.h>
#include <stdlib.h> /* 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`.