Changeset 2899


Ignore:
Timestamp:
Jul 30, 2010 7:58:20 PM (9 years ago)
Author:
prsteel
Message:

Fixes an apparent bug.

Before, arithmetic operations on expressions were defined to operate
in-place, which causes issues when manipulating expressions. For example,

foo = model.X + model.Y
for x in xrange(0,10):

print foo + model.X

would in fact manipulate the expression foo. As this is believed to be
undesirable (and was motivated by a specific use case, namely model
transformations) it has been changed to return a clone of the
expression in question.

One test case failed as a side effect of this change, and a correction
was made to resolve this. This correction was also inside expr.py. The
problem was that the ._nargs attribute of _SumExpression objects was
not copied on .clone() calls. This has been changed. NOTE: it is not
known whether a similar fix must be made to product expressions. No
test cases are failing now, but this must be resolved.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/expr.py

    r2641 r2899  
    436436        tmp.name = self.name
    437437        tmp._args = copy.copy(self._args)
     438        tmp._nargs = copy.copy(self._nargs)
    438439        tmp._coef = copy.copy(self._coef)
    439440        tmp._const = self._const
     
    566567        other = as_numeric(args[1])
    567568        if type(args[0]) is _SumExpression:
    568             args[0].add(expr=other)
    569             return args[0]
     569            tmp = args[0].clone()
     570            tmp.add(expr=other)
     571            return tmp
    570572        else:
    571573            tmp = _SumExpression()
     
    580582        other = as_numeric(args[1])
    581583        if type(args[0]) is _SumExpression:
    582             args[0].add(expr=other)
    583             return args[0]
     584            tmp = args[0].clone()
     585            tmp.add(expr=other)
     586            return tmp
    584587        else:
    585588            tmp = _SumExpression()
     
    594597        other = as_numeric(args[1])
    595598        if type(args[0]) is _SumExpression:
    596             args[0].add(coef=-1, expr=other)
    597             return args[0]
     599            tmp = args[0].clone()
     600            tmp.add(coef=-1, expr=other)
     601            return tmp
    598602        else:
    599603            tmp = _SumExpression()
     
    608612        other = as_numeric(args[1])
    609613        if type(args[0]) is _SumExpression:
    610             args[0].negate()
    611             args[0].add(expr=other)
    612             return args[0]
     614            tmp = args[0].clone()
     615            tmp.negate()
     616            tmp.add(expr=other)
     617            return tmp
    613618        else:
    614619            tmp = _SumExpression()
     
    623628        other = as_numeric(args[1])
    624629        if type(args[0]) is _ProductExpression:
    625             args[0].add(numerator=other)
    626             return args[0]
     630            tmp = args[0].clone()
     631            tmp.add(numerator=other)
     632            return tmp
    627633        else:
    628634            tmp = _ProductExpression()
     
    637643        other = as_numeric(args[1])
    638644        if type(args[0]) is _ProductExpression:
    639             args[0].add(numerator=other)
    640             return args[0]
     645            tmp = args[0].clone()
     646            tmp.add(numerator=other)
     647            return tmp
    641648        else:
    642649            tmp = _ProductExpression()
     
    651658        other = as_numeric(args[1])
    652659        if type(args[0]) is _ProductExpression:
    653             args[0].add(denominator=other)
    654             return args[0]
     660            tmp = args[0].clone()
     661            tmp.add(denominator=other)
     662            return tmp
    655663        else:
    656664            tmp = _ProductExpression()
     
    666674        other = as_numeric(args[1])
    667675        if type(args[0]) is _ProductExpression:
    668             args[0].invert()
    669             args[0].add(numerator=other)
    670             return args[0]
     676            tmp = args[0].clone()
     677            tmp.invert()
     678            tmp.add(numerator=other)
     679            return tmp
    671680        else:
    672681            tmp = _ProductExpression()
Note: See TracChangeset for help on using the changeset viewer.