aboutsummaryrefslogtreecommitdiffstats
path: root/astro/cosmo_calc/ddivid.cc
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@gmail.com>2016-05-28 09:40:25 +0800
committerAaron LI <aaronly.me@gmail.com>2016-05-28 09:40:25 +0800
commit34ed04686523ecdafb96c6cdff379df176cf1984 (patch)
tree6bdaa0eeb2d397fe2142e6275225a3f71dd69bcd /astro/cosmo_calc/ddivid.cc
parentcbce64c6b79dda6ee5f5155f9754e3495e03d6bd (diff)
downloadatoolbox-34ed04686523ecdafb96c6cdff379df176cf1984.tar.bz2
Add astro/cosmo_calc from "chandra-acis-analysis"
Diffstat (limited to 'astro/cosmo_calc/ddivid.cc')
-rw-r--r--astro/cosmo_calc/ddivid.cc56
1 files changed, 56 insertions, 0 deletions
diff --git a/astro/cosmo_calc/ddivid.cc b/astro/cosmo_calc/ddivid.cc
new file mode 100644
index 0000000..aa72471
--- /dev/null
+++ b/astro/cosmo_calc/ddivid.cc
@@ -0,0 +1,56 @@
+#include <cassert>
+
+double ddivid(double (*foo)(double),double x1,double x2,double err)
+{
+ assert(x2>x1);
+ assert(foo(x2)*foo(x1)<0);
+ while(x2-x1>err)
+ {
+ double x=(x2+x1)/2.;
+ double y=foo(x);
+ double y1=foo(x1);
+ double y2=foo(x2);
+ if(y1*y<0)
+ {
+ x2=x;
+ }
+ else if(y2*y<0)
+ {
+ x1=x;
+ }
+ else
+ {
+ assert(0);
+ }
+
+ }
+ return (x1+x2)/2.;
+}
+
+
+double ddivid(double (*foo)(double),double z,double x1,double x2,double err)
+{
+ assert(x2>x1);
+ assert((foo(x2)-z)*(foo(x1)-z)<0);
+ while(x2-x1>err)
+ {
+ double x=(x2+x1)/2.;
+ double y=foo(x)-z;
+ double y1=foo(x1)-z;
+ double y2=foo(x2)-z;
+ if(y1*y<0)
+ {
+ x2=x;
+ }
+ else if(y2*y<0)
+ {
+ x1=x;
+ }
+ else
+ {
+ assert(0);
+ }
+
+ }
+ return (x1+x2)/2.;
+}