Skip to content

Commit

Permalink
symbolic tuple subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
jtauber committed Feb 3, 2014
1 parent 7b18a2c commit 8392346
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
see ``tuples.rst`` for an explanation.
"""

from symbolic import Sym, Expr, Add
from symbolic import Sym, Expr, Add, Sub


class Tuple(Expr):
Expand Down Expand Up @@ -39,11 +39,18 @@ def __add__(self, other):
raise TypeError("addend must be tuple or symbol")

def __sub__(self, other):
if type(self) != type(other) or len(self) != len(other):
raise TypeError("can't subtract incompatible Tuples")
return self.__class__(
*(s - o for (s, o) in zip(self._components, other._components))
)
if isinstance(other, Tuple):
if type(self) != type(other) or len(self) != len(other):
raise TypeError("can't subtract incompatible Tuples")
else:
return self.__class__(
*(s - o for (s, o) in zip(
self._components, other._components))
)
elif isinstance(other, Sym):
return Sub(self, other)
else:
raise TypeError("subtrahend must be tuple or symbol")

def __mul__(self, other):
if not isinstance(other, Tuple):
Expand Down

0 comments on commit 8392346

Please sign in to comment.