source: sasmodels/explore/precision.py @ fba9ca0

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since fba9ca0 was fba9ca0, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

add gammaln and gammainc to the opencl math library

  • Property mode set to 100755
File size: 24.8 KB
RevLine 
[eb2946f]1#!/usr/bin/env python
2r"""
[57eb6a4]3Show numerical precision of various expressions.
4
5Evaluates the same function(s) in single and double precision and compares
6the results to 500 digit mpmath evaluation of the same function.
7
8Note: a quick way to generation C and python code for taylor series
9expansions from sympy:
10
11    import sympy as sp
12    x = sp.var("x")
13    f = sp.sin(x)/x
14    t = sp.series(f, n=12).removeO()  # taylor series with no O(x^n) term
15    p = sp.horner(t)   # Horner representation
16    p = p.replace(x**2, sp.var("xsq")  # simplify if alternate terms are zero
17    p = p.n(15)  # evaluate coefficients to 15 digits (optional)
18    c_code = sp.ccode(p, assign_to=sp.var("p"))  # convert to c code
19    py_code = c[:-1]  # strip semicolon to convert c to python
20
21    # mpmath has pade() rational function approximation, which might work
22    # better than the taylor series for some functions:
23    P, Q = mp.pade(sp.Poly(t.n(15),x).coeffs(), L, M)
24    P = sum(a*x**n for n,a in enumerate(reversed(P)))
25    Q = sum(a*x**n for n,a in enumerate(reversed(Q)))
26    c_code = sp.ccode(sp.horner(P)/sp.horner(Q), assign_to=sp.var("p"))
27
28    # There are richardson and shanks series accelerators in both sympy
29    # and mpmath that may be helpful.
[eb2946f]30"""
31from __future__ import division, print_function
32
33import sys
34import os
35sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
36
37import numpy as np
38from numpy import pi, inf
39import scipy.special
40try:
41    from mpmath import mp
42except ImportError:
43    # CRUFT: mpmath split out into its own package
44    from sympy.mpmath import mp
45#import matplotlib; matplotlib.use('TkAgg')
46import pylab
47
48from sasmodels import core, data, direct_model, modelinfo
49
50class Comparator(object):
51    def __init__(self, name, mp_function, np_function, ocl_function, xaxis, limits):
52        self.name = name
53        self.mp_function = mp_function
54        self.np_function = np_function
55        self.ocl_function = ocl_function
56        self.xaxis = xaxis
57        self.limits = limits
58
59    def __repr__(self):
60        return "Comparator(%s)"%self.name
61
62    def call_mpmath(self, vec, bits=500):
63        """
64        Direct calculation using mpmath extended precision library.
65        """
66        with mp.workprec(bits):
67            return [self.mp_function(mp.mpf(x)) for x in vec]
68
69    def call_numpy(self, x, dtype):
70        """
71        Direct calculation using numpy/scipy.
72        """
73        x = np.asarray(x, dtype)
74        return self.np_function(x)
75
76    def call_ocl(self, x, dtype, platform='ocl'):
77        """
78        Calculation using sasmodels ocl libraries.
79        """
80        x = np.asarray(x, dtype)
81        model = core.build_model(self.ocl_function, dtype=dtype)
82        calculator = direct_model.DirectModel(data.empty_data1D(x), model)
83        return calculator(background=0)
84
[5181ccc]85    def run(self, xrange="log", diff="relative"):
[eb2946f]86        r"""
87        Compare accuracy of different methods for computing f.
88
[5181ccc]89        *xrange* is::
[eb2946f]90
[5181ccc]91            log:    [10^-3,10^5]
92            logq:   [10^-4, 10^1]
93            linear: [1,1000]
94            zoom:   [1000,1010]
95            neg:    [-100,100]
96
[fba9ca0]97        For arbitrary range use "start:stop:steps:scale" where scale is
98        one of log, lin, or linear.
99
[5181ccc]100        *diff* is "relative", "absolute" or "none"
[eb2946f]101
102        *x_bits* is the precision with which the x values are specified.  The
103        default 23 should reproduce the equivalent of a single precisio
104        """
[5181ccc]105        linear = not xrange.startswith("log")
[eb2946f]106        if xrange == "zoom":
[fba9ca0]107            start, stop, steps = 1000, 1010, 2000
[eb2946f]108        elif xrange == "neg":
[fba9ca0]109            start, stop, steps = -100.1, 100.1, 2000
[5181ccc]110        elif xrange == "linear":
[fba9ca0]111            start, stop, steps = 1, 1000, 2000
112            start, stop, steps = 0.001, 2, 2000
[5181ccc]113        elif xrange == "log":
[fba9ca0]114            start, stop, steps = -3, 5, 400
[5181ccc]115        elif xrange == "logq":
[fba9ca0]116            start, stop, steps = -4, 1, 400
117        elif ':' in xrange:
118            parts = xrange.split(':')
119            linear = parts[3] != "log" if len(parts) == 4 else True
120            steps = int(parts[2]) if len(parts) > 2 else 400
121            start = float(parts[0])
122            stop = float(parts[1])
123
[5181ccc]124        else:
125            raise ValueError("unknown range "+xrange)
[eb2946f]126        with mp.workprec(500):
[5181ccc]127            # Note: we make sure that we are comparing apples to apples...
128            # The x points are set using single precision so that we are
129            # examining the accuracy of the transformation from x to f(x)
130            # rather than x to f(nearest(x)) where nearest(x) is the nearest
131            # value to x in the given precision.
[eb2946f]132            if linear:
[fba9ca0]133                start = max(start, self.limits[0])
134                stop = min(stop, self.limits[1])
135                qrf = np.linspace(start, stop, steps, dtype='single')
136                #qrf = np.linspace(start, stop, steps, dtype='double')
[eb2946f]137                qr = [mp.mpf(float(v)) for v in qrf]
[fba9ca0]138                #qr = mp.linspace(start, stop, steps)
[eb2946f]139            else:
[fba9ca0]140                start = np.log10(max(10**start, self.limits[0]))
141                stop = np.log10(min(10**stop, self.limits[1]))
142                qrf = np.logspace(start, stop, steps, dtype='single')
143                #qrf = np.logspace(start, stop, steps, dtype='double')
[eb2946f]144                qr = [mp.mpf(float(v)) for v in qrf]
[fba9ca0]145                #qr = [10**v for v in mp.linspace(start, stop, steps)]
[eb2946f]146
147        target = self.call_mpmath(qr, bits=500)
148        pylab.subplot(121)
149        self.compare(qr, 'single', target, linear, diff)
150        pylab.legend(loc='best')
151        pylab.subplot(122)
152        self.compare(qr, 'double', target, linear, diff)
153        pylab.legend(loc='best')
154        pylab.suptitle(self.name + " compared to 500-bit mpmath")
155
[5181ccc]156    def compare(self, x, precision, target, linear=False, diff="relative"):
[eb2946f]157        r"""
158        Compare the different computation methods using the given precision.
159        """
160        if precision == 'single':
161            #n=11; plotdiff(x, target, self.call_mpmath(x, n), 'mp %d bits'%n, diff=diff)
162            #n=23; plotdiff(x, target, self.call_mpmath(x, n), 'mp %d bits'%n, diff=diff)
163            pass
164        elif precision == 'double':
165            #n=53; plotdiff(x, target, self.call_mpmath(x, n), 'mp %d bits'%n, diff=diff)
166            #n=83; plotdiff(x, target, self.call_mpmath(x, n), 'mp %d bits'%n, diff=diff)
167            pass
168        plotdiff(x, target, self.call_numpy(x, precision), 'numpy '+precision, diff=diff)
169        plotdiff(x, target, self.call_ocl(x, precision, 0), 'OpenCL '+precision, diff=diff)
170        pylab.xlabel(self.xaxis)
[5181ccc]171        if diff == "relative":
[eb2946f]172            pylab.ylabel("relative error")
[5181ccc]173        elif diff == "absolute":
174            pylab.ylabel("absolute error")
[eb2946f]175        else:
176            pylab.ylabel(self.name)
177            pylab.semilogx(x, target, '-', label="true value")
178        if linear:
179            pylab.xscale('linear')
180
[5181ccc]181def plotdiff(x, target, actual, label, diff):
[eb2946f]182    """
183    Plot the computed value.
184
185    Use relative error if SHOW_DIFF, otherwise just plot the value directly.
186    """
[5181ccc]187    if diff == "relative":
[fba9ca0]188        err = np.array([(abs((t-a)/t) if t != 0 else a) for t, a in zip(target, actual)], 'd')
[eb2946f]189        #err = np.clip(err, 0, 1)
190        pylab.loglog(x, err, '-', label=label)
[5181ccc]191    elif diff == "absolute":
192        err = np.array([abs((t-a)) for t, a in zip(target, actual)], 'd')
193        pylab.loglog(x, err, '-', label=label)
[eb2946f]194    else:
195        limits = np.min(target), np.max(target)
196        pylab.semilogx(x, np.clip(actual, *limits), '-', label=label)
197
198def make_ocl(function, name, source=[]):
199    class Kernel(object):
200        pass
201    Kernel.__file__ = name+".py"
202    Kernel.name = name
203    Kernel.parameters = []
204    Kernel.source = source
205    Kernel.Iq = function
206    model_info = modelinfo.make_model_info(Kernel)
207    return model_info
208
[fba9ca0]209# Hack to allow second parameter A in two parameter functions
210A = 1
211def parse_extra_pars():
212    global A
213
214    A_str = str(A)
215    pop = []
216    for k, v in enumerate(sys.argv[1:]):
217        if v.startswith("A="):
218            A_str = v[2:]
219            pop.append(k+1)
220    if pop:
221        sys.argv = [v for k, v in enumerate(sys.argv) if k not in pop]
222        A = float(A_str)
223
224parse_extra_pars()
225
[eb2946f]226
227# =============== FUNCTION DEFINITIONS ================
228
229FUNCTIONS = {}
230def add_function(name, mp_function, np_function, ocl_function,
231                 shortname=None, xaxis="x", limits=(-inf, inf)):
232    if shortname is None:
233        shortname = name.replace('(x)', '').replace(' ', '')
234    FUNCTIONS[shortname] = Comparator(name, mp_function, np_function, ocl_function, xaxis, limits)
235
236add_function(
237    name="J0(x)",
238    mp_function=mp.j0,
239    np_function=scipy.special.j0,
240    ocl_function=make_ocl("return sas_J0(q);", "sas_J0", ["lib/polevl.c", "lib/sas_J0.c"]),
241)
242add_function(
243    name="J1(x)",
244    mp_function=mp.j1,
245    np_function=scipy.special.j1,
246    ocl_function=make_ocl("return sas_J1(q);", "sas_J1", ["lib/polevl.c", "lib/sas_J1.c"]),
247)
248add_function(
249    name="JN(-3, x)",
250    mp_function=lambda x: mp.besselj(-3, x),
251    np_function=lambda x: scipy.special.jn(-3, x),
252    ocl_function=make_ocl("return sas_JN(-3, q);", "sas_JN",
253                          ["lib/polevl.c", "lib/sas_J0.c", "lib/sas_J1.c", "lib/sas_JN.c"]),
254    shortname="J-3",
255)
256add_function(
257    name="JN(3, x)",
258    mp_function=lambda x: mp.besselj(3, x),
259    np_function=lambda x: scipy.special.jn(3, x),
260    ocl_function=make_ocl("return sas_JN(3, q);", "sas_JN",
261                          ["lib/polevl.c", "lib/sas_J0.c", "lib/sas_J1.c", "lib/sas_JN.c"]),
262    shortname="J3",
263)
264add_function(
265    name="JN(2, x)",
266    mp_function=lambda x: mp.besselj(2, x),
267    np_function=lambda x: scipy.special.jn(2, x),
268    ocl_function=make_ocl("return sas_JN(2, q);", "sas_JN",
269                          ["lib/polevl.c", "lib/sas_J0.c", "lib/sas_J1.c", "lib/sas_JN.c"]),
270    shortname="J2",
271)
272add_function(
273    name="2 J1(x)/x",
274    mp_function=lambda x: 2*mp.j1(x)/x,
275    np_function=lambda x: 2*scipy.special.j1(x)/x,
276    ocl_function=make_ocl("return sas_2J1x_x(q);", "sas_2J1x_x", ["lib/polevl.c", "lib/sas_J1.c"]),
277)
278add_function(
279    name="J1(x)",
280    mp_function=mp.j1,
281    np_function=scipy.special.j1,
282    ocl_function=make_ocl("return sas_J1(q);", "sas_J1", ["lib/polevl.c", "lib/sas_J1.c"]),
283)
284add_function(
285    name="Si(x)",
286    mp_function=mp.si,
287    np_function=lambda x: scipy.special.sici(x)[0],
288    ocl_function=make_ocl("return sas_Si(q);", "sas_Si", ["lib/sas_Si.c"]),
289)
290#import fnlib
291#add_function(
292#    name="fnlibJ1",
293#    mp_function=mp.j1,
294#    np_function=fnlib.J1,
295#    ocl_function=make_ocl("return sas_J1(q);", "sas_J1", ["lib/polevl.c", "lib/sas_J1.c"]),
296#)
297add_function(
298    name="sin(x)",
299    mp_function=mp.sin,
300    np_function=np.sin,
301    #ocl_function=make_ocl("double sn, cn; SINCOS(q,sn,cn); return sn;", "sas_sin"),
302    ocl_function=make_ocl("return sin(q);", "sas_sin"),
303)
304add_function(
305    name="sin(x)/x",
306    mp_function=lambda x: mp.sin(x)/x if x != 0 else 1,
307    ## scipy sinc function is inaccurate and has an implied pi*x term
308    #np_function=lambda x: scipy.special.sinc(x/pi),
309    ## numpy sin(x)/x needs to check for x=0
310    np_function=lambda x: np.sin(x)/x,
311    ocl_function=make_ocl("return sas_sinx_x(q);", "sas_sinc"),
312)
313add_function(
314    name="cos(x)",
315    mp_function=mp.cos,
316    np_function=np.cos,
317    #ocl_function=make_ocl("double sn, cn; SINCOS(q,sn,cn); return cn;", "sas_cos"),
318    ocl_function=make_ocl("return cos(q);", "sas_cos"),
319)
320add_function(
321    name="gamma(x)",
322    mp_function=mp.gamma,
323    np_function=scipy.special.gamma,
324    ocl_function=make_ocl("return sas_gamma(q);", "sas_gamma", ["lib/sas_gamma.c"]),
[487e695]325    limits=(-3.1, 10),
[eb2946f]326)
327add_function(
[fba9ca0]328    name="gammaln(x)",
329    mp_function=mp.loggamma,
330    np_function=scipy.special.gammaln,
331    ocl_function=make_ocl("return sas_gammaln(q);", "sas_gammaln", ["lib/sas_gammainc.c"]),
332    #ocl_function=make_ocl("return lgamma(q);", "sas_gammaln"),
333)
334add_function(
335    name="gammainc(x)",
336    mp_function=lambda x, a=A: mp.gammainc(a, a=0, b=x)/mp.gamma(a),
337    np_function=lambda x, a=A: scipy.special.gammainc(a, x),
338    ocl_function=make_ocl("return sas_gammainc(%.15g,q);"%A, "sas_gammainc", ["lib/sas_gammainc.c"]),
339)
340add_function(
341    name="gammaincc(x)",
342    mp_function=lambda x, a=A: mp.gammainc(a, a=x, b=mp.inf)/mp.gamma(a),
343    np_function=lambda x, a=A: scipy.special.gammaincc(a, x),
344    ocl_function=make_ocl("return sas_gammaincc(%.15g,q);"%A, "sas_gammaincc", ["lib/sas_gammainc.c"]),
345)
346add_function(
[eb2946f]347    name="erf(x)",
348    mp_function=mp.erf,
349    np_function=scipy.special.erf,
350    ocl_function=make_ocl("return sas_erf(q);", "sas_erf", ["lib/polevl.c", "lib/sas_erf.c"]),
[487e695]351    limits=(-5., 5.),
[eb2946f]352)
353add_function(
354    name="erfc(x)",
355    mp_function=mp.erfc,
356    np_function=scipy.special.erfc,
357    ocl_function=make_ocl("return sas_erfc(q);", "sas_erfc", ["lib/polevl.c", "lib/sas_erf.c"]),
[487e695]358    limits=(-5., 5.),
[eb2946f]359)
360add_function(
[2a602c7]361    name="expm1(x)",
362    mp_function=mp.expm1,
363    np_function=np.expm1,
364    ocl_function=make_ocl("return expm1(q);", "sas_expm1"),
365    limits=(-5., 5.),
366)
367add_function(
[eb2946f]368    name="arctan(x)",
369    mp_function=mp.atan,
370    np_function=np.arctan,
371    ocl_function=make_ocl("return atan(q);", "sas_arctan"),
372)
373add_function(
374    name="3 j1(x)/x",
375    mp_function=lambda x: 3*(mp.sin(x)/x - mp.cos(x))/(x*x),
376    # Note: no taylor expansion near 0
377    np_function=lambda x: 3*(np.sin(x)/x - np.cos(x))/(x*x),
378    ocl_function=make_ocl("return sas_3j1x_x(q);", "sas_j1c", ["lib/sas_3j1x_x.c"]),
379)
380add_function(
[487e695]381    name="(1-cos(x))/x^2",
382    mp_function=lambda x: (1 - mp.cos(x))/(x*x),
383    np_function=lambda x: (1 - np.cos(x))/(x*x),
384    ocl_function=make_ocl("return (1-cos(q))/q/q;", "sas_1mcosx_x2"),
385)
386add_function(
387    name="(1-sin(x)/x)/x",
388    mp_function=lambda x: 1/x - mp.sin(x)/(x*x),
389    np_function=lambda x: 1/x - np.sin(x)/(x*x),
390    ocl_function=make_ocl("return (1-sas_sinx_x(q))/q;", "sas_1msinx_x_x"),
391)
392add_function(
[2a7e20e]393    name="(1/2-sin(x)/x+(1-cos(x))/x^2)/x",
[487e695]394    mp_function=lambda x: (0.5 - mp.sin(x)/x + (1-mp.cos(x))/(x*x))/x,
395    np_function=lambda x: (0.5 - np.sin(x)/x + (1-np.cos(x))/(x*x))/x,
396    ocl_function=make_ocl("return (0.5-sin(q)/q + (1-cos(q))/q/q)/q;", "sas_T2"),
397)
398add_function(
[eb2946f]399    name="fmod_2pi",
400    mp_function=lambda x: mp.fmod(x, 2*mp.pi),
401    np_function=lambda x: np.fmod(x, 2*np.pi),
402    ocl_function=make_ocl("return fmod(q, 2*M_PI);", "sas_fmod"),
403)
[6e72989]404add_function(
405    name="debye",
406    mp_function=lambda x: 2*(mp.exp(-x**2) + x**2 - 1)/x**4,
[237c9cf]407    np_function=lambda x: 2*(np.expm1(-x**2) + x**2)/x**4,
[6e72989]408    ocl_function=make_ocl("""
409    const double qsq = q*q;
[237c9cf]410    if (qsq < 1.0) { // Pade approximation
[3a220e6]411        const double x = qsq;
[237c9cf]412        if (0) { // 0.36 single
[3a220e6]413            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 4}]
414            return (x*x/180. + 1.)/((1./30.*x + 1./3.)*x + 1);
[237c9cf]415        } else if (0) { // 1.0 for single
[3a220e6]416            // padeapproximant[2*exp[-x^2] + x^2-1)/x^4, {x, 0, 6}]
417            const double A1=1./24., A2=1./84, A3=-1./3360;
418            const double B1=3./8., B2=3./56., B3=1./336.;
419            return (((A3*x + A2)*x + A1)*x + 1.)/(((B3*x + B2)*x + B1)*x + 1.);
[237c9cf]420        } else if (1) { // 1.0 for single, 0.25 for double
[3a220e6]421            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
422            const double A1=1./15., A2=1./60, A3=0., A4=1./75600.;
423            const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.;
424            return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.)
425                  /((((B4*x + B3)*x + B2)*x + B1)*x + 1.);
[237c9cf]426        } else { // 1.0 for single, 0.5 for double
[3a220e6]427            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
428            const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.;
429            const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.;
430            return (((((A5*x + A4)*x + A3)*x + A2)*x + A1)*x + 1.)
431                  /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.);
432        }
[237c9cf]433    } else if (qsq < 1.) { // Taylor series; 0.9 for single, 0.25 for double
[6e72989]434        const double x = qsq;
435        const double C0 = +1.;
436        const double C1 = -1./3.;
437        const double C2 = +1./12.;
438        const double C3 = -1./60.;
439        const double C4 = +1./360.;
440        const double C5 = -1./2520.;
441        const double C6 = +1./20160.;
442        const double C7 = -1./181440.;
443        //return ((((C5*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
[3a220e6]444        //return (((((C6*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
445        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
446    } else {
[237c9cf]447        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq);
[6e72989]448    }
449    """, "sas_debye"),
450)
[eb2946f]451
452RADIUS=3000
453LENGTH=30
454THETA=45
455def mp_cyl(x):
456    f = mp.mpf
457    theta = f(THETA)*mp.pi/f(180)
458    qr = x * f(RADIUS)*mp.sin(theta)
459    qh = x * f(LENGTH)/f(2)*mp.cos(theta)
[5181ccc]460    be = f(2)*mp.j1(qr)/qr
461    si = mp.sin(qh)/qh
462    background = f(0)
463    #background = f(1)/f(1000)
464    volume = mp.pi*f(RADIUS)**f(2)*f(LENGTH)
465    contrast = f(5)
466    units = f(1)/f(10000)
467    #return be
468    #return si
469    return units*(volume*contrast*be*si)**f(2)/volume + background
[eb2946f]470def np_cyl(x):
471    f = np.float64 if x.dtype == np.float64 else np.float32
472    theta = f(THETA)*f(np.pi)/f(180)
473    qr = x * f(RADIUS)*np.sin(theta)
474    qh = x * f(LENGTH)/f(2)*np.cos(theta)
[5181ccc]475    be = f(2)*scipy.special.j1(qr)/qr
476    si = np.sin(qh)/qh
477    background = f(0)
478    #background = f(1)/f(1000)
479    volume = f(np.pi)*f(RADIUS)**2*f(LENGTH)
480    contrast = f(5)
481    units = f(1)/f(10000)
482    #return be
483    #return si
484    return units*(volume*contrast*be*si)**f(2)/volume + background
[eb2946f]485ocl_cyl = """\
486    double THETA = %(THETA).15e*M_PI_180;
487    double qr = q*%(RADIUS).15e*sin(THETA);
488    double qh = q*0.5*%(LENGTH).15e*cos(THETA);
[5181ccc]489    double be = sas_2J1x_x(qr);
490    double si = sas_sinx_x(qh);
491    double background = 0;
492    //double background = 0.001;
493    double volume = M_PI*square(%(RADIUS).15e)*%(LENGTH).15e;
494    double contrast = 5.0;
495    double units = 1e-4;
496    //return be;
497    //return si;
498    return units*square(volume*contrast*be*si)/volume + background;
[eb2946f]499"""%{"LENGTH":LENGTH, "RADIUS": RADIUS, "THETA": THETA}
500add_function(
501    name="cylinder(r=%g, l=%g, theta=%g)"%(RADIUS, LENGTH, THETA),
502    mp_function=mp_cyl,
503    np_function=np_cyl,
504    ocl_function=make_ocl(ocl_cyl, "ocl_cyl", ["lib/polevl.c", "lib/sas_J1.c"]),
505    shortname="cylinder",
506    xaxis="$q/A^{-1}$",
507)
508
509lanczos_gamma = """\
510    const double coeff[] = {
[fba9ca0]511            76.18009172947146, -86.50532032941677,
512            24.01409824083091, -1.231739572450155,
[eb2946f]513            0.1208650973866179e-2,-0.5395239384953e-5
514            };
515    const double x = q;
516    double tmp  = x + 5.5;
517    tmp -= (x + 0.5)*log(tmp);
518    double ser = 1.000000000190015;
519    for (int k=0; k < 6; k++) ser += coeff[k]/(x + k+1);
520    return -tmp + log(2.5066282746310005*ser/x);
521"""
522add_function(
[fba9ca0]523    name="loggamma(x)",
[eb2946f]524    mp_function=mp.loggamma,
525    np_function=scipy.special.gammaln,
526    ocl_function=make_ocl(lanczos_gamma, "lgamma"),
527)
528
[2a602c7]529replacement_expm1 = """\
530      double x = (double)q;  // go back to float for single precision kernels
531      // Adapted from the cephes math library.
532      // Copyright 1984 - 1992 by Stephen L. Moshier
533      if (x != x || x == 0.0) {
534         return x; // NaN and +/- 0
535      } else if (x < -0.5 || x > 0.5) {
536         return exp(x) - 1.0;
537      } else {
538         const double xsq = x*x;
539         const double p = (((
540            +1.2617719307481059087798E-4)*xsq
541            +3.0299440770744196129956E-2)*xsq
542            +9.9999999999999999991025E-1);
543         const double q = ((((
544            +3.0019850513866445504159E-6)*xsq
545            +2.5244834034968410419224E-3)*xsq
546            +2.2726554820815502876593E-1)*xsq
547            +2.0000000000000000000897E0);
548         double r = x * p;
549         r =  r / (q - r);
550         return r+r;
551       }
552"""
553add_function(
554    name="sas_expm1(x)",
555    mp_function=mp.expm1,
556    np_function=np.expm1,
557    ocl_function=make_ocl(replacement_expm1, "sas_expm1"),
558)
559
[eb2946f]560# Alternate versions of 3 j1(x)/x, for posterity
561def taylor_3j1x_x(x):
562    """
563    Calculation using taylor series.
564    """
565    # Generate coefficients using the precision of the target value.
566    n = 5
567    cinv = [3991680, -45360, 840, -30, 3]
568    three = x.dtype.type(3)
569    p = three/np.array(cinv, x.dtype)
570    return np.polyval(p[-n:], x*x)
571add_function(
572    name="3 j1(x)/x: taylor",
573    mp_function=lambda x: 3*(mp.sin(x)/x - mp.cos(x))/(x*x),
574    np_function=taylor_3j1x_x,
575    ocl_function=make_ocl("return sas_3j1x_x(q);", "sas_j1c", ["lib/sas_3j1x_x.c"]),
576)
577def trig_3j1x_x(x):
578    r"""
579    Direct calculation using linear combination of sin/cos.
580
581    Use the following trig identity:
582
583    .. math::
584
585        a \sin(x) + b \cos(x) = c \sin(x + \phi)
586
587    where $c = \surd(a^2+b^2)$ and $\phi = \tan^{-1}(b/a) to calculate the
588    numerator $\sin(x) - x\cos(x)$.
589    """
590    one = x.dtype.type(1)
591    three = x.dtype.type(3)
592    c = np.sqrt(one + x*x)
593    phi = np.arctan2(-x, one)
594    return three*(c*np.sin(x+phi))/(x*x*x)
595add_function(
596    name="3 j1(x)/x: trig",
597    mp_function=lambda x: 3*(mp.sin(x)/x - mp.cos(x))/(x*x),
598    np_function=trig_3j1x_x,
599    ocl_function=make_ocl("return sas_3j1x_x(q);", "sas_j1c", ["lib/sas_3j1x_x.c"]),
600)
601def np_2J1x_x(x):
602    """
603    numpy implementation of 2J1(x)/x using single precision algorithm
604    """
605    # pylint: disable=bad-continuation
606    f = x.dtype.type
607    ax = abs(x)
608    if ax < f(8.0):
609        y = x*x
610        ans1 = f(2)*(f(72362614232.0)
611                  + y*(f(-7895059235.0)
612                  + y*(f(242396853.1)
613                  + y*(f(-2972611.439)
614                  + y*(f(15704.48260)
615                  + y*(f(-30.16036606)))))))
616        ans2 = (f(144725228442.0)
617                  + y*(f(2300535178.0)
618                  + y*(f(18583304.74)
619                  + y*(f(99447.43394)
620                  + y*(f(376.9991397)
621                  + y)))))
622        return ans1/ans2
623    else:
624        y = f(64.0)/(ax*ax)
625        xx = ax - f(2.356194491)
626        ans1 = (f(1.0)
627                  + y*(f(0.183105e-2)
628                  + y*(f(-0.3516396496e-4)
629                  + y*(f(0.2457520174e-5)
630                  + y*f(-0.240337019e-6)))))
631        ans2 = (f(0.04687499995)
632                  + y*(f(-0.2002690873e-3)
633                  + y*(f(0.8449199096e-5)
634                  + y*(f(-0.88228987e-6)
635                  + y*f(0.105787412e-6)))))
636        sn, cn = np.sin(xx), np.cos(xx)
637        ans = np.sqrt(f(0.636619772)/ax) * (cn*ans1 - (f(8.0)/ax)*sn*ans2) * f(2)/x
638        return -ans if (x < f(0.0)) else ans
639add_function(
640    name="2 J1(x)/x:alt",
641    mp_function=lambda x: 2*mp.j1(x)/x,
642    np_function=lambda x: np.asarray([np_2J1x_x(v) for v in x], x.dtype),
643    ocl_function=make_ocl("return sas_2J1x_x(q);", "sas_2J1x_x", ["lib/polevl.c", "lib/sas_J1.c"]),
644)
645
646ALL_FUNCTIONS = set(FUNCTIONS.keys())
[fba9ca0]647ALL_FUNCTIONS.discard("loggamma")  # use cephes-based gammaln instead
[eb2946f]648ALL_FUNCTIONS.discard("3j1/x:taylor")
649ALL_FUNCTIONS.discard("3j1/x:trig")
650ALL_FUNCTIONS.discard("2J1/x:alt")
651
652# =============== MAIN PROGRAM ================
653
654def usage():
655    names = ", ".join(sorted(ALL_FUNCTIONS))
656    print("""\
[2a7e20e]657usage: precision.py [-f/a/r] [-x<range>] "name" ...
[eb2946f]658where
[5181ccc]659    -f indicates that the function value should be plotted,
660    -a indicates that the absolute error should be plotted,
661    -r indicates that the relative error should be plotted (default),
662    -x<range> indicates the steps in x, where <range> is one of the following
[fba9ca0]663        log indicates log stepping in [10^-3, 10^5] (default)
664        logq indicates log stepping in [10^-4, 10^1]
665        linear indicates linear stepping in [1, 1000]
666        zoom indicates linear stepping in [1000, 1010]
667        neg indicates linear stepping in [-100.1, 100.1]
668        start:stop:n[:stepping] indicates an n-step plot in [start, stop]
669            or [10^start, 10^stop] if stepping is "log" (default n=400)
670Some functions (notably gammainc/gammaincc) have an additional parameter A
671which can be set from the command line as A=value.  Default is A=1.
672
673Name is one of:
[eb2946f]674    """+names)
675    sys.exit(1)
676
677def main():
678    import sys
[5181ccc]679    diff = "relative"
[eb2946f]680    xrange = "log"
[5181ccc]681    options = [v for v in sys.argv[1:] if v.startswith('-')]
682    for opt in options:
683        if opt == '-f':
684            diff = "none"
685        elif opt == '-r':
686            diff = "relative"
687        elif opt == '-a':
688            diff = "absolute"
689        elif opt.startswith('-x'):
690            xrange = opt[2:]
691        else:
692            usage()
693
694    names = [v for v in sys.argv[1:] if not v.startswith('-')]
695    if not names:
[eb2946f]696        usage()
[5181ccc]697
698    if names[0] == "all":
699        cutoff = names[1] if len(names) > 1 else ""
700        names = list(sorted(ALL_FUNCTIONS))
701        names = [k for k in names if k >= cutoff]
702    if any(k not in FUNCTIONS for k in names):
[eb2946f]703        usage()
[5181ccc]704    multiple = len(names) > 1
[eb2946f]705    pylab.interactive(multiple)
[5181ccc]706    for k in names:
[eb2946f]707        pylab.clf()
708        comparator = FUNCTIONS[k]
709        comparator.run(xrange=xrange, diff=diff)
710        if multiple:
711            raw_input()
712    if not multiple:
713        pylab.show()
714
715if __name__ == "__main__":
716    main()
Note: See TracBrowser for help on using the repository browser.