6 from fractions
import Fraction
, gcd
16 def _polymorphic(func
):
17 @functools.wraps(func
)
18 def wrapper(left
, right
):
19 if isinstance(right
, Expression
):
20 return func(left
, right
)
21 elif isinstance(right
, numbers
.Rational
):
22 right
= Constant(right
)
23 return func(left
, right
)
30 This class implements linear expressions.
40 def __new__(cls
, coefficients
=None, constant
=0):
41 if isinstance(coefficients
, str):
43 raise TypeError('too many arguments')
44 return cls
.fromstring(coefficients
)
45 if isinstance(coefficients
, dict):
46 coefficients
= coefficients
.items()
47 if coefficients
is None:
48 return Constant(constant
)
49 coefficients
= [(symbol
, coefficient
)
50 for symbol
, coefficient
in coefficients
if coefficient
!= 0]
51 if len(coefficients
) == 0:
52 return Constant(constant
)
53 elif len(coefficients
) == 1 and constant
== 0:
54 symbol
, coefficient
= coefficients
[0]
57 self
= object().__new
__(cls
)
58 self
._coefficients
= {}
59 for symbol
, coefficient
in coefficients
:
60 if isinstance(symbol
, Symbol
):
62 elif not isinstance(symbol
, str):
63 raise TypeError('symbols must be strings or Symbol instances')
64 if isinstance(coefficient
, Constant
):
65 coefficient
= coefficient
.constant
66 if not isinstance(coefficient
, numbers
.Rational
):
67 raise TypeError('coefficients must be rational numbers '
68 'or Constant instances')
69 self
._coefficients
[symbol
] = coefficient
70 if isinstance(constant
, Constant
):
71 constant
= constant
.constant
72 if not isinstance(constant
, numbers
.Rational
):
73 raise TypeError('constant must be a rational number '
74 'or a Constant instance')
75 self
._constant
= constant
76 self
._symbols
= tuple(sorted(self
._coefficients
))
77 self
._dimension
= len(self
._symbols
)
80 def coefficient(self
, symbol
):
81 if isinstance(symbol
, Symbol
):
83 elif not isinstance(symbol
, str):
84 raise TypeError('symbol must be a string or a Symbol instance')
86 return self
._coefficients
[symbol
]
90 __getitem__
= coefficient
92 def coefficients(self
):
93 for symbol
in self
.symbols
:
94 yield symbol
, self
.coefficient(symbol
)
106 return self
._dimension
108 def isconstant(self
):
115 for symbol
in self
.symbols
:
116 yield self
.coefficient(symbol
)
129 def __add__(self
, other
):
130 coefficients
= dict(self
.coefficients())
131 for symbol
, coefficient
in other
.coefficients():
132 if symbol
in coefficients
:
133 coefficients
[symbol
] += coefficient
135 coefficients
[symbol
] = coefficient
136 constant
= self
.constant
+ other
.constant
137 return Expression(coefficients
, constant
)
142 def __sub__(self
, other
):
143 coefficients
= dict(self
.coefficients())
144 for symbol
, coefficient
in other
.coefficients():
145 if symbol
in coefficients
:
146 coefficients
[symbol
] -= coefficient
148 coefficients
[symbol
] = -coefficient
149 constant
= self
.constant
- other
.constant
150 return Expression(coefficients
, constant
)
152 def __rsub__(self
, other
):
153 return -(self
- other
)
156 def __mul__(self
, other
):
157 if other
.isconstant():
158 coefficients
= dict(self
.coefficients())
159 for symbol
in coefficients
:
160 coefficients
[symbol
] *= other
.constant
161 constant
= self
.constant
* other
.constant
162 return Expression(coefficients
, constant
)
163 if isinstance(other
, Expression
) and not self
.isconstant():
164 raise ValueError('non-linear expression: '
165 '{} * {}'.format(self
._parenstr
(), other
._parenstr
()))
166 return NotImplemented
171 def __truediv__(self
, other
):
172 if other
.isconstant():
173 coefficients
= dict(self
.coefficients())
174 for symbol
in coefficients
:
175 coefficients
[symbol
] = \
176 Fraction(coefficients
[symbol
], other
.constant
)
177 constant
= Fraction(self
.constant
, other
.constant
)
178 return Expression(coefficients
, constant
)
179 if isinstance(other
, Expression
):
180 raise ValueError('non-linear expression: '
181 '{} / {}'.format(self
._parenstr
(), other
._parenstr
()))
182 return NotImplemented
184 def __rtruediv__(self
, other
):
185 if isinstance(other
, self
):
186 if self
.isconstant():
187 constant
= Fraction(other
, self
.constant
)
188 return Expression(constant
=constant
)
190 raise ValueError('non-linear expression: '
191 '{} / {}'.format(other
._parenstr
(), self
._parenstr
()))
192 return NotImplemented
195 def __eq__(self
, other
):
197 # see http://docs.sympy.org/dev/tutorial/gotchas.html#equals-signs
198 return isinstance(other
, Expression
) and \
199 self
._coefficients
== other
._coefficients
and \
200 self
.constant
== other
.constant
203 def __le__(self
, other
):
204 from .polyhedra
import Le
205 return Le(self
, other
)
208 def __lt__(self
, other
):
209 from .polyhedra
import Lt
210 return Lt(self
, other
)
213 def __ge__(self
, other
):
214 from .polyhedra
import Ge
215 return Ge(self
, other
)
218 def __gt__(self
, other
):
219 from .polyhedra
import Gt
220 return Gt(self
, other
)
223 return hash((tuple(sorted(self
._coefficients
.items())), self
._constant
))
226 lcm
= functools
.reduce(lambda a
, b
: a
*b
// gcd(a
, b
),
227 [value
.denominator
for value
in self
.values()])
231 def _fromast(cls
, node
):
232 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
233 return cls
._fromast
(node
.body
[0])
234 elif isinstance(node
, ast
.Expr
):
235 return cls
._fromast
(node
.value
)
236 elif isinstance(node
, ast
.Name
):
237 return Symbol(node
.id)
238 elif isinstance(node
, ast
.Num
):
239 return Constant(node
.n
)
240 elif isinstance(node
, ast
.UnaryOp
) and isinstance(node
.op
, ast
.USub
):
241 return -cls
._fromast
(node
.operand
)
242 elif isinstance(node
, ast
.BinOp
):
243 left
= cls
._fromast
(node
.left
)
244 right
= cls
._fromast
(node
.right
)
245 if isinstance(node
.op
, ast
.Add
):
247 elif isinstance(node
.op
, ast
.Sub
):
249 elif isinstance(node
.op
, ast
.Mult
):
251 elif isinstance(node
.op
, ast
.Div
):
253 raise SyntaxError('invalid syntax')
255 _RE_NUM_VAR
= re
.compile(r
'(\d+|\))\s*([^\W\d_]\w*|\()')
258 def fromstring(cls
, string
):
259 # add implicit multiplication operators, e.g. '5x' -> '5*x'
260 string
= cls
._RE
_NUM
_VAR
.sub(r
'\1*\2', string
)
261 tree
= ast
.parse(string
, 'eval')
262 return cls
._fromast
(tree
)
267 for symbol
in self
.symbols
:
268 coefficient
= self
.coefficient(symbol
)
273 string
+= ' + {}'.format(symbol
)
274 elif coefficient
== -1:
276 string
+= '-{}'.format(symbol
)
278 string
+= ' - {}'.format(symbol
)
281 string
+= '{}*{}'.format(coefficient
, symbol
)
282 elif coefficient
> 0:
283 string
+= ' + {}*{}'.format(coefficient
, symbol
)
285 assert coefficient
< 0
287 string
+= ' - {}*{}'.format(coefficient
, symbol
)
289 constant
= self
.constant
290 if constant
!= 0 and i
== 0:
291 string
+= '{}'.format(constant
)
293 string
+= ' + {}'.format(constant
)
296 string
+= ' - {}'.format(constant
)
301 def _parenstr(self
, always
=False):
303 if not always
and (self
.isconstant() or self
.issymbol()):
306 return '({})'.format(string
)
309 return '{}({!r})'.format(self
.__class
__.__name
__, str(self
))
312 def fromsympy(cls
, expr
):
316 for symbol
, coefficient
in expr
.as_coefficients_dict().items():
317 coefficient
= Fraction(coefficient
.p
, coefficient
.q
)
318 if symbol
== sympy
.S
.One
:
319 constant
= coefficient
320 elif isinstance(symbol
, sympy
.Symbol
):
322 coefficients
[symbol
] = coefficient
324 raise ValueError('non-linear expression: {!r}'.format(expr
))
325 return cls(coefficients
, constant
)
330 for symbol
, coefficient
in self
.coefficients():
331 term
= coefficient
* sympy
.Symbol(symbol
)
333 expr
+= self
.constant
337 class Symbol(Expression
):
339 __slots__
= Expression
.__slots
__ + (
343 def __new__(cls
, name
):
344 if isinstance(name
, Symbol
):
346 elif not isinstance(name
, str):
347 raise TypeError('name must be a string or a Symbol instance')
349 self
= object().__new
__(cls
)
350 self
._coefficients
= {name
: 1}
352 self
._symbols
= tuple(name
)
365 def _fromast(cls
, node
):
366 if isinstance(node
, ast
.Module
) and len(node
.body
) == 1:
367 return cls
._fromast
(node
.body
[0])
368 elif isinstance(node
, ast
.Expr
):
369 return cls
._fromast
(node
.value
)
370 elif isinstance(node
, ast
.Name
):
371 return Symbol(node
.id)
372 raise SyntaxError('invalid syntax')
375 return '{}({!r})'.format(self
.__class
__.__name
__, self
._name
)
378 def fromsympy(cls
, expr
):
380 if isinstance(expr
, sympy
.Symbol
):
381 return cls(expr
.name
)
383 raise TypeError('expr must be a sympy.Symbol instance')
387 if isinstance(names
, str):
388 names
= names
.replace(',', ' ').split()
389 return (Symbol(name
) for name
in names
)
392 class Constant(Expression
):
394 def __new__(cls
, numerator
=0, denominator
=None):
395 self
= object().__new
__(cls
)
396 if denominator
is None and isinstance(numerator
, Constant
):
397 self
._constant
= numerator
.constant
399 self
._constant
= Fraction(numerator
, denominator
)
400 self
._coefficients
= {}
405 def isconstant(self
):
409 return self
.constant
!= 0
412 def fromstring(cls
, string
):
413 if isinstance(string
, str):
414 return Constant(Fraction(string
))
416 raise TypeError('string must be a string instance')
419 if self
.constant
.denominator
== 1:
420 return '{}({!r})'.format(self
.__class
__.__name
__,
421 self
.constant
.numerator
)
423 return '{}({!r}, {!r})'.format(self
.__class
__.__name
__,
424 self
.constant
.numerator
, self
.constant
.denominator
)
427 def fromsympy(cls
, expr
):
429 if isinstance(expr
, sympy
.Rational
):
430 return cls(expr
.p
, expr
.q
)
431 elif isinstance(expr
, numbers
.Rational
):
434 raise TypeError('expr must be a sympy.Rational instance')