separated root finder in util.math
authorForrest Voight <forrest@forre.st>
Tue, 24 Jan 2012 04:42:35 +0000 (23:42 -0500)
committerForrest Voight <forrest@forre.st>
Tue, 24 Jan 2012 04:42:35 +0000 (23:42 -0500)
p2pool/util/math.py

index 745f0ca..5b6aba7 100644 (file)
@@ -95,13 +95,16 @@ def erf(x):
     y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
     return sign*y # erf(-x) = -erf(x)
 
-def ierf(z, steps=10):
+def find_root(f, fp, start, steps=10):
     guess = 0
     for i in xrange(steps):
-        d = 2*math.e**(-guess**2)/math.sqrt(math.pi)
-        guess = guess - (erf(guess) - z)/d
+        d = fp(guess)
+        guess = guess - f(guess)/d
     return guess
 
+def ierf(z):
+    return find_root(lambda x: erf(x) - z, lambda guess: 2*math.e**(-guess**2)/math.sqrt(math.pi), 0)
+
 try:
     from scipy import special
 except ImportError: