Changeset 617 for branches


Ignore:
Timestamp:
12/19/12 21:38:17 (3 years ago)
Author:
mmckerns
Message:

commented out guess and *bounds options in build_*;
throw Error for inequalities in most _symbolic methods;
don't return _classify_variables... print instead;
improve docs and some renaming of kwds options;

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/_symbolic.py

    r616 r617  
    11from symbolic import * 
    2 from wrapper import isbounded 
    3 from penalty import issolution 
    42from mystic.tools import permutations 
     3#from wrapper import isbounded 
     4#from penalty import issolution 
    55 
    66def _classify_variables(constraints, variables='x', nvars=None):  
     
    3838        found in the constraints equation string. 
    3939""" 
     40    if ">" in constraints or "<" in constraints: 
     41        raise NotImplementedError, "cannot classify inequalities"  
     42 
    4043    #XXX: use simplify_symbolic? or first if not in form xi = ... ? 
    4144    if list_or_tuple_or_ndarray(variables): 
     
    155158        found in the constraints equation string. 
    156159""" 
     160    if ">" in constraints or "<" in constraints: 
     161        raise NotImplementedError, "cannot simplify inequalities"  
     162 
    157163    #XXX: if constraints contain x1,x2,x4 for 'x', should x3 be in code,xlist? 
    158164    if list_or_tuple_or_ndarray(variables): 
     
    255261        else: ndim = 0 
    256262 
    257     warn = True  # if True, don't supress warning about old versions of sympy 
     263    warn = True  # if True, don't supress warnings 
    258264    if kwds.has_key('warn'): warn = kwds['warn'] 
    259265    locals = kwds.get('locals', None) 
     
    358364        By default, increasing order (i.e. 1, 2, ...) is used. order 
    359365        must enumerate all variables, hence len(order) == nvars. 
    360  
     366""" 
     367    """ 
    361368Further Inputs: 
    362     details -- boolean for whether or not to also return 'variable_info'. 
    363369    guess -- list of parameter values proposed to solve the constraints. 
    364370    lower_bounds -- list of lower bounds on solution values. 
     
    368374        upper bounds, the constraints function must be formulated in a manner 
    369375        such that it does not immediately violate the given bounds. 
    370     """ 
    371     """ 
     376 
    372377    Returns a constraints function with the constraints simplified nicely. 
    373378    For example, the constraints function is: 
     
    382387    inverter/simplifier will probably also work! It just won't get rid of 
    383388    redundancies, but that should be ok. 
    384     """ 
    385     strict = False # if True, force to use 'permutations' code 
    386     warn = True  # if True, don't supress warning about old versions of sympy 
    387     verbose = False # if False, keep information to a minimum 
    388     details = False # if True, print details from _classify_variables 
    389     guess = None 
    390     upper_bounds = None 
    391     lower_bounds = None 
     389""" 
     390    permute = False # if True, force to use 'permutations' code 
     391    warn = True  # if True, don't supress warnings 
     392    verbose = False # if True, print tempcode and _classify_variables 
     393#   guess = None 
     394#   upper_bounds = None 
     395#   lower_bounds = None 
    392396    #-----------------------undocumented------------------------------- 
    393     if kwds.has_key('strict'): strict = kwds['strict'] 
     397    if kwds.has_key('permute'): permute = kwds['permute'] 
    394398    if kwds.has_key('warn'): warn = kwds['warn'] 
    395399    if kwds.has_key('verbose'): verbose = kwds['verbose'] 
     400#   if kwds.has_key('guess'): guess = kwds['guess'] 
     401#   if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 
     402#   if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 
    396403    #------------------------------------------------------------------ 
    397     if kwds.has_key('details'): details = kwds['details'] 
    398     if kwds.has_key('guess'): guess = kwds['guess'] 
    399     if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 
    400     if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 
    401404 
    402405    if list_or_tuple_or_ndarray(variables): 
     
    411414        else: ndim = 0 
    412415    if nvars: ndim = nvars 
    413     elif guess: ndim = len(guess) 
    414     elif lower_bounds: ndim = len(lower_bounds) 
    415     elif upper_bounds: ndim = len(upper_bounds) 
     416#   elif guess: ndim = len(guess) 
     417#   elif lower_bounds: ndim = len(lower_bounds) 
     418#   elif upper_bounds: ndim = len(upper_bounds) 
    416419 
    417420    # The following code attempts to construct something like: 
     
    429432        from sympy import Eq, Symbol 
    430433        from sympy import solve as symsol 
    431     except ImportError: 
    432         if warn: print "Warning: sympy not installed."# Equation will not be simplified." 
     434    except ImportError: # Equation will not be simplified." 
     435        if warn: print "Warning: sympy not installed." 
    433436        solv = generate_solvers(constraints, variables=varname) 
    434437        return generate_constraint(solv) 
     
    443446 
    444447    # Figure out if trying various permutations is necessary 
    445     if (guess and lower_bounds) or (guess and upper_bounds): 
    446         strict = True 
     448#   if (guess and lower_bounds) or (guess and upper_bounds): 
     449#       permute = True 
    447450 
    448451    xinlist = xlist.split(',')[:-1] 
     
    451454        xorder = list(asarray(xinlist)[list(order)]) 
    452455 
    453     if strict: 
     456    if permute: 
    454457        # If there are strict bounds and initial x value, form a constraints  
    455458        # function for each permutation. 
     
    474477 
    475478            if soln == None and warn: 
    476                 print "Warning: constraints seem to be inconsistent." 
     479                print "Warning: sympy could not simplify equation." 
    477480 
    478481            solvedstring = "" 
     
    499502            solv = generate_solvers(string, variables=varname) 
    500503            cf = generate_constraint(solv) 
    501             if guess: compatible = isbounded(cf, guess, min=lower_bounds, \ 
    502                                                         max=upper_bounds) 
    503             else: compatible = True 
     504#           if guess: compatible = isbounded(cf, guess, min=lower_bounds, \ 
     505#                                                       max=upper_bounds) 
     506#           else: compatible = True 
     507            compatible = True #(due to commented out 'guess') 
    504508            if compatible: 
    505509                #print string # Debugging 
    506                 if details: 
    507                     info = _classify_variables(string, varname, ndim) 
    508                     return [cf, info] 
    509                 else: 
    510                     return cf# Return the first compatible constraints function 
     510                if verbose: 
     511                    print _classify_variables(string, varname, ndim) 
     512                return cf # Return the first compatible constraints function 
    511513            else: 
    512514                _constraints = cf # Save for later and try other permutations 
    513515        # Perhaps raising an Exception is better? But sometimes it's ok... 
    514         warning='Warning: guess does not satisfy both constraints and bounds.\n\ 
    515 Constraints function may be faulty.' 
     516       #warning='Warning: guess incompatible with constraints and bounds.' 
     517        warning='Warning: an error occurred in building the constraints.' 
    516518        if warn: print warning 
    517         if details: 
    518             info = _classify_variables(string, varname, ndim) 
    519             return [cf, info] 
     519        if verbose: 
     520            print _classify_variables(string, varname, ndim) 
    520521        return cf 
    521522 
     
    528529        exec code in globals(), locals() #FIXME: insecure 
    529530        if soln == None and warn: 
    530             print "Warning: constraints seem to be inconsistent." 
     531            print "Warning: sympy could not simplify equation." 
    531532 
    532533        solvedstring = "" 
     
    536537        solv = generate_solvers(solvedstring, variables=varname) 
    537538        cf = generate_constraint(solv) 
    538         if details: 
    539             info = _classify_variables(solvedstring, varname, ndim) 
    540             return [cf, info] 
     539        if verbose: 
     540            print _classify_variables(solvedstring, varname, ndim) 
    541541        return cf 
    542542 
     
    580580        By default, increasing order (i.e. 1, 2, ...) is used. order 
    581581        must enumerate all variables, hence len(order) == nvars. 
    582  
     582""" 
     583    """ 
    583584Further Inputs: 
    584     details -- boolean for whether or not to also return 'variable_info'. 
    585585    guess -- list of parameter values proposed to solve the constraints. 
    586586    lower_bounds -- list of lower bounds on solution values. 
     
    636636        By default, increasing order (i.e. 1, 2, ...) is used. order 
    637637        must enumerate all variables, hence len(order) == nvars. 
    638  
     638""" 
     639    """ 
    639640Further Inputs: 
    640     details -- boolean for whether or not to also return 'variable_info'. 
    641641    guess -- list of parameter values proposed to solve the constraints. 
    642642    lower_bounds -- list of lower bounds on solution values. 
     
    647647        such that it does not immediately violate the given bounds. 
    648648""" 
    649     strict = False # if True, force to use 'permutations' code 
    650     warn = True  # if True, don't supress warning about old versions of sympy 
    651     verbose = False # if True, return all permutations 
    652     details = False # if True, print details from _classify_variables 
    653     guess = None 
    654     upper_bounds = None 
    655     lower_bounds = None 
     649    permute = False # if True, force to use 'permutations' code 
     650    _all = False # if True, return all permutations 
     651    warn = True  # if True, don't supress warnings 
     652    verbose = False # if True, print details from _classify_variables 
     653#   guess = None 
     654#   upper_bounds = None 
     655#   lower_bounds = None 
    656656    #-----------------------undocumented------------------------------- 
    657     if kwds.has_key('strict'): strict = kwds['strict'] 
     657    if kwds.has_key('all'): _all = kwds['all'] 
     658    if kwds.has_key('permute'): permute = kwds['permute'] 
    658659    if kwds.has_key('warn'): warn = kwds['warn'] 
    659660    if kwds.has_key('verbose'): verbose = kwds['verbose'] 
     661#   if kwds.has_key('guess'): guess = kwds['guess'] 
     662#   if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 
     663#   if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 
    660664    #------------------------------------------------------------------ 
    661     if kwds.has_key('details'): details = kwds['details'] 
    662     if kwds.has_key('guess'): guess = kwds['guess'] 
    663     if kwds.has_key('upper_bounds'): upper_bounds = kwds['upper_bounds'] 
    664     if kwds.has_key('lower_bounds'): lower_bounds = kwds['lower_bounds'] 
    665665 
    666666    if list_or_tuple_or_ndarray(variables): 
     
    675675        else: ndim = 0 
    676676    if nvars: ndim = nvars 
    677     elif guess: ndim = len(guess) 
    678     elif lower_bounds: ndim = len(lower_bounds) 
    679     elif upper_bounds: ndim = len(upper_bounds) 
     677#   elif guess: ndim = len(guess) 
     678#   elif lower_bounds: ndim = len(lower_bounds) 
     679#   elif upper_bounds: ndim = len(upper_bounds) 
    680680 
    681681    # Figure out if trying various permutations is necessary 
    682     if (guess and lower_bounds) or (guess and upper_bounds): 
    683         strict = True 
     682#   if (guess and lower_bounds) or (guess and upper_bounds): 
     683#       permute = True 
    684684 
    685685    eqns = constraints.splitlines() 
     
    763763        cf = generate_constraint(solv) 
    764764 
    765         if verbose: 
     765        if _all: 
    766766            complete_list.append(cf) 
    767767            continue 
    768768 
    769         if strict and guess: 
    770             try: # catches trying to square root a negative number, for example. 
    771                 cf(guess) 
    772             except ValueError: 
    773                 continue 
    774             compatible = isbounded(cf, guess, min=lower_bounds, \ 
    775                                               max=upper_bounds) 
    776             satisfied = issolution(cf, cf(guess)) 
    777             if compatible and satisfied: 
    778                 if details: 
    779                     info = _classify_variables('\n'.join(simplified), varname, ndim) 
    780                     return [cf, info] 
    781                 else: 
    782                     return cf 
    783         else: #not strict or not guess 
    784             if details: 
    785                 info = _classify_variables('\n'.join(simplified), varname, ndim) 
    786                 return [cf, info] 
    787             else: 
    788                 return cf 
    789  
    790     warning='Warning: guess does not satisfy both constraints and bounds.\n\ 
    791 Constraints may not be enforced correctly. Constraints function may be faulty.' 
     769#       if permute and guess: 
     770#           try: # catches trying to square root a negative number, for example. 
     771#               cf(guess) 
     772#           except ValueError: 
     773#               continue 
     774#           compatible = isbounded(cf, guess, min=lower_bounds, \ 
     775#                                             max=upper_bounds) 
     776#           satisfied = issolution(cf, cf(guess)) 
     777#           if compatible and satisfied: 
     778#               if verbose: 
     779#                   print _classify_variables('\n'.join(simplified), varname, ndim) 
     780#               return cf 
     781#       else: #not permute or not guess 
     782        if True: #(due to commented out 'guess') 
     783            if verbose: 
     784                print _classify_variables('\n'.join(simplified), varname, ndim) 
     785            return cf 
     786 
     787#   warning='Warning: guess incompatible with constraints and bounds.' 
     788    warning='Warning: an error occurred in building the constraints.' 
    792789    if warn: print warning 
    793     if details: 
    794         info = _classify_variables('\n'.join(simplified), varname, ndim) 
    795         return [cf, info] 
    796  
    797790    if verbose: 
     791        print _classify_variables('\n'.join(simplified), varname, ndim) 
     792    if _all: 
    798793        return complete_list 
    799794    return cf 
Note: See TracChangeset for help on using the changeset viewer.