# Changeset 2640

Ignore:
Timestamp:
Jun 2, 2010 12:56:37 PM (11 years ago)
Message:

Refactored repn_add

Refactored variable names to be more self-explanatory.
Referenced comments in generate_canonical_repn.
Added inline explanations

File:
1 edited

Unmodified
Added
Removed
• ## coopr.pyomo/trunk/coopr/pyomo/expr/canonical_repn.py

 r2214 import copy # # A frozen dictionary that can be hashed.  This dictionary isn't _really_ return rval # # Generate a canonical representation of an expression. # # The canonical representation is a dictionary.  Each element is a mapping # from a term degree to terms in the expression.  If the term degree is # None, then the map value is simply an expression of terms without a # specific degree.  Otherwise, the map value is a dictionary of terms to # their coefficients.  A term is represented as a frozen dictionary that # maps variable id to variable power.  A constant term is represented # with None. # # Examples: #  Let x ... x be the first 4 variables, and #      y ... y be the next 4 variables # # 1.3                           {0:{ None :1.3}} # 3.2*x                      {1:{ {0:1} :3.2}} # 2*x*y + 3*x + 4      {0:{None:4.0}, 1:{{1:1}:3.0}, 2:{{0:1, 5:1}:2.0}} # 2*x**4*y    + 4         {0:{None:4.0}, 1:{{1:1}:3.0}, 5:{{0:4, 5:1 }:2.0}} # log(y) + x*x         {2:{{0:2}:1.0}, None:log(y)} # def generate_canonical_repn(expr): return collect_canonical_repn(expr) return expr def repn_add(rep, r2, coef=1.0): for d in r2: if d is None: if d in rep: rep[d] += r2[d] def repn_add(lhs, rhs, coef=1.0): """ lhs and rhs are the expressions being added together. 'lhs' and 'rhs' are left-hand and right-hand side operands See generate_canonical_repn for explanation of pyomo expressions """ for order in rhs: # For each order term, first-order might be 3*x, # second order 4*x*y or 5*x**2 if order is None: # i.e., (currently) order is a constant or logarithm if order in lhs: lhs[order] += rhs[order] else: rep[d] = r2[d] continue if d < 0: continue if not d in rep: rep[d] = {} for var in r2[d]: rep[d][var] = coef*r2[d][var] + rep[d].get(var,0.0) lhs[order] = rhs[order] continue if order < 0: # ignore now, handled below continue if not order in lhs: lhs[order] = {} for var in rhs[order]: # Add coefficients of variables in this order (e.g., third power) lhs[order][var] = coef*rhs[order][var] + lhs[order].get(var,0.0) # # Merge the VarValue maps # if -1 in r2: if -1 in rep: rep[-1].update(r2[-1]) else: rep[-1] = r2[-1] return rep if -1 in rhs: if -1 in lhs: lhs[-1].update(rhs[-1]) else: lhs[-1] = rhs[-1] return lhs # # Generate a canonical representation of an expression. # # The canonical representation is a dictionary.  Each element is a mapping # from a term degree to terms in the expression.  If the term degree is # None, then the map value is simply an expression of terms without a # specific degree.  Otherwise, the map value is a dictionary of terms to # their coefficients.  A term is represented as a frozen dictionary that # maps variable id to variable power.  A constant term is represented # with None. # # Examples: #  Let x ... x be the first 4 variables, and #      y ... y be the next 4 variables # # 1.3                           {0:{ None :1.3}} # 3.2*x                      {1:{ {0:1} :3.2}} # 2*x*y + 3*x + 4      {0:{None:4.0}, 1:{{1:1}:3.0}, 2:{{0:1, 5:1}:2.0}} # log(y) + x*x         {2:{{0:2}:1.0}, None:log(y)} # def generate_canonical_repn(expr): return collect_canonical_repn(expr) def is_constant(repn):
Note: See TracChangeset for help on using the changeset viewer.