Changeset 4f2478e in sasmodels
- Timestamp:
- Jan 28, 2016 1:19:03 PM (9 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- a3e78c3
- Parents:
- bf6631a
- Location:
- sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/compare.py
rd15a908 r4f2478e 117 117 return dt.total_seconds() 118 118 119 120 class push_seed(object): 121 """ 122 Set the seed value for the random number generator. 123 124 When used in a with statement, the random number generator state is 125 restored after the with statement is complete. 126 127 :Parameters: 128 129 *seed* : int or array_like, optional 130 Seed for RandomState 131 132 :Example: 133 134 Seed can be used directly to set the seed:: 135 136 >>> from numpy.random import randint 137 >>> push_seed(24) 138 <...push_seed object at...> 139 >>> print(randint(0,1000000,3)) 140 [242082 899 211136] 141 142 Seed can also be used in a with statement, which sets the random 143 number generator state for the enclosed computations and restores 144 it to the previous state on completion:: 145 146 >>> with push_seed(24): 147 ... print(randint(0,1000000,3)) 148 [242082 899 211136] 149 150 Using nested contexts, we can demonstrate that state is indeed 151 restored after the block completes:: 152 153 >>> with push_seed(24): 154 ... print(randint(0,1000000)) 155 ... with push_seed(24): 156 ... print(randint(0,1000000,3)) 157 ... print(randint(0,1000000)) 158 242082 159 [242082 899 211136] 160 899 161 162 The restore step is protected against exceptions in the block:: 163 164 >>> with push_seed(24): 165 ... print(randint(0,1000000)) 166 ... try: 167 ... with push_seed(24): 168 ... print(randint(0,1000000,3)) 169 ... raise Exception() 170 ... except: 171 ... print("Exception raised") 172 ... print(randint(0,1000000)) 173 242082 174 [242082 899 211136] 175 Exception raised 176 899 177 """ 178 def __init__(self, seed=None): 179 self._state = np.random.get_state() 180 np.random.seed(seed) 181 182 def __enter__(self): 183 return None 184 185 def __exit__(self, *args): 186 np.random.set_state(self._state) 119 187 120 188 def tic(): … … 179 247 return [0, (2*v if v > 0 else 1)] 180 248 249 181 250 def _randomize_one(p, v): 182 251 """ … … 188 257 return np.random.uniform(*parameter_range(p, v)) 189 258 259 190 260 def randomize_pars(pars, seed=None): 191 261 """ … … 197 267 :func:`constrain_pars` needs to be called afterward.. 198 268 """ 199 np.random.seed(seed)200 # Note: the sort guarantees order `of calls to random number generator201 pars = dict((p, _randomize_one(p, v))202 for p, v in sorted(pars.items()))269 with push_seed(seed): 270 # Note: the sort guarantees order `of calls to random number generator 271 pars = dict((p, _randomize_one(p, v)) 272 for p, v in sorted(pars.items())) 203 273 return pars 204 274 -
sasmodels/compare_many.py
rd15a908 r4f2478e 261 261 262 262 if __name__ == "__main__": 263 #from .compare import push_seed 264 #with push_seed(1): main() 263 265 main() -
sasmodels/models/lib/sph_j1c.c
r9c461c7 r4f2478e 1 1 /** 2 * Spherical Bessel function j1(x)/x2 * Spherical Bessel function 3*j1(x)/x 3 3 * 4 4 * Used for low q to avoid cancellation error. … … 8 8 * requires the first 3 terms. Double precision requires the 4th term. 9 9 * The fifth term is not needed, and is commented out. 10 * Taylor expansion: 11 * 1.0 + q2*(-3./30. + q2*(3./840.))+ q2*(-3./45360. + q2*(3./3991680.)))) 12 * Expression returned from Herbie (herbie.uwpise.org/demo): 13 * const double t = ((1. + 3.*q2*q2/5600.) - q2/20.); 14 * return t*t; 10 15 */ 11 16 … … 18 23 SINCOS(q, sin_q, cos_q); 19 24 20 const double bessel = (q < 1.e-1) ?21 1.0 + q2*(-3./30. + q2*(3./840. + q2*(-3./45360.))) // + q2*(3./3991680.)))25 const double bessel = (q < 0.384038453352533) 26 ? (1.0 + q2*(-3./30. + q2*(3./840.)) 22 27 : 3.0*(sin_q/q - cos_q)/q2; 23 28 24 29 return bessel; 30 31 /* 32 // Code to test various expressions 33 if (sizeof(q2) > 4) { 34 return 3.0*(sin_q/q - cos_q)/q2; 35 } else if (q < 0.384038453352533) { 36 //const double t = ((1. + 3.*q2*q2/5600.) - q2/20.); return t*t; 37 return 1.0 + q2*q2*(3./840.) - q2*(3./30.); 38 //return 1.0 + q2*(-3./30. + q2*(3./840.)); 39 //return 1.0 + q2*(-3./30. + q2*(3./840. + q2*(-3./45360.))); 40 //return 1.0 + q2*(-3./30. + q2*(3./840. + q2*(-3./45360. + q2*(3./3991680.)))); 41 } else { 42 return 3.0*(sin_q/q - cos_q)/q2; 43 } 44 */ 25 45 }
Note: See TracChangeset
for help on using the changeset viewer.