- Timestamp:
- 12/31/12 09:59:13 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/wrapper.py
r610 r626 41 41 42 42 43 from numpy import asarray, any, inf, vectorize 43 from numpy import asarray, any, inf, vectorize, choose 44 44 def bounded(min=None, max=None): 45 45 """impose an infinite barrier box constraint on a function … … 119 119 120 120 #from random import sample, choice 121 def discrete(samples ):121 def discrete(samples, index=None): 122 122 """impose a discrete set of input values for the selected function 123 123 … … 126 126 >>> @discrete([1.0, 2.0]) 127 127 ... def identity(x): 128 ... return x128 ... return x 129 129 130 130 >>> 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]""" 132 139 samples = [asarray(samples)] 133 140 samples[0].sort() … … 157 164 def dec(f): 158 165 def func(x, *args, **kwds): 166 xtype = type(x) 159 167 arglo, arghi = argnear(x) 160 168 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))) 161 174 return f(xp, *args, **kwds) 162 175 func.samples = _points
Note: See TracChangeset
for help on using the changeset viewer.