Changeset 626 for branches


Ignore:
Timestamp:
12/31/12 09:59:13 (3 years ago)
Author:
mmckerns
Message:

discrete decorator can be applied to given indicies

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/wrapper.py

    r610 r626  
    4141 
    4242 
    43 from numpy import asarray, any, inf, vectorize 
     43from numpy import asarray, any, inf, vectorize, choose 
    4444def bounded(min=None, max=None): 
    4545    """impose an infinite barrier box constraint on a function 
     
    119119 
    120120#from random import sample, choice 
    121 def discrete(samples): 
     121def discrete(samples, index=None): 
    122122    """impose a discrete set of input values for the selected function 
    123123 
     
    126126>>> @discrete([1.0, 2.0]) 
    127127... def identity(x): 
    128 ...   return x 
     128...     return x 
    129129 
    130130>>> identity([0.123, 1.789, 4.000]) 
    131 [1.0, 2.0, 2.0]""" #FIXME: should return same type as input 
     131[1.0, 2.0, 2.0] 
     132 
     133>>> @discrete([1,3,5,7], index=(0,3)]) 
     134... def squared(x): 
     135....    return [i**2 for i in x] 
     136 
     137>>> squared([0,2,4,6,8,10]) 
     138[1, 4, 16, 25, 64, 100]""" 
    132139    samples = [asarray(samples)] 
    133140    samples[0].sort() 
     
    157164    def dec(f): 
    158165        def func(x, *args, **kwds): 
     166            xtype = type(x) 
    159167            arglo, arghi = argnear(x) 
    160168            xp = near(x, samples[0][arglo], samples[0][arghi]) 
     169            # create a choice array from given indices 
     170            mask = xp*False #FIXME: better ways to do this 
     171            try: mask[sorted(index)] = True #FIXME: better ways to do this 
     172            except IndexError: pass 
     173            xp = xtype(choose(mask, (x,xp))) 
    161174            return f(xp, *args, **kwds) 
    162175        func.samples = _points 
Note: See TracChangeset for help on using the changeset viewer.