projects
/
linpy.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
fd99094
)
Use OrderedDict to store Expression coefficients
author
Vivien Maisonneuve
<v.maisonneuve@gmail.com>
Mon, 30 Jun 2014 09:55:28 +0000
(11:55 +0200)
committer
Vivien Maisonneuve
<v.maisonneuve@gmail.com>
Mon, 30 Jun 2014 09:55:28 +0000
(11:55 +0200)
pypol/linexprs.py
patch
|
blob
|
history
diff --git
a/pypol/linexprs.py
b/pypol/linexprs.py
index
9ab5c86
..
d8b020d
100644
(file)
--- a/
pypol/linexprs.py
+++ b/
pypol/linexprs.py
@@
-3,6
+3,7
@@
import functools
import numbers
import re
import numbers
import re
+from collections import OrderedDict
from fractions import Fraction, gcd
from fractions import Fraction, gcd
@@
-67,13
+68,14
@@
class Expression:
raise TypeError('coefficients must be rational numbers '
'or Constant instances')
self._coefficients[symbol] = coefficient
raise TypeError('coefficients must be rational numbers '
'or Constant instances')
self._coefficients[symbol] = coefficient
+ self._coefficients = OrderedDict(sorted(self._coefficients.items()))
if isinstance(constant, Constant):
constant = constant.constant
if not isinstance(constant, numbers.Rational):
raise TypeError('constant must be a rational number '
'or a Constant instance')
self._constant = constant
if isinstance(constant, Constant):
constant = constant.constant
if not isinstance(constant, numbers.Rational):
raise TypeError('constant must be a rational number '
'or a Constant instance')
self._constant = constant
- self._symbols = tuple(s
orted(self._coefficients)
)
+ self._symbols = tuple(s
elf._coefficients
)
self._dimension = len(self._symbols)
return self
self._dimension = len(self._symbols)
return self
@@
-90,8
+92,7
@@
class Expression:
__getitem__ = coefficient
def coefficients(self):
__getitem__ = coefficient
def coefficients(self):
- for symbol in self.symbols:
- yield symbol, self.coefficient(symbol)
+ yield from self._coefficients.items()
@property
def constant(self):
@property
def constant(self):
@@
-220,7
+221,7
@@
class Expression:
return Gt(self, other)
def __hash__(self):
return Gt(self, other)
def __hash__(self):
- return hash((tuple(s
orted(self._coefficients.items()
)), self._constant))
+ return hash((tuple(s
elf.coefficients(
)), self._constant))
def _toint(self):
lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),
def _toint(self):
lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),