summaryrefslogtreecommitdiff
path: root/src/approx_derivative.c
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-11 10:04:04 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-11 10:04:04 -0600
commit43f06890e2689af2ef54c4480fe5790692a24f65 (patch)
treeb933f3e05aad81d780c0c94646676efa1bbad22d /src/approx_derivative.c
parenta74a732b27fb610133190e89a91b2d42d0cf78b3 (diff)
downloadcmath-43f06890e2689af2ef54c4480fe5790692a24f65.tar.gz
cmath-43f06890e2689af2ef54c4480fe5790692a24f65.zip
deprecate common lisp solutions and write c; it's too much effort to keep up with the requirements for an archive.
Diffstat (limited to 'src/approx_derivative.c')
-rw-r--r--src/approx_derivative.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/approx_derivative.c b/src/approx_derivative.c
new file mode 100644
index 0000000..b33a208
--- /dev/null
+++ b/src/approx_derivative.c
@@ -0,0 +1,38 @@
+#include "lizfcm.h"
+#include <assert.h>
+
+double central_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a + h;
+ double x1 = a - h;
+
+ double y2 = (*f)(x2);
+ double y1 = (*f)(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}
+
+double forward_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a + h;
+ double x1 = a;
+
+ double y2 = (*f)(x2);
+ double y1 = (*f)(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}
+
+double backward_derivative_at(double (*f)(double), double a, double h) {
+ assert(h > 0);
+
+ double x2 = a;
+ double x1 = a - h;
+
+ double y2 = (*f)(x2);
+ double y1 = (*f)(x1);
+
+ return (y2 - y1) / (x2 - x1);
+}