Changeset 595
- Timestamp:
- 11/20/12 11:48:23 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/memoize.py
r590 r595 50 50 51 51 52 def simple_round_factory(tol): 53 def simple_round(*args, **kwds): 54 argstype = type(args) 55 _args = list(args) 56 _kwds = kwds.copy() 57 for i,j in enumerate(args): 58 if isinstance(j, float): _args[i] = round(j, tol) # don't round int 59 for i,j in kwds.items(): 60 if isinstance(j, float): _kwds[i] = round(j, tol) 61 return argstype(_args), _kwds 62 return simple_round 63 64 def simple_round(tol=0): #NOTE: only rounds floats, nothing else 65 def dec(f): 66 def func(*args, **kwds): 67 _simple_round = simple_round_factory(tol) 68 _args,_kwds = _simple_round(*args, **kwds) 69 return f(*_args, **_kwds) 70 return func 71 return dec 72 73 52 74 def get_archive(archive): 53 75 import dill as pickle … … 101 123 #FIXME: resolve signature difference... @memoized versus @memoized() 102 124 103 def memoized0_nopickle_round(tol=0 ):125 def memoized0_nopickle_round(tol=0, deep=False): 104 126 """Decorator that memoizes a function's return value each time it is called. 105 127 If called later with the same arguments, the memoized value is returned, and … … 111 133 memo = {} 112 134 113 @deep_round(tol) 135 if deep: rounded = deep_round 136 else: rounded = simple_round 137 138 @rounded(tol) 114 139 def rounded_args(*args, **kwds): 115 140 return (args, kwds) … … 130 155 131 156 132 def memoized0_round(tol=0 ):157 def memoized0_round(tol=0, deep=False): 133 158 """Decorator that memoizes a function's return value each time it is called. 134 159 If called later with the same arguments, the memoized value is returned, and … … 139 164 memo = {} 140 165 141 @deep_round(tol) 166 if deep: rounded = deep_round 167 else: rounded = simple_round 168 169 @rounded(tol) 142 170 def rounded_args(*args, **kwds): 143 171 return (args, kwds) -
branches/decorate/surrogate.py
r591 r595 1 1 #! /usr/bin/env python 2 """Original matlab code:3 4 function A=marc_surr(x)5 h=x(1)*25.4*10^(-3);6 a=x(2)*pi/180;7 v=x(3);8 Ho=0.5794;9 s=1.4004;10 n=0.4482;11 K=10.3963;12 p=0.4757;13 u=1.0275;14 m=0.4682;15 Dp=1.778;16 17 v_bl=Ho*(h/(cos(a))^(n))^s;18 19 if v<v_bl20 A=021 else22 A=K*((h/Dp)^p)*((cos(a))^u)*(tanh((v/v_bl)-1))^m;23 end24 """25 26 ### NOTES ###27 # h = thickness = [60,105]28 # a = obliquity = [0,30]29 # v = speed = [2.1,2.8]30 31 # explore the cuboid (h,a,v), with32 # subdivisions at h=100, a=20, v=2.233 # due to ballistic limit: v(h=100,a=20) = 2.22,34 # perforation in this region should be zero.35 # NOTE: 'failure' is A < = t36 #37 # Calculate for each of the 8 subcuboids:38 # * probability mass, i.e. the product of the normalized side-lengths,39 # since we're taking h, a and v to be uniformly distributed in their40 # intervals41 # * McDiarmid diameter of the perforation area A when restricted to that42 # cuboid or subcuboid43 # * the mean value of the perforation area A on each (sub)cuboid44 45 2 from math import pi, cos, tanh 46 3 -
branches/decorate/test_memoize.py
r581 r595 90 90 cost1 = memoized0_round(1)(cost) 91 91 cost0 = memoized0_round(0)(cost) 92 costD = memoized0_round(0, deep=True)(cost) 92 93 93 94 print "rounding to one decimals..." … … 103 104 print cost0([1,2,3.4321], 3.6789) 104 105 106 print "\nrerun again with deep rounding to zero decimals..." 107 print costD([1,2,3.1234], 3.9876) 108 print costD([1,2,3.1234], 3.9876) 109 print costD([1,2,3.1234], 3.6789) 110 print costD([1,2,3.4321], 3.6789) 111 105 112 106 113 # EOF
Note: See TracChangeset
for help on using the changeset viewer.