Changeset 616 for branches


Ignore:
Timestamp:
12/19/12 17:34:01 (3 years ago)
Author:
mmckerns
Message:

added test_build_constraint, renamed test_generate_constraint in test_symbolic;
math from numpy in _symbolic consistent with symbolic;
prepent build_linear and build_nonlinear with an underscore (i.e. rename)

Location:
branches/decorate
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/_symbolic.py

    r615 r616  
    11from symbolic import * 
     2from wrapper import isbounded 
     3from penalty import issolution 
    24from mystic.tools import permutations 
    35 
     
    270272    # default is _locals with numpy and math imported 
    271273    # numpy throws an 'AttributeError', but math passes error to sympy 
    272     code = """from numpy import *; from math import *;""" #XXX: prefer math? 
     274    code = """from numpy import *; from math import *;""" # prefer math 
     275    code += """from numpy import mean as average;""" # use np.mean not average 
     276    code += """from numpy import var as variance;""" # look like mystic.math 
     277    code += """from numpy import ptp as spread;"""   # look like mystic.math 
    273278    code = compile(code, '<string>', 'exec') 
    274279    exec code in _locals 
     
    321326 
    322327 
    323 def build_linear(constraints, variables='x', nvars=None, order=None, **kwds): 
     328def _build_linear(constraints, variables='x', nvars=None, order=None, **kwds): 
    324329    """Build a constraints function given a string of linear constraints. 
    325330Returns a constraints function.  
     
    334339        >>> constraints = '''x1 = x3 + 2. 
    335340        ...     x3 = x4*2.''' 
    336         >>> f = build_linear(constraints) 
     341        >>> f = _build_linear(constraints, nvars=4) 
    337342        >>> f([1.0, 0.0, 0.0, 1.0]) 
    338343        [4.0, 0.0, 2.0, 1.0] 
     
    378383    redundancies, but that should be ok. 
    379384    """ 
    380     # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)') 
    381385    strict = False # if True, force to use 'permutations' code 
    382386    warn = True  # if True, don't supress warning about old versions of sympy 
     
    551555        >>> constraints = '''x1 = x3 + 2. 
    552556        ...     x3 = x4*2.''' 
    553         >>> f = build_constraint(constraints) 
     557        >>> f = build_constraint(constraints, nvars=4) 
    554558        >>> f([1.0, 0.0, 0.0, 1.0]) 
    555559        [4.0, 0.0, 2.0, 1.0] 
     560        >>> constraints = ''' 
     561        ...     spread([x1,x2]) - 1.0 = mean([x1,x2])    
     562        ...     mean([x1,x2,x3]) = x3''' 
     563        >>> f = _build_nonlinear(constraints) 
     564        >>> f([1.0, 2.0, 3.0]) 
     565        [1.0, 5.0, 3.0] 
    556566 
    557567Additional Inputs: 
     
    581591        such that it does not immediately violate the given bounds. 
    582592""" 
    583     # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)') 
    584593    try: 
    585         return build_linear(constraints, variables=variables, \ 
    586                             nvars=nvars, order=order, **kwds) 
     594        return _build_linear(constraints, variables=variables, \ 
     595                             nvars=nvars, order=order, **kwds) 
    587596    except: 
    588         return build_nonlinear(constraints, variables=variables, \ 
    589                                nvars=nvars, order=order, **kwds) 
    590  
    591  
    592 def build_nonlinear(constraints, variables='x', nvars=None, order=None, **kwds): 
     597        return _build_nonlinear(constraints, variables=variables, \ 
     598                                nvars=nvars, order=order, **kwds) 
     599 
     600 
     601def _build_nonlinear(constraints, variables='x', nvars=None, order=None,**kwds): 
    593602    """Build a constraints function given a string of nonlinear constraints. 
    594603Returns a constraints function.  
     
    602611    For example: 
    603612        >>> constraints = '''x2 = x4*3. + (x1*x3)**x1''' 
    604         >>> f = build_nonlinear(constraints) 
     613        >>> f = _build_nonlinear(constraints, nvars=5) 
    605614        >>> f([1.0, 1.0, 1.0, 1.0, 1.0]) 
    606615        [1.0, 4.0, 1.0, 1.0, 1.0] 
     616        >>> constraints = ''' 
     617        ...     spread([x1,x2]) - 1.0 = mean([x1,x2])    
     618        ...     mean([x1,x2,x3]) = x3''' 
     619        >>> f = _build_nonlinear(constraints) 
     620        >>> f([1.0, 2.0, 3.0]) 
     621        [1.0, 5.0, 3.0] 
    607622 
    608623Additional Inputs: 
     
    632647        such that it does not immediately violate the given bounds. 
    633648""" 
    634     # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)') 
    635649    strict = False # if True, force to use 'permutations' code 
    636650    warn = True  # if True, don't supress warning about old versions of sympy 
  • branches/decorate/symbolic.py

    r614 r616  
    264264            constraint = fixed.strip() 
    265265 
    266             # Replace 'range', 'mean', and 'variance' (uses numpy, not mystic) 
    267             if constraint.find('range(') != -1: 
    268                 constraint = constraint.replace('range(', 'ptp(') # spread 
     266            # Replace 'spread', 'mean', and 'variance' (uses numpy, not mystic) 
     267            if constraint.find('spread(') != -1: 
     268                constraint = constraint.replace('spread(', 'ptp(') 
    269269            if constraint.find('mean(') != -1: 
    270                 constraint = constraint.replace('mean(', 'average(') # mean 
     270                constraint = constraint.replace('mean(', 'average(') 
    271271            if constraint.find('variance(') != -1: 
    272                 constraint = constraint.replace('variance(', 'var(') # variance 
     272                constraint = constraint.replace('variance(', 'var(') 
    273273 
    274274            # Sorting into equality and inequality constraints, and making all 
     
    350350            constraint = fixed.strip() 
    351351 
    352             # Replace 'range', 'mean', and 'variance' (uses mystic, not numpy) 
    353             if constraint.find('range(') != -1: 
    354                 constraint = constraint.replace('range(', 'spread(') 
     352            # Replace 'ptp', 'average', and 'var' (uses mystic, not numpy) 
    355353            if constraint.find('ptp(') != -1: 
    356354                constraint = constraint.replace('ptp(', 'spread(') 
     
    441439    globals = {} 
    442440    code = """from math import *; from numpy import *;""" 
     441    code += """from numpy import mean as average;""" # use np.mean not average 
    443442   #code += """from mystic.math.measures import spread, variance, mean;""" 
    444443    code = compile(code, '<string>', 'exec') 
     
    511510    globals = {} 
    512511    code = """from math import *; from numpy import *;""" 
     512    code += """from numpy import mean as average;""" # use np.mean not average 
    513513    code += """from mystic.math.measures import spread, variance, mean;""" 
    514514    code += """from mystic.math.measures import impose_spread, impose_mean;""" 
  • branches/decorate/test_symbolic.py

    r613 r616  
    11from symbolic import * 
     2from _symbolic import * 
    23from mystic.math import almostEqual 
    34from penalty import as_constraint 
     
    3839  assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10) 
    3940 
    40 def test_impose_penalty(): 
     41def test_generate_constraint(): 
    4142 
    4243  constraints = """ 
    4344  mean([x1, x2, x3]) = 5.0 
    44   range([x1, x2, x3]) = 10.0""" 
     45  spread([x1, x2, x3]) = 10.0""" 
    4546 
    4647  from mystic.math.measures import mean, spread 
     
    5253  assert almostEqual(constraint([1,2,3]), [0.0,5.0,10.0], 1e-10) 
    5354 
     55def test_build_constraint(): 
     56 
     57  constraints = """ 
     58  spread([x1,x2]) - 1.0 = mean([x1,x2])    
     59  mean([x1,x2,x3]) = x3""" 
     60 
     61  from mystic.math.measures import mean, spread 
     62  constraint = build_constraint(constraints) 
     63  x = constraint([1.0, 2.0, 3.0]) 
     64  assert all(x) == all([1.0, 5.0, 3.0]) 
     65  assert mean(x) == x[2] 
     66  assert spread(x[:-1]) - 1.0 == mean(x[:-1]) 
     67 
    5468 
    5569if __name__ == '__main__': 
    5670  test_generate_penalty() 
    5771  test_numpy_penalty() 
    58   test_impose_penalty() 
     72  test_generate_constraint() 
     73  test_build_constraint() 
    5974 
Note: See TracChangeset for help on using the changeset viewer.