From: Vivien Maisonneuve Date: Wed, 18 Jun 2014 09:46:17 +0000 (+0200) Subject: Use property decorators for symbols method X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/commitdiff_plain/17280dae6521320cf320a04d57372db5cbed5dba?hp=bb2fe6868c69f2d478da3ecbf4efeeef21c6eae2 Use property decorators for symbols method --- diff --git a/polyp.py b/polyp.py index ca32493..d48fd80 100644 --- a/polyp.py +++ b/polyp.py @@ -135,12 +135,13 @@ class Expression: context[symbol] = Symbol(symbol) return eval(string, context) + @property def symbols(self): - yield from sorted(self._coefficients) + return tuple(sorted(self._coefficients)) @property def dimension(self): - return len(list(self.symbols())) + return len(list(self.symbols)) def coefficient(self, symbol): symbol = _strsymbol(symbol) @@ -152,7 +153,7 @@ class Expression: __getitem__ = coefficient def coefficients(self): - for symbol in self.symbols(): + for symbol in self.symbols: yield symbol, self.coefficient(symbol) @property @@ -163,14 +164,14 @@ class Expression: return len(self._coefficients) == 0 def values(self): - for symbol in self.symbols(): + for symbol in self.symbols: yield self.coefficient(symbol) yield self.constant def symbol(self): if not self.issymbol(): raise ValueError('not a symbol: {}'.format(self)) - for symbol in self.symbols(): + for symbol in self.symbols: return symbol def issymbol(self): @@ -229,7 +230,7 @@ class Expression: def __repr__(self): string = '' - symbols = sorted(self.symbols()) + symbols = self.symbols i = 0 for symbol in symbols: coefficient = self[symbol] @@ -352,10 +353,10 @@ class Polyhedron: inequalities = [] symbols = set() for equality in equalities: - symbols.update(equality.symbols()) + symbols.update(equality.symbols) for inequality in inequalities: - symbols.update(inequality.symbols()) - symbols = sorted(symbols) + symbols.update(inequality.symbols) + symbols = list(symbols) string = _iscc.set(symbols, equalities, inequalities) string = _iscc.eval(string) return cls._fromiscc(symbols, string) @@ -368,7 +369,7 @@ class Polyhedron: if re.match(r'^\s*\{\s*\}\s*$', string): return empty self = super().__new__(cls) - self._symbols = _strsymbols(symbols) + self._symbols = sorted(_strsymbols(symbols)) self._equalities = [] self._inequalities = [] string = re.sub(r'^\s*\{\s*(.*?)\s*\}\s*$', lambda m: m.group(1), string) @@ -414,21 +415,22 @@ class Polyhedron: yield from self.equalities yield from self.inequalities + @property def symbols(self): - yield from self._symbols + return tuple(self._symbols) @property def dimension(self): - return len(self.symbols()) + return len(self.symbols) def __bool__(self): # return false if the polyhedron is empty, true otherwise raise not self.isempty() def _symbolunion(self, *others): - symbols = set(self.symbols()) + symbols = set(self.symbols) for other in others: - symbols.update(other.symbols()) + symbols.update(other.symbols) return sorted(symbols) def __eq__(self, other): @@ -494,7 +496,6 @@ class Polyhedron: def intersection(self, *others): symbols = self._symbolunion(*others) - symbols = sorted(symbols) strings = [self._toiscc(symbols)] for other in others: strings.append(other._toiscc(symbols)) @@ -509,7 +510,6 @@ class Polyhedron: # return a new polyhedron with elements in the polyhedron that are not # in the others symbols = self._symbolunion(*others) - symbols = sorted(symbols) strings = [self._toiscc(symbols)] for other in others: strings.append(other._toiscc(symbols)) @@ -522,7 +522,7 @@ class Polyhedron: def projection(self, symbols): symbols = _strsymbols(symbols) - string = _iscc.map(symbols, self.symbols(), + string = _iscc.map(symbols, self.symbols, self.equalities, self.inequalities) string = _iscc.eval('poly (dom ({}))'.format(string)) return Polyhedron._fromiscc(symbols, string)