+def _polymorphic_method(func):
+ @functools.wraps(func)
+ def wrapper(a, b):
+ if isinstance(b, Expression):
+ return func(a, b)
+ if isinstance(b, numbers.Rational):
+ b = constant(b)
+ return func(a, b)
+ return NotImplemented
+ return wrapper
+
+def _polymorphic_operator(func):
+ @functools.wraps(func)
+ def wrapper(a, b):
+ if isinstance(a, numbers.Rational):
+ a = constant(a)
+ if isinstance(b, numbers.Rational):
+ b = constant(b)
+ if isinstance(a, Expression) and isinstance(b, Expression):
+ return func(a, b)
+ raise TypeError('arguments must be linear expressions')
+ return wrapper
+
+