Changeset 595 for branches


Ignore:
Timestamp:
11/20/12 11:48:23 (3 years ago)
Author:
mmckerns
Message:

enable a simple round (only rounds floats, does not 'deep round')

Location:
branches/decorate
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/memoize.py

    r590 r595  
    5050 
    5151 
     52def 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 
     64def 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 
    5274def get_archive(archive): 
    5375    import dill as pickle 
     
    101123#FIXME: resolve signature difference...  @memoized versus @memoized() 
    102124 
    103 def memoized0_nopickle_round(tol=0): 
     125def memoized0_nopickle_round(tol=0, deep=False): 
    104126    """Decorator that memoizes a function's return value each time it is called. 
    105127    If called later with the same arguments, the memoized value is returned, and 
     
    111133    memo = {} 
    112134 
    113     @deep_round(tol) 
     135    if deep: rounded = deep_round 
     136    else: rounded = simple_round 
     137 
     138    @rounded(tol) 
    114139    def rounded_args(*args, **kwds): 
    115140        return (args, kwds) 
     
    130155 
    131156 
    132 def memoized0_round(tol=0): 
     157def memoized0_round(tol=0, deep=False): 
    133158    """Decorator that memoizes a function's return value each time it is called. 
    134159    If called later with the same arguments, the memoized value is returned, and 
     
    139164    memo = {} 
    140165 
    141     @deep_round(tol) 
     166    if deep: rounded = deep_round 
     167    else: rounded = simple_round 
     168 
     169    @rounded(tol) 
    142170    def rounded_args(*args, **kwds): 
    143171        return (args, kwds) 
  • branches/decorate/surrogate.py

    r591 r595  
    11#! /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_bl 
    20   A=0 
    21 else 
    22   A=K*((h/Dp)^p)*((cos(a))^u)*(tanh((v/v_bl)-1))^m; 
    23 end 
    24 """ 
    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), with 
    32 # subdivisions at h=100, a=20, v=2.2 
    33 # due to ballistic limit: v(h=100,a=20) = 2.22, 
    34 # perforation in this region should be zero. 
    35 # NOTE: 'failure' is A < = t 
    36 #  
    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 their 
    40 #    intervals 
    41 #  * McDiarmid diameter of the perforation area A when restricted to that 
    42 #    cuboid or subcuboid 
    43 #  * the mean value of the perforation area A on each (sub)cuboid 
    44  
    452from math import pi, cos, tanh 
    463 
  • branches/decorate/test_memoize.py

    r581 r595  
    9090cost1 = memoized0_round(1)(cost) 
    9191cost0 = memoized0_round(0)(cost) 
     92costD = memoized0_round(0, deep=True)(cost) 
    9293 
    9394print "rounding to one decimals..." 
     
    103104print cost0([1,2,3.4321], 3.6789) 
    104105 
     106print "\nrerun again with deep rounding to zero decimals..." 
     107print costD([1,2,3.1234], 3.9876) 
     108print costD([1,2,3.1234], 3.9876) 
     109print costD([1,2,3.1234], 3.6789) 
     110print costD([1,2,3.4321], 3.6789) 
     111 
    105112 
    106113# EOF 
Note: See TracChangeset for help on using the changeset viewer.