Changeset 686
- Timestamp:
- 06/11/13 17:59:35 (3 years ago)
- Location:
- mystic
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/measures.py
r646 r686 338 338 inequality = ""; equality = ""; equality2 = "" 339 339 for i in range(ndim): 340 inequality += "x%s >= 0.0\n" % (i +1) # positive341 equality += "x%s + " % (i +1) # normalized342 equality2 += "%s * x%s + " % (float(samples[i]),(i +1)) # mean340 inequality += "x%s >= 0.0\n" % (i) # positive 341 equality += "x%s + " % (i) # normalized 342 equality2 += "%s * x%s + " % (float(samples[i]),(i)) # mean 343 343 344 344 equality += "0.0 = %s\n" % float(norm) … … 383 383 equality = ""; equality2 = ""; equality3 = "" 384 384 for i in range(ndim): 385 inequality += "x%s >= 0.0\n" % (i +1) # positive386 equality += "x%s + " % (i +1) # normalized387 equality2 += "%s * x%s + " % (float(samples[i]),(i +1)) # mean388 equality3 += "x%s*(%s-%s)**2 + " % ((i +1),float(samples[i]),m) # var385 inequality += "x%s >= 0.0\n" % (i) # positive 386 equality += "x%s + " % (i) # normalized 387 equality2 += "%s * x%s + " % (float(samples[i]),(i)) # mean 388 equality3 += "x%s*(%s-%s)**2 + " % ((i),float(samples[i]),m) # var 389 389 390 390 equality += "0.0 = %s\n" % float(norm) -
mystic/mystic/_symbolic.py
r645 r686 2 2 # >>> from sympy import Eq, Symbol 3 3 # >>> from sympy import solve as symsol 4 # >>> x0 = Symbol('x0') 4 5 # >>> x1 = Symbol('x1') 5 6 # >>> x2 = Symbol('x2') 6 # >>> x3 = Symbol('x3') 7 # >>> eq1 = Eq(x2, x1 - 2.) 8 # >>> eq2 = Eq(x2, x3*2.) 9 # >>> soln = symsol([eq2, eq1], [x1, x2, x3]) 7 # >>> eq1 = Eq(x1, x0 - 2.) 8 # >>> eq2 = Eq(x1, x2*2.) 9 # >>> soln = symsol([eq2, eq1], [x0, x1, x2]) 10 10 11 11 from mystic.tools import permutations … … 27 27 For example: 28 28 >>> constraints = ''' 29 ... x 1 = x5**230 ... x 3 = x4 + x5'''29 ... x0 = x4**2 30 ... x2 = x3 + x4''' 31 31 >>> _classify_variables(constraints, nvars=5) 32 {'dependent':['x 1','x3'], 'independent':['x4','x5'], 'unconstrained':['x2']}33 >>> constraints = ''' 34 ... x 1 = x5**235 ... x 5 - x4= 0.36 ... x 5 - x1 = x3'''32 {'dependent':['x0','x2'], 'independent':['x3','x4'], 'unconstrained':['x1']} 33 >>> constraints = ''' 34 ... x0 = x4**2 35 ... x4 - x3 = 0. 36 ... x4 - x0 = x2''' 37 37 >>> _classify_variables(constraints, nvars=5) 38 {'dependent': ['x 1','x3','x5'], 'independent': ['x4'], 'unconstrained': ['x2']}38 {'dependent': ['x0','x2','x4'], 'independent': ['x3'], 'unconstrained': ['x1']} 39 39 40 40 Additional Inputs: 41 41 nvars -- number of variables. Includes variables not explicitly 42 given by the constraint equations (e.g. 'x 2' in the example above).42 given by the constraint equations (e.g. 'x1' in the example above). 43 43 variables -- desired variable name. Default is 'x'. A list of variable 44 44 name strings is also accepted for when desired variable names … … 52 52 #XXX: use solve? or first if not in form xi = ... ? 53 53 if list_or_tuple_or_ndarray(variables): 54 if nvars : variables = variables[:nvars]54 if nvars != None: variables = variables[:nvars] 55 55 constraints = replace_variables(constraints, variables) 56 56 varname = '$' … … 59 59 varname = variables # varname used below instead of variables 60 60 myvar = get_variables(constraints, variables) 61 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 61 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 62 62 else: ndim = 0 63 if nvars : ndim = nvars63 if nvars != None: ndim = nvars 64 64 65 65 eqns = constraints.splitlines() 66 indices = range( 1, ndim+1)66 indices = range(ndim) 67 67 dep = [] 68 68 indep = [] 69 69 for eqn in eqns: # find which variables are used 70 70 if eqn: 71 for var in range( 1, ndim+1):71 for var in range(ndim): 72 72 if indices.count(var) != 0: 73 73 if eqn.find(varname + str(var)) != -1: … … 97 97 dep.append(_dep[0]) 98 98 indep.remove(_dep[0]) 99 #FIXME: 'equivalent' equations not ignored (e.g. x 3=x3; or x3=1, 2*x3=2)99 #FIXME: 'equivalent' equations not ignored (e.g. x2=x2; or x2=1, 2*x2=2) 100 100 """These are good: 101 101 >>> constraints = ''' 102 ... x 1 = x5**2103 ... x 3 - x5 - x4= 0.'''102 ... x0 = x4**2 103 ... x2 - x4 - x3 = 0.''' 104 104 >>> _classify_variables(constraints, nvars=5) 105 {'dependent': ['x 1','x3'], 'independent': ['x4','x5'], 'unconstrained': ['x2']}105 {'dependent': ['x0','x2'], 'independent': ['x3','x4'], 'unconstrained': ['x1']} 106 106 >>> constraints = ''' 107 ... x 1 + x3= 0.108 ... x 1 + 2*x3= 0.'''107 ... x0 + x2 = 0. 108 ... x0 + 2*x2 = 0.''' 109 109 >>> _classify_variables(constraints, nvars=5) 110 {'dependent': ['x 1','x3'], 'independent': [], 'unconstrained': ['x2','x4','x5']}110 {'dependent': ['x0','x2'], 'independent': [], 'unconstrained': ['x1','x3','x4']} 111 111 112 112 This is a bug: 113 113 >>> constraints = ''' 114 ... x 1 + x3= 0.115 ... 2*x 1 + 2*x3= 0.'''114 ... x0 + x2 = 0. 115 ... 2*x0 + 2*x2 = 0.''' 116 116 >>> _classify_variables(constraints, nvars=5) 117 {'dependent': ['x 1','x3'], 'independent': [], 'unconstrained': ['x2','x4','x5']}117 {'dependent': ['x0','x2'], 'independent': [], 'unconstrained': ['x1','x3','x4']} 118 118 """ #XXX: should simplify first? 119 119 dep.sort() … … 121 121 # return the actual variable names (not the indicies) 122 122 if varname == variables: # then was single variable 123 variables = [varname+str(i) for i in range( 1,ndim+1)]124 dep = [variables[i -1] for i in dep]125 indep = [variables[i -1] for i in indep]126 indices = [variables[i -1] for i in indices]123 variables = [varname+str(i) for i in range(ndim)] 124 dep = [variables[i] for i in dep] 125 indep = [variables[i] for i in indep] 126 indices = [variables[i] for i in indices] 127 127 d = {'dependent':dep, 'independent':indep, 'unconstrained':indices} 128 128 return d … … 143 143 For example: 144 144 >>> constraints = ''' 145 ... x 1 = x5**2146 ... x 5 - x4= 0.147 ... x 5 - x1 = x3'''145 ... x0 = x4**2 146 ... x4 - x3 = 0. 147 ... x4 - x0 = x2''' 148 148 >>> code, lhs, rhs, vars, neqn = _prepare_sympy(constraints, nvars=5) 149 149 >>> print code 150 x0=Symbol('x0') 150 151 x1=Symbol('x1') 151 152 x2=Symbol('x2') 152 153 x3=Symbol('x3') 153 154 x4=Symbol('x4') 154 x5=Symbol('x5')155 155 rand = Symbol('rand') 156 156 >>> print lhs, rhs 157 ['x 1 ', 'x5 - x4 ', 'x5 - x1 '] [' x5**2', ' 0.', ' x3']157 ['x0 ', 'x4 - x3 ', 'x4 - x0 '] [' x4**2', ' 0.', ' x2'] 158 158 print "%s in %s eqns" % (vars, neqn) 159 x 1,x2,x3,x4,x5, in 3 eqns159 x0,x1,x2,x3,x4, in 3 eqns 160 160 161 161 Additional Inputs: 162 162 nvars -- number of variables. Includes variables not explicitly 163 given by the constraint equations (e.g. 'x 2' in the example above).163 given by the constraint equations (e.g. 'x1' in the example above). 164 164 variables -- desired variable name. Default is 'x'. A list of variable 165 165 name strings is also accepted for when desired variable names … … 171 171 172 172 from mystic.symbolic import replace_variables, get_variables 173 #XXX: if constraints contain x 1,x2,x4 for 'x', should x3be in code,xlist?173 #XXX: if constraints contain x0,x1,x3 for 'x', should x2 be in code,xlist? 174 174 if list_or_tuple_or_ndarray(variables): 175 if nvars : variables = variables[:nvars]175 if nvars != None: variables = variables[:nvars] 176 176 constraints = replace_variables(constraints, variables, markers='_') 177 177 varname = '_' … … 180 180 varname = variables # varname used below instead of variables 181 181 myvar = get_variables(constraints, variables) 182 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 182 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 183 183 else: ndim = 0 184 if nvars : ndim = nvars184 if nvars != None: ndim = nvars 185 185 186 186 # split constraints_str into lists of left hand sides and right hand sides … … 207 207 # First create list of x variables 208 208 xlist = "" 209 for i in range( 1, ndim + 1):209 for i in range(ndim): 210 210 xn = varname + str(i) 211 211 xlist += xn + "," … … 213 213 # Start constructing the code string 214 214 code = "" 215 for i in range( 1, ndim + 1):215 for i in range(ndim): 216 216 xn = varname + str(i) 217 217 code += xn + '=' + "Symbol('" + xn + "')\n" … … 231 231 232 232 For example: 233 >>> equation = "x 2 - 3. = x1*x3"233 >>> equation = "x1 - 3. = x0*x2" 234 234 >>> print _solve_single(equation) 235 x 1 = -(3.0 - x2)/x3235 x0 = -(3.0 - x1)/x2 236 236 237 237 Additional Inputs: … … 244 244 245 245 For example: 246 >>> equation = "x 2 - 3. = x1*x3"247 >>> print _solve_single(equation, target='x 2')248 x 2 = 3.0 + x1*x3246 >>> equation = "x1 - 3. = x0*x2" 247 >>> print _solve_single(equation, target='x1') 248 x1 = 3.0 + x0*x2 249 249 250 250 Further Inputs: … … 276 276 from mystic.symbolic import replace_variables, get_variables 277 277 if list_or_tuple_or_ndarray(variables): 278 if nvars : variables = variables[:nvars]278 if nvars != None: variables = variables[:nvars] 279 279 constraints = replace_variables(constraint, variables, markers='_') 280 280 varname = '_' … … 287 287 varname = variables # varname used below instead of variables 288 288 myvar = get_variables(constraint, variables) 289 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 289 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 290 290 else: ndim = 0 291 if nvars : ndim = nvars291 if nvars != None: ndim = nvars 292 292 293 293 # create function to replace "_" with original variables … … 295 295 if list_or_tuple_or_ndarray(variables): 296 296 vars = get_variables(mystring,'_') 297 indices = [int(v.strip('_')) -1for v in vars]297 indices = [int(v.strip('_')) for v in vars] 298 298 for i in range(len(vars)): 299 299 mystring = mystring.replace(vars[i],variables[indices[i]]) … … 343 343 344 344 ######################################################################## 345 # solve each xi: symsol(single_equation, [x 1,x2,...,xi,...,xn])346 # returns: {x 1: f(xn,...), x2: f(xn,...), ..., xn: f(...,x1)}345 # solve each xi: symsol(single_equation, [x0,x1,...,xi,...,xn]) 346 # returns: {x0: f(xn,...), x1: f(xn,...), ..., xn: f(...,x0)} 347 347 if permute or not target: #XXX: the goal is solving *only one* equation 348 348 code += '_xlist = %s\n' % ','.join(targeted) … … 396 396 For example: 397 397 >>> constraints = ''' 398 ... x 1 - x3= 2.399 ... x 3 = x4*2.'''398 ... x0 - x2 = 2. 399 ... x2 = x3*2.''' 400 400 >>> print _solve_linear(constraints) 401 x 3 = 2.0*x4402 x 1 = 2.0 + 2.0*x4401 x2 = 2.0*x3 402 x0 = 2.0 + 2.0*x3 403 403 404 404 Additional Inputs: … … 414 414 For example: 415 415 >>> constraints = ''' 416 ... x 1 - x3= 2.417 ... x 3 = x4*2.'''418 >>> print _solve_linear(constraints, target=['x 4','x3'])419 x 4 = -1.0 + 0.5*x1420 x 3 = -2.0 + x1416 ... x0 - x2 = 2. 417 ... x2 = x3*2.''' 418 >>> print _solve_linear(constraints, target=['x3','x2']) 419 x3 = -1.0 + 0.5*x0 420 x2 = -2.0 + x0 421 421 422 422 Further Inputs: … … 442 442 from mystic.symbolic import replace_variables, get_variables 443 443 if list_or_tuple_or_ndarray(variables): 444 if nvars : variables = variables[:nvars]444 if nvars != None: variables = variables[:nvars] 445 445 _constraints = replace_variables(constraints, variables, '_') 446 446 varname = '_' … … 453 453 varname = variables # varname used below instead of variables 454 454 myvar = get_variables(constraints, variables) 455 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 455 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 456 456 else: ndim = 0 457 if nvars : ndim = nvars457 if nvars != None: ndim = nvars 458 458 459 459 # create function to replace "_" with original variables … … 461 461 if list_or_tuple_or_ndarray(variables): 462 462 vars = get_variables(mystring,'_') 463 indices = [int(v.strip('_')) -1for v in vars]463 indices = [int(v.strip('_')) for v in vars] 464 464 for i in range(len(vars)): 465 465 mystring = mystring.replace(vars[i],variables[indices[i]]) … … 524 524 _code = code 525 525 xlist = ','.join(perm).rstrip(',') #XXX: if not all, use target ? 526 # solve dependent xi: symsol([linear_system], [x 1,x2,...,xi,...,xn])527 # returns: {x 1: f(xn,...), x2: f(xn,...), ...}526 # solve dependent xi: symsol([linear_system], [x0,x1,...,xi,...,xn]) 527 # returns: {x0: f(xn,...), x1: f(xn,...), ...} 528 528 _code += 'soln = symsol([' + eqlist + '], [' + xlist + '])' 529 529 if verbose: print _code … … 587 587 For example: 588 588 >>> constraints = ''' 589 ... x 1 - x3= 2.590 ... x 3 = x4*2.'''589 ... x0 - x2 = 2. 590 ... x2 = x3*2.''' 591 591 >>> print solve(constraints) 592 x 3 = 2.0*x4593 x 1 = 2.0 + 2.0*x4594 >>> constraints = ''' 595 ... spread([x 1,x2]) - 1.0 = mean([x1,x2])596 ... mean([x 1,x2,x3]) = x3'''592 x2 = 2.0*x3 593 x0 = 2.0 + 2.0*x3 594 >>> constraints = ''' 595 ... spread([x0,x1]) - 1.0 = mean([x0,x1]) 596 ... mean([x0,x1,x2]) = x2''' 597 597 >>> print solve(constraints) 598 x 1 = -0.5 + 0.5*x3599 x 2 = 0.5 + 1.5*x3598 x0 = -0.5 + 0.5*x2 599 x1 = 0.5 + 1.5*x2 600 600 601 601 Additional Inputs: … … 611 611 For example: 612 612 >>> constraints = ''' 613 ... x 1 - x3= 2.614 ... x 3 = x4*2.'''615 >>> print solve(constraints, target=['x 4','x3'])616 x 4 = -1.0 + 0.5*x1617 x 3 = -2.0 + x1613 ... x0 - x2 = 2. 614 ... x2 = x3*2.''' 615 >>> print solve(constraints, target=['x3','x2']) 616 x3 = -1.0 + 0.5*x0 617 x2 = -2.0 + x0 618 618 619 619 Further Inputs: … … 651 651 652 652 For example: 653 >>> constraints = '''x 2 = x4*3. + x1*x3'''653 >>> constraints = '''x1 = x3*3. + x0*x2''' 654 654 >>> print _solve_nonlinear(constraints) 655 x 1 = (x2 - 3.0*x4)/x3656 >>> constraints = ''' 657 ... spread([x 1,x2]) - 1.0 = mean([x1,x2])658 ... mean([x 1,x2,x3]) = x3'''655 x0 = (x1 - 3.0*x3)/x2 656 >>> constraints = ''' 657 ... spread([x0,x1]) - 1.0 = mean([x0,x1]) 658 ... mean([x0,x1,x2]) = x2''' 659 659 >>> print _solve_nonlinear(constraints) 660 x 1 = -0.5 + 0.5*x3661 x 2 = 0.5 + 1.5*x3660 x0 = -0.5 + 0.5*x2 661 x1 = 0.5 + 1.5*x2 662 662 663 663 Additional Inputs: … … 673 673 For example: 674 674 >>> constraints = ''' 675 ... spread([x 1,x2]) - 1.0 = mean([x1,x2])676 ... mean([x 1,x2,x3]) = x3'''677 >>> print _solve_nonlinear(constraints, target=['x 2'])678 x 2 = -0.833333333333333 + 0.166666666666667*x3679 x 1 = -0.5 + 0.5*x3675 ... spread([x0,x1]) - 1.0 = mean([x0,x1]) 676 ... mean([x0,x1,x2]) = x2''' 677 >>> print _solve_nonlinear(constraints, target=['x1']) 678 x1 = -0.833333333333333 + 0.166666666666667*x2 679 x0 = -0.5 + 0.5*x2 680 680 681 681 Further Inputs: … … 701 701 from mystic.symbolic import replace_variables, get_variables 702 702 if list_or_tuple_or_ndarray(variables): 703 if nvars : variables = variables[:nvars]703 if nvars != None: variables = variables[:nvars] 704 704 constraints = replace_variables(constraints, variables, '_') 705 705 varname = '_' … … 708 708 varname = variables # varname used below instead of variables 709 709 myvar = get_variables(constraints, variables) 710 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 710 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 711 711 else: ndim = 0 712 if nvars : ndim = nvars712 if nvars != None: ndim = nvars 713 713 714 714 # create function to replace "_" with original variables … … 716 716 if list_or_tuple_or_ndarray(variables): 717 717 vars = get_variables(mystring,'_') 718 indices = [int(v.strip('_')) -1for v in vars]718 indices = [int(v.strip('_')) for v in vars] 719 719 for i in range(len(vars)): 720 720 mystring = mystring.replace(vars[i],variables[indices[i]]) … … 734 734 neqns = len(actual_eqns) 735 735 736 xperms = [varname+str(i +1) for i in range(ndim)]736 xperms = [varname+str(i) for i in range(ndim)] 737 737 if target: 738 738 [target.remove(i) for i in target if i not in xperms] … … 754 754 # look into reducing the number of repeats? 755 755 for perm in xperms: 756 # Sort the list actual_eqns so any equation containing x 1is first, etc.756 # Sort the list actual_eqns so any equation containing x0 is first, etc. 757 757 sorted_eqns = [] 758 758 actual_eqns_copy = orig_eqns[:] … … 774 774 tempusedvar.sort() 775 775 nmissing = ndim - len(tempusedvar) 776 for m in range( 1, nmissing+1):776 for m in range(nmissing): 777 777 usedvars.append(varname + str(len(tempusedvar) + m)) 778 778 … … 819 819 if verbose: 820 820 print _classify_variables(simplified, variables, ndim) 821 if permute: #FIXME: target='x 4,x2' may order correct, while 'x2,x4' doesn't821 if permute: #FIXME: target='x3,x1' may order correct, while 'x1,x3' doesn't 822 822 filter = []; results = [] 823 823 for i in complete_list: -
mystic/mystic/symbolic.py
r645 r686 36 36 >>> h = [5.] 37 37 >>> print linear_symbolic(A,b,G,h) 38 1.0*x 1 + 0.0*x2 + 0.0*x3<= 5.039 3.0*x 1 + 4.0*x2 + 5.0*x3= 0.040 1.0*x 1 + 6.0*x2 + -9.0*x3= 0.038 1.0*x0 + 0.0*x1 + 0.0*x2 <= 5.0 39 3.0*x0 + 4.0*x1 + 5.0*x2 = 0.0 40 1.0*x0 + 6.0*x1 + -9.0*x2 = 0.0 41 41 """ 42 42 eqstring = "" … … 62 62 Asum = "" 63 63 for j in range(ndim): 64 Asum += str(A[i][j]) + '*x' + str(j +1) + ' + '64 Asum += str(A[i][j]) + '*x' + str(j) + ' + ' 65 65 eqstring += Asum.rstrip(' + ') + ' = ' + str(b[i]) + '\n' 66 66 … … 87 87 Gsum = "" 88 88 for j in range(ndim): 89 Gsum += str(G[i][j]) + '*x' + str(j +1) + ' + '89 Gsum += str(G[i][j]) + '*x' + str(j) + ' + ' 90 90 ineqstring += Gsum.rstrip(' + ') + ' <= ' + str(h[i]) + '\n' 91 91 totalconstraints = ineqstring + eqstring … … 105 105 be replaced in the order that they are provided, where if the 106 106 default marker "$i" is used, the first variable will be replaced 107 with "$ 1", the second with "$2", and so on.107 with "$0", the second with "$1", and so on. 108 108 109 109 For example: … … 111 111 >>> constraints = '''spam + eggs - 42''' 112 112 >>> print replace_variables(constraints, variables, 'x') 113 'x 1 + x2- 42'113 'x0 + x1 - 42' 114 114 115 115 Additional Inputs: … … 131 131 equations = replace_variables(constraints,variables,'_') 132 132 vars = get_variables(equations,'_') 133 indices = [int(v.strip('_')) -1for v in vars]133 indices = [int(v.strip('_')) for v in vars] 134 134 for i in range(len(vars)): 135 135 equations = equations.replace(vars[i],markers[indices[i]]) … … 162 162 ''' #FIXME: don't parse if __name__ in __builtins__, globals, or locals? 163 163 for i in indices: #FIXME: or better, use 're' pattern matching 164 constraints = constraints.replace(variables[i], marker + str(i +1))164 constraints = constraints.replace(variables[i], marker + str(i)) 165 165 return constraints.replace(marker, markers) 166 166 … … 199 199 equations = replace_variables(constraints,variables,'_') 200 200 vars = get_variables(equations,'_') 201 indices = [int(v.strip('_')) -1for v in vars]201 indices = [int(v.strip('_')) for v in vars] 202 202 varnamelist = [] 203 203 from numpy import sort … … 230 230 For example: 231 231 >>> constraints = ''' 232 ... x 3 = x1/2.233 ... x 1>= 0.'''232 ... x2 = x0/2. 233 ... x0 >= 0.''' 234 234 >>> penalty_parser(constraints, nvars=3) 235 235 (('-(x[0] - (0.))',), ('x[2] - (x[0]/2.)',)) … … 246 246 #ndim = len(get_variables(src(func), variables)) 247 247 if list_or_tuple_or_ndarray(variables): 248 if nvars : variables = variables[:nvars]248 if nvars != None: variables = variables[:nvars] 249 249 constraints = replace_variables(constraints, variables) 250 250 varname = '$' … … 253 253 varname = variables # varname used below instead of variables 254 254 myvar = get_variables(constraints, variables) 255 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 255 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 256 256 else: ndim = 0 257 if nvars : ndim = nvars257 if nvars != None: ndim = nvars 258 258 259 259 # Parse the constraints string … … 265 265 fixed = line 266 266 # Iterate in reverse in case ndim > 9. 267 indices = list(range( 1, ndim+1))267 indices = list(range(ndim)) 268 268 indices.reverse() 269 269 for i in indices: 270 fixed = fixed.replace(varname + str(i), 'x[' + str(i -1) + ']')270 fixed = fixed.replace(varname + str(i), 'x[' + str(i) + ']') 271 271 constraint = fixed.strip() 272 272 … … 317 317 For example: 318 318 >>> constraints = ''' 319 ... x 3 = x1/2.320 ... x 1>= 0.'''319 ... x2 = x0/2. 320 ... x0 >= 0.''' 321 321 >>> constraints_parser(constraints, nvars=3) 322 322 ('x[2] = x[0]/2.', 'x[0] = max(0., x[0])') … … 333 333 #ndim = len(get_variables(src(func), variables)) 334 334 if list_or_tuple_or_ndarray(variables): 335 if nvars : variables = variables[:nvars]335 if nvars != None: variables = variables[:nvars] 336 336 constraints = replace_variables(constraints, variables) 337 337 varname = '$' … … 340 340 varname = variables # varname used below instead of variables 341 341 myvar = get_variables(constraints, variables) 342 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) 342 if myvar: ndim = max([int(v.strip(varname)) for v in myvar]) + 1 343 343 else: ndim = 0 344 if nvars : ndim = nvars344 if nvars != None: ndim = nvars 345 345 346 346 # Parse the constraints string … … 351 351 fixed = line 352 352 # Iterate in reverse in case ndim > 9. 353 indices = list(range( 1, ndim+1))353 indices = list(range(ndim)) 354 354 indices.reverse() 355 355 for i in indices: 356 fixed = fixed.replace(varname + str(i), 'x[' + str(i -1) + ']')356 fixed = fixed.replace(varname + str(i), 'x[' + str(i) + ']') 357 357 constraint = fixed.strip() 358 358 … … 418 418 For example: 419 419 >>> constraints = ''' 420 ... x 1**2 = 2.5*x4- 5.0421 ... exp(x 3/x1) >= 7.0'''420 ... x0**2 = 2.5*x3 - 5.0 421 ... exp(x2/x0) >= 7.0''' 422 422 >>> ineqf,eqf = generate_conditions(constraints, nvars=4) 423 423 >>> print ineqf[0].__doc__ … … 490 490 For example: 491 491 >>> constraints = ''' 492 ... x 3 = x1/2.493 ... x 1>= 0.'''492 ... x2 = x0/2. 493 ... x0 >= 0.''' 494 494 >>> solv = generate_solvers(constraints, nvars=3) 495 495 >>> print solv[0].__doc__ … … 564 564 For example: 565 565 >>> constraints = ''' 566 ... x 3 = x1/2.567 ... x 1>= 0.'''566 ... x2 = x0/2. 567 ... x0 >= 0.''' 568 568 >>> ineqf,eqf = generate_conditions(constraints, nvars=3) 569 569 >>> penalty = generate_penalty((ineqf,eqf)) … … 627 627 For example: 628 628 >>> constraints = ''' 629 ... x 1 = cos(x2) + 2.630 ... x 2 = x3*2.'''629 ... x0 = cos(x1) + 2. 630 ... x1 = x2*2.''' 631 631 >>> solv = generate_solvers(constraints) 632 632 >>> constraint = generate_constraint(solv) … … 640 640 For example: 641 641 >>> constraints = ''' 642 ... x 3 = x1/2.643 ... x 1>= 0.'''642 ... x2 = x0/2. 643 ... x0 >= 0.''' 644 644 >>> solv = generate_solvers(constraints, nvars=3) 645 645 >>> print solv[0].__doc__ -
mystic/tests/test_symbolic.py
r645 r686 6 6 7 7 constraints = """ 8 x 1**2 = 2.5*x4- a9 exp(x 3/x1) >= b"""8 x0**2 = 2.5*x3 - a 9 exp(x2/x0) >= b""" 10 10 11 11 ineq,eq = generate_conditions(constraints, nvars=4, locals={'a':5.0, 'b':7.0}) … … 24 24 25 25 constraints = """ 26 mean([x 1, x2, x3]) = 5.027 x 1 = x2 + x3"""26 mean([x0, x1, x2]) = 5.0 27 x0 = x1 + x2""" 28 28 29 29 ineq,eq = generate_conditions(constraints) … … 41 41 42 42 constraints = """ 43 mean([x 1, x2, x3]) = 5.044 spread([x 1, x2, x3]) = 10.0"""43 mean([x0, x1, x2]) = 5.0 44 spread([x0, x1, x2]) = 10.0""" 45 45 46 46 from mystic.math.measures import mean, spread … … 55 55 56 56 constraints = """ 57 spread([x 1,x2]) - 1.0 = mean([x1,x2])58 mean([x 1,x2,x3]) = x3"""57 spread([x0,x1]) - 1.0 = mean([x0,x1]) 58 mean([x0,x1,x2]) = x2""" 59 59 60 60 from mystic.math.measures import mean, spread -
mystic/tests/test_symbolic_basic.py
r645 r686 73 73 # Test a nonlinear constraints example. 74 74 string = """ 75 x 2*x3= 1.76 x 3 = x1- 3.75 x1*x2 = 1. 76 x2 = x0 - 3. 77 77 """ 78 78 print "building constraints function for:%s" % string.rstrip() … … 136 136 137 137 def test_feasible_pt(): 138 constraints = """x 1 + x2 + x7 - 2*x4> 3"""138 constraints = """x0 + x1 + x6 - 2*x3 > 3""" 139 139 soln = solve(constraints, guess=[1.]*8) #FIXME: either constraints.solve 140 140 print 'actual solution:', soln, "\n" # or symbolic.solve ... ?
Note: See TracChangeset
for help on using the changeset viewer.