1 | r""" |
---|
2 | Show numerical precision of $2 J_1(x)/x$. |
---|
3 | """ |
---|
4 | import sys; sys.path.insert(0, '..') |
---|
5 | |
---|
6 | import numpy as np |
---|
7 | try: |
---|
8 | from mpmath import mp |
---|
9 | except: |
---|
10 | # CRUFT: mpmath split out into its own package |
---|
11 | from sympy.mpmath import mp |
---|
12 | #import matplotlib; matplotlib.use('TkAgg') |
---|
13 | import pylab |
---|
14 | |
---|
15 | |
---|
16 | SHOW_DIFF = True # True if show diff rather than function value |
---|
17 | #SHOW_DIFF = False # True if show diff rather than function value |
---|
18 | LINEAR_X = False # True if q is linearly spaced instead of log spaced |
---|
19 | #LINEAR_X = True # True if q is linearly spaced instead of log spaced |
---|
20 | FUNCTION = "J1(x)" |
---|
21 | |
---|
22 | def mp_fn(vec, bits=500): |
---|
23 | """ |
---|
24 | Direct calculation using sympy multiprecision library. |
---|
25 | """ |
---|
26 | with mp.workprec(bits): |
---|
27 | return [_mp_fn(mp.mpf(x)) for x in vec] |
---|
28 | |
---|
29 | def _mp_fn(x): |
---|
30 | """ |
---|
31 | Actual function that gets evaluated. The caller just vectorizes. |
---|
32 | """ |
---|
33 | return mp.j1(x) |
---|
34 | |
---|
35 | def np_fn(x, dtype): |
---|
36 | """ |
---|
37 | Direct calculation using scipy. |
---|
38 | """ |
---|
39 | from scipy.special import j1 as J1 |
---|
40 | x = np.asarray(x, dtype) |
---|
41 | return J1(x) |
---|
42 | |
---|
43 | def sasmodels_fn(x, dtype, platform='ocl'): |
---|
44 | """ |
---|
45 | Calculation using pade approximant. |
---|
46 | """ |
---|
47 | from sasmodels import core, data, direct_model |
---|
48 | model = core.load_model('bessel', dtype=dtype) |
---|
49 | calculator = direct_model.DirectModel(data.empty_data1D(x), model) |
---|
50 | |
---|
51 | return calculator(background=0) |
---|
52 | |
---|
53 | def plotdiff(x, target, actual, label): |
---|
54 | """ |
---|
55 | Plot the computed value. |
---|
56 | |
---|
57 | Use relative error if SHOW_DIFF, otherwise just plot the value directly. |
---|
58 | """ |
---|
59 | if SHOW_DIFF: |
---|
60 | err = abs((target-actual)/target) |
---|
61 | #err = np.clip(err, 0, 1) |
---|
62 | pylab.loglog(x, err, '-', label=label) |
---|
63 | else: |
---|
64 | limits = np.min(target), np.max(target) |
---|
65 | pylab.semilogx(x, np.clip(actual,*limits), '-', label=label) |
---|
66 | |
---|
67 | def compare(x, precision, target): |
---|
68 | r""" |
---|
69 | Compare the different computation methods using the given precision. |
---|
70 | """ |
---|
71 | #plotdiff(x, target, mp_fn(x, 11), 'mp 11 bits') |
---|
72 | plotdiff(x, target, np_fn(x, precision), 'numpy '+precision) |
---|
73 | plotdiff(x, target, sasmodels_fn(x, precision, 0), 'sasmodels '+precision) |
---|
74 | pylab.xlabel("qr (1/Ang)") |
---|
75 | if SHOW_DIFF: |
---|
76 | pylab.ylabel("relative error") |
---|
77 | else: |
---|
78 | pylab.ylabel(FUNCTION) |
---|
79 | pylab.semilogx(x, target, '-', label="true value") |
---|
80 | if LINEAR_X: |
---|
81 | pylab.xscale('linear') |
---|
82 | |
---|
83 | def main(): |
---|
84 | r""" |
---|
85 | Compare accuracy of different methods for computing $3 j_1(x)/x$. |
---|
86 | :return: |
---|
87 | """ |
---|
88 | if LINEAR_X: |
---|
89 | qr = np.linspace(1,1000,2000) |
---|
90 | else: |
---|
91 | qr = np.logspace(-3,5,400) |
---|
92 | target = np.asarray(mp_fn(qr), 'double') |
---|
93 | pylab.subplot(121) |
---|
94 | compare(qr, 'single', target) |
---|
95 | pylab.legend(loc='best') |
---|
96 | pylab.subplot(122) |
---|
97 | compare(qr, 'double', target) |
---|
98 | pylab.legend(loc='best') |
---|
99 | pylab.suptitle(FUNCTION) |
---|
100 | |
---|
101 | if __name__ == "__main__": |
---|
102 | #print "\n".join(str(x) for x in mp_J1c([1e-6,1e-5,1e-4,1e-3])) |
---|
103 | main() |
---|
104 | pylab.show() |
---|