X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/blobdiff_plain/c3d305942999eeb429c6941184b47e7f1b55c499..1154bf4ff8c2d7e7882703917a58d3a42995d78a:/pypol/linexprs.py diff --git a/pypol/linexprs.py b/pypol/linexprs.py index 9449219..10daf9d 100644 --- a/pypol/linexprs.py +++ b/pypol/linexprs.py @@ -9,7 +9,7 @@ from fractions import Fraction, gcd __all__ = [ 'Expression', - 'Symbol', 'symbols', + 'Symbol', 'symbols', 'symbolname', 'symbolnames', 'Constant', ] @@ -59,10 +59,7 @@ class Expression: self = object().__new__(cls) self._coefficients = {} for symbol, coefficient in coefficients: - if isinstance(symbol, Symbol): - symbol = symbol.name - elif not isinstance(symbol, str): - raise TypeError('symbols must be strings or Symbol instances') + symbol = symbolname(symbol) if isinstance(coefficient, Constant): coefficient = coefficient.constant if not isinstance(coefficient, numbers.Rational): @@ -82,10 +79,7 @@ class Expression: return self def coefficient(self, symbol): - if isinstance(symbol, Symbol): - symbol = symbol.name - elif not isinstance(symbol, str): - raise TypeError('symbol must be a string or a Symbol instance') + symbol = symbolname(symbol) try: return self._coefficients[symbol] except KeyError: @@ -255,6 +249,29 @@ class Expression: return left / right raise SyntaxError('invalid syntax') + def subs(self, symbol, expression=None): + if expression is None: + if isinstance(symbol, dict): + symbol = symbol.items() + substitutions = symbol + else: + substitutions = [(symbol, expression)] + result = self + for symbol, expression in substitutions: + symbol = symbolname(symbol) + result = result._subs(symbol, expression) + return result + + def _subs(self, symbol, expression): + coefficients = {name: coefficient + for name, coefficient in self.coefficients() + if name != symbol} + constant = self.constant + coefficient = self.coefficient(symbol) + result = Expression(coefficients, self.constant) + result += coefficient * expression + return result + _RE_NUM_VAR = re.compile(r'(\d+|\))\s*([^\W\d_]\w*|\()') @classmethod @@ -345,11 +362,7 @@ class Symbol(Expression): ) def __new__(cls, name): - if isinstance(name, Symbol): - name = name.name - elif not isinstance(name, str): - raise TypeError('name must be a string or a Symbol instance') - name = name.strip() + name = symbolname(name) self = object().__new__(cls) self._name = name self._hash = hash(self._name) @@ -363,10 +376,7 @@ class Symbol(Expression): return self._hash def coefficient(self, symbol): - if isinstance(symbol, Symbol): - symbol = symbol.name - elif not isinstance(symbol, str): - raise TypeError('symbol must be a string or a Symbol instance') + symbol = symbolname(symbol) if symbol == self.name: return 1 else: @@ -420,6 +430,19 @@ def symbols(names): names = names.replace(',', ' ').split() return (Symbol(name) for name in names) +def symbolname(symbol): + if isinstance(symbol, str): + return symbol.strip() + elif isinstance(symbol, Symbol): + return symbol.name + else: + raise TypeError('symbol must be a string or a Symbol instance') + +def symbolnames(symbols): + if isinstance(symbols, str): + return symbols.replace(',', ' ').split() + return tuple(symbolname(symbol) for symbol in symbols) + class Constant(Expression): @@ -441,10 +464,7 @@ class Constant(Expression): return self._hash def coefficient(self, symbol): - if isinstance(symbol, Symbol): - symbol = symbol.name - elif not isinstance(symbol, str): - raise TypeError('symbol must be a string or a Symbol instance') + symbol = symbolname(symbol) return 0 def coefficients(self):