diff --git a/pyomo/repn/standard_repn.py b/pyomo/repn/standard_repn.py index d615dd5c82a..b543f264e59 100644 --- a/pyomo/repn/standard_repn.py +++ b/pyomo/repn/standard_repn.py @@ -743,12 +743,19 @@ def _collect_pow(exp, multiplier, idMap, compute_values, verbose, quadratic): or res.constant != 0) if has_constant: ans.constant = multiplier*res.constant*res.constant - while len(res.linear) > 0: - key1, coef1 = res.linear.popitem() + + # this is reversed since we want to pop off the end for efficiency + # and the quadratic terms have a convention that the indexing tuple + # of key1, key2 is such that key1 <= key2 + keys = sorted(res.linear.keys(), reverse=True) + while len(keys) > 0: + key1 = keys.pop() + coef1 = res.linear[key1] if has_constant: ans.linear[key1] = 2*multiplier*coef1*res.constant ans.quadratic[key1,key1] = multiplier*coef1*coef1 - for key2, coef2 in res.linear.items(): + for key2 in keys: + coef2 = res.linear[key2] ans.quadratic[key1,key2] = 2*multiplier*coef1*coef2 return ans