Changeset 87 for python/tools.py


Ignore:
Timestamp:
02/05/09 20:28:35 (7 years ago)
Author:
mmckerns
Message:

added tools.wrap_bounds; added range bounds to fmin (modified from park.simplex)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tools.py

    r82 r87  
    189189    return ncalls, function_wrapper 
    190190 
     191def wrap_bounds(function, min=None, max=None): 
     192    from numpy import asarray, any, inf 
     193    bounds = True 
     194    if min is not None and max is not None: #has upper & lower bound 
     195        min = asarray(min) 
     196        max = asarray(max) 
     197    elif min is not None: #has lower bound 
     198        min = asarray(min) 
     199        max = asarray([inf for i in min]) 
     200    elif max is not None: #has upper bound 
     201        max = asarray(max) 
     202        min = asarray([-inf for i in max]) 
     203    else: #not bounded 
     204        bounds = False 
     205    if bounds: 
     206        def function_wrapper(x): 
     207            if any((x<min)|(x>max)): #if violate bounds, evaluate as inf 
     208                return inf 
     209            return function(x) 
     210    else: 
     211        def function_wrapper(x): 
     212            return function(x) 
     213    return function_wrapper 
     214 
    191215def wrap_cf(CF, REG=None, cfmult = 1.0, regmult = 0.0): 
    192216    def _(*args, **kwargs): 
Note: See TracChangeset for help on using the changeset viewer.