Changes in explore/precision.py [ee60aa7:fba9ca0] in sasmodels
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/precision.py
ree60aa7 rfba9ca0 95 95 neg: [-100,100] 96 96 97 For arbitrary range use "start:stop:steps:scale" where scale is 98 one of log, lin, or linear. 99 97 100 *diff* is "relative", "absolute" or "none" 98 101 … … 102 105 linear = not xrange.startswith("log") 103 106 if xrange == "zoom": 104 lin_min, lin_max, lin_steps = 1000, 1010, 2000107 start, stop, steps = 1000, 1010, 2000 105 108 elif xrange == "neg": 106 lin_min, lin_max, lin_steps = -100.1, 100.1, 2000109 start, stop, steps = -100.1, 100.1, 2000 107 110 elif xrange == "linear": 108 lin_min, lin_max, lin_steps = 1, 1000, 2000109 lin_min, lin_max, lin_steps = 0.001, 2, 2000111 start, stop, steps = 1, 1000, 2000 112 start, stop, steps = 0.001, 2, 2000 110 113 elif xrange == "log": 111 log_min, log_max, log_steps = -3, 5, 400114 start, stop, steps = -3, 5, 400 112 115 elif xrange == "logq": 113 log_min, log_max, log_steps = -4, 1, 400 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 114 124 else: 115 125 raise ValueError("unknown range "+xrange) … … 121 131 # value to x in the given precision. 122 132 if linear: 123 lin_min = max(lin_min, self.limits[0])124 lin_max = min(lin_max, self.limits[1])125 qrf = np.linspace( lin_min, lin_max, lin_steps, dtype='single')126 #qrf = np.linspace( lin_min, lin_max, lin_steps, dtype='double')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') 127 137 qr = [mp.mpf(float(v)) for v in qrf] 128 #qr = mp.linspace( lin_min, lin_max, lin_steps)138 #qr = mp.linspace(start, stop, steps) 129 139 else: 130 log_min = np.log10(max(10**log_min, self.limits[0]))131 log_max = np.log10(min(10**log_max, self.limits[1]))132 qrf = np.logspace( log_min, log_max, log_steps, dtype='single')133 #qrf = np.logspace( log_min, log_max, log_steps, dtype='double')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') 134 144 qr = [mp.mpf(float(v)) for v in qrf] 135 #qr = [10**v for v in mp.linspace( log_min, log_max, log_steps)]145 #qr = [10**v for v in mp.linspace(start, stop, steps)] 136 146 137 147 target = self.call_mpmath(qr, bits=500) … … 176 186 """ 177 187 if diff == "relative": 178 err = np.array([ abs((t-a)/t) for t, a in zip(target, actual)], 'd')188 err = np.array([(abs((t-a)/t) if t != 0 else a) for t, a in zip(target, actual)], 'd') 179 189 #err = np.clip(err, 0, 1) 180 190 pylab.loglog(x, err, '-', label=label) … … 197 207 return model_info 198 208 209 # Hack to allow second parameter A in two parameter functions 210 A = 1 211 def 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 224 parse_extra_pars() 225 199 226 200 227 # =============== FUNCTION DEFINITIONS ================ … … 299 326 ) 300 327 add_function( 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 ) 334 add_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 ) 340 add_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 ) 346 add_function( 301 347 name="erf(x)", 302 348 mp_function=mp.erf, … … 357 403 ) 358 404 add_function( 359 name=" gauss_coil",405 name="debye", 360 406 mp_function=lambda x: 2*(mp.exp(-x**2) + x**2 - 1)/x**4, 361 407 np_function=lambda x: 2*(np.expm1(-x**2) + x**2)/x**4, 362 408 ocl_function=make_ocl(""" 363 409 const double qsq = q*q; 364 // For double: use O(5) Pade with 0.5 cutoff (10 mad + 1 divide) 365 // For single: use O(7) Taylor with 0.8 cutoff (7 mad) 366 if (qsq < 0.0) { 410 if (qsq < 1.0) { // Pade approximation 367 411 const double x = qsq; 368 412 if (0) { // 0.36 single … … 374 418 const double B1=3./8., B2=3./56., B3=1./336.; 375 419 return (((A3*x + A2)*x + A1)*x + 1.)/(((B3*x + B2)*x + B1)*x + 1.); 376 } else if ( 0) { // 1.0 for single, 0.25 for double420 } else if (1) { // 1.0 for single, 0.25 for double 377 421 // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 378 422 const double A1=1./15., A2=1./60, A3=0., A4=1./75600.; … … 387 431 /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.); 388 432 } 389 } else if (qsq < 0.8) {433 } else if (qsq < 1.) { // Taylor series; 0.9 for single, 0.25 for double 390 434 const double x = qsq; 391 435 const double C0 = +1.; … … 465 509 lanczos_gamma = """\ 466 510 const double coeff[] = { 467 76.18009172947146, 468 24.01409824083091, 511 76.18009172947146, -86.50532032941677, 512 24.01409824083091, -1.231739572450155, 469 513 0.1208650973866179e-2,-0.5395239384953e-5 470 514 }; … … 477 521 """ 478 522 add_function( 479 name="log 523 name="loggamma(x)", 480 524 mp_function=mp.loggamma, 481 525 np_function=scipy.special.gammaln, … … 601 645 602 646 ALL_FUNCTIONS = set(FUNCTIONS.keys()) 603 ALL_FUNCTIONS.discard("loggamma") # OCL version not ready yet647 ALL_FUNCTIONS.discard("loggamma") # use cephes-based gammaln instead 604 648 ALL_FUNCTIONS.discard("3j1/x:taylor") 605 649 ALL_FUNCTIONS.discard("3j1/x:trig") … … 617 661 -r indicates that the relative error should be plotted (default), 618 662 -x<range> indicates the steps in x, where <range> is one of the following 619 log indicates log stepping in [10^-3, 10^5] (default) 620 logq indicates log stepping in [10^-4, 10^1] 621 linear indicates linear stepping in [1, 1000] 622 zoom indicates linear stepping in [1000, 1010] 623 neg indicates linear stepping in [-100.1, 100.1] 624 and name is "all" or one of: 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) 670 Some functions (notably gammainc/gammaincc) have an additional parameter A 671 which can be set from the command line as A=value. Default is A=1. 672 673 Name is one of: 625 674 """+names) 626 675 sys.exit(1)
Note: See TracChangeset
for help on using the changeset viewer.