- Timestamp:
- 12/19/12 17:34:01 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/_symbolic.py
r615 r616 1 1 from symbolic import * 2 from wrapper import isbounded 3 from penalty import issolution 2 4 from mystic.tools import permutations 3 5 … … 270 272 # default is _locals with numpy and math imported 271 273 # 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 273 278 code = compile(code, '<string>', 'exec') 274 279 exec code in _locals … … 321 326 322 327 323 def build_linear(constraints, variables='x', nvars=None, order=None, **kwds):328 def _build_linear(constraints, variables='x', nvars=None, order=None, **kwds): 324 329 """Build a constraints function given a string of linear constraints. 325 330 Returns a constraints function. … … 334 339 >>> constraints = '''x1 = x3 + 2. 335 340 ... x3 = x4*2.''' 336 >>> f = build_linear(constraints)341 >>> f = _build_linear(constraints, nvars=4) 337 342 >>> f([1.0, 0.0, 0.0, 1.0]) 338 343 [4.0, 0.0, 2.0, 1.0] … … 378 383 redundancies, but that should be ok. 379 384 """ 380 # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)')381 385 strict = False # if True, force to use 'permutations' code 382 386 warn = True # if True, don't supress warning about old versions of sympy … … 551 555 >>> constraints = '''x1 = x3 + 2. 552 556 ... x3 = x4*2.''' 553 >>> f = build_constraint(constraints )557 >>> f = build_constraint(constraints, nvars=4) 554 558 >>> f([1.0, 0.0, 0.0, 1.0]) 555 559 [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] 556 566 557 567 Additional Inputs: … … 581 591 such that it does not immediately violate the given bounds. 582 592 """ 583 # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)')584 593 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) 587 596 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 601 def _build_nonlinear(constraints, variables='x', nvars=None, order=None,**kwds): 593 602 """Build a constraints function given a string of nonlinear constraints. 594 603 Returns a constraints function. … … 602 611 For example: 603 612 >>> constraints = '''x2 = x4*3. + (x1*x3)**x1''' 604 >>> f = build_nonlinear(constraints)613 >>> f = _build_nonlinear(constraints, nvars=5) 605 614 >>> f([1.0, 1.0, 1.0, 1.0, 1.0]) 606 615 [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] 607 622 608 623 Additional Inputs: … … 632 647 such that it does not immediately violate the given bounds. 633 648 """ 634 # XXX: sympy fails on 'math' functions of variables (e.g. 'cos(x1)')635 649 strict = False # if True, force to use 'permutations' code 636 650 warn = True # if True, don't supress warning about old versions of sympy -
branches/decorate/symbolic.py
r614 r616 264 264 constraint = fixed.strip() 265 265 266 # Replace ' range', 'mean', and 'variance' (uses numpy, not mystic)267 if constraint.find(' range(') != -1:268 constraint = constraint.replace(' range(', 'ptp(') # spread266 # Replace 'spread', 'mean', and 'variance' (uses numpy, not mystic) 267 if constraint.find('spread(') != -1: 268 constraint = constraint.replace('spread(', 'ptp(') 269 269 if constraint.find('mean(') != -1: 270 constraint = constraint.replace('mean(', 'average(') # mean270 constraint = constraint.replace('mean(', 'average(') 271 271 if constraint.find('variance(') != -1: 272 constraint = constraint.replace('variance(', 'var(') # variance272 constraint = constraint.replace('variance(', 'var(') 273 273 274 274 # Sorting into equality and inequality constraints, and making all … … 350 350 constraint = fixed.strip() 351 351 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) 355 353 if constraint.find('ptp(') != -1: 356 354 constraint = constraint.replace('ptp(', 'spread(') … … 441 439 globals = {} 442 440 code = """from math import *; from numpy import *;""" 441 code += """from numpy import mean as average;""" # use np.mean not average 443 442 #code += """from mystic.math.measures import spread, variance, mean;""" 444 443 code = compile(code, '<string>', 'exec') … … 511 510 globals = {} 512 511 code = """from math import *; from numpy import *;""" 512 code += """from numpy import mean as average;""" # use np.mean not average 513 513 code += """from mystic.math.measures import spread, variance, mean;""" 514 514 code += """from mystic.math.measures import impose_spread, impose_mean;""" -
branches/decorate/test_symbolic.py
r613 r616 1 1 from symbolic import * 2 from _symbolic import * 2 3 from mystic.math import almostEqual 3 4 from penalty import as_constraint … … 38 39 assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10) 39 40 40 def test_ impose_penalty():41 def test_generate_constraint(): 41 42 42 43 constraints = """ 43 44 mean([x1, x2, x3]) = 5.0 44 range([x1, x2, x3]) = 10.0"""45 spread([x1, x2, x3]) = 10.0""" 45 46 46 47 from mystic.math.measures import mean, spread … … 52 53 assert almostEqual(constraint([1,2,3]), [0.0,5.0,10.0], 1e-10) 53 54 55 def 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 54 68 55 69 if __name__ == '__main__': 56 70 test_generate_penalty() 57 71 test_numpy_penalty() 58 test_impose_penalty() 72 test_generate_constraint() 73 test_build_constraint() 59 74
Note: See TracChangeset
for help on using the changeset viewer.