[issue3366] Add gamma and error functions to math module

nirinA raseliarison report at bugs.python.org
Mon Jul 28 19:04:20 CEST 2008

nirinA raseliarison <nirina at mail.blueline.mg> added the comment:

i wrote pure Python equivalent of the patch and will
post it to the ASPN cookbook as suggested. i'm wondering
if i have to upload the code here too, or just the link
will suffice?

Raymond Hettinger:
> Can you also implement blending of approximations:
> (1-t)*f1(x) + t*f2(x)

i do this with the Python code for now, and my question is:
how to choose the values for the borders?

with erf, for example, one has typically 5 domains
of approximation and one can write something like:

    def erf(x):
        f = float(x)
        if (f == inf):
            return 1.0
        elif (f == -inf):
            return -1.0
        elif (f == nan):
            return nan
            if (abs(x)<0.84375):
                return erf1(x)
            elif (0.84375<=abs(x)<1.25):
                return erf2(x)
            elif (1.25<=abs(x)<2.857142):
                return erf3(x)
            elif (2.857142<=abs(x)<6):
                return erf4(x)
            elif (abs(x)>=6):
                return erf5(x)

implementing the blending of approximations,
one may write:

    def blend(x, a, b, f1, f2):
        if x < a:
            return f1(x)
        if x > b:
            return f2(x)
        return (((b-x)*f1(x))-((a-x)*f2(x)))/(b-a)

and the definition becomes:

    def erf(x):
	if (abs(x)<0.83):
		return erf1(x)
	elif (0.83<=abs(x)<0.85):
		return blend(x,0.83,0.85,erf1,erf2)
	elif (0.85<=abs(x)<1.24):
		return erf2(x)
	elif (1.24<=abs(x)<1.26):
		return blend(x,1.24,1.26,erf2,erf3)
	elif (1.26<=abs(x)<2.84):
		return erf3(x)
	elif (2.84<=abs(x)<2.86):
		return blend(x,2.84,2.86,erf3,erf4)
	elif (2.86<=abs(x)<5.99):
		return erf4(x)
	elif (5.99<=abs(x)<6.01):
		return blend(x,5.99,6.01,erf4,erf5)
	elif (abs(x)>=6.01):
		return erf5(x)

but the choice of these values is somewhat arbitrary.
or i completely misunderstood what you mean by "blending of approximations"!

for erf functions, blending of approximations, as i understand it,
may be implemented easily as the functions are monotonics.
for gammas, this will be a bit complicated, especially for
negative values, where there are extremums for half integers and
discontinuities for integers.
in the other hand, i'm wondering if these coefficients had been chosen
with optimization of discontinuities already taken into account.

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list