1 """ Defines default methods for unary and binary operations.
4 from basic
import Basic
, S
, SympifyError
, sympify
5 #from add import Add /cyclic/
6 #from mul import Mul /cyclic/
7 #from pow import Pow /cyclic/
8 #from relational import Equality, Unequality, Inequality, StrictInequality /cyclic/
9 #from sympy.functions.elementary.complexes import abs as abs_ /cyclic/
11 def _no_unary_operation(op
, obj
):
12 return 'unary operation `%s` not defined for %s' % (op
, obj
.__class
__.__name
__)
13 def _no_binary_operation(op
, obj1
, obj2
):
14 return 'binary operation `%s` not defined between %s and %s' \
15 % (op
, obj1
.__class__
.__name
__, obj2
.__class__
.__name
__)
17 class ArithMeths(object):
22 return S
.NegativeOne
* self
25 def __add__(self
, other
):
27 other
= sympify(other
)
30 if not isinstance(other
, Basic
):
32 return Add(self
, other
)
33 def __radd__(self
, other
):
34 return sympify(other
).__add
__(self
)
35 def __sub__(self
, other
):
37 other
= sympify(other
)
40 if not isinstance(other
, Basic
):
42 return self
+ (-other
)
43 def __rsub__(self
, other
):
44 return sympify(other
).__sub
__(self
)
45 def __mul__(self
, other
):
46 # FIXME this is a dirty hack. matrix should be ordinary SymPy object
47 from sympy
.matrices
import Matrix
48 if isinstance(other
, Matrix
): return NotImplemented
50 other
= sympify(other
)
53 if not isinstance(other
, Basic
):
55 return Mul(self
, other
)
56 def __rmul__(self
, other
):
57 return sympify(other
).__mul
__(self
)
58 def __pow__(self
, other
):
60 other
= sympify(other
)
63 if not isinstance(other
, Basic
):
65 return Pow(self
, other
)
66 def __rpow__(self
, other
):
68 other
= sympify(other
)
71 if not isinstance(other
, Basic
):
73 return other
.__pow
__(self
)
74 def __div__(self
, other
):
76 other
= sympify(other
)
79 if not isinstance(other
, Basic
):
81 return self
* (other
** S
.NegativeOne
)
82 def __truediv__(self
, other
):
83 return self
.__div
__(other
)
84 def __rdiv__(self
, other
):
85 return sympify(other
).__div
__(self
)
86 def __rtruediv__(self
, other
):
87 return self
.__rdiv
__(other
)
89 class NoArithMeths(object):
92 raise TypeError, _no_unary_operation('+', self
)
94 raise TypeError, _no_unary_operation('-', self
)
95 def __add__(self
, other
):
96 raise TypeError, _no_binary_operation('+', self
, other
)
97 def __radd__(self
, other
):
98 raise TypeError, _no_binary_operation('+', other
, self
)
99 def __sub__(self
, other
):
100 raise TypeError, _no_binary_operation('-', self
, other
)
101 def __rsub__(self
, other
):
102 raise TypeError, _no_binary_operation('-', other
, self
)
103 def __mul__(self
, other
):
104 raise TypeError, _no_binary_operation('*', self
, other
)
105 def __rmul__(self
, other
):
106 raise TypeError, _no_binary_operation('*', other
, self
)
107 def __div__(self
, other
):
108 raise TypeError, _no_binary_operation('/', self
, other
)
109 def __rdiv__(self
, other
):
110 raise TypeError, _no_binary_operation('/', other
, self
)
111 def __pow__(self
, other
):
112 raise TypeError, _no_binary_operation('**', self
, other
)
113 def __rpow__(self
, other
):
114 raise TypeError, _no_binary_operation('**', other
, self
)
115 def _eval_power(self
, other
):
118 class RelMeths(object):
120 def __eq__(self
, other
):
122 other
= sympify(other
)
125 return Equality(self
, other
)
126 def __ne__(self
, other
):
128 other
= sympify(other
)
131 return Unequality(self
, other
)
132 def __lt__(self
, other
):
133 #return sympify(other) > self
134 return StrictInequality(self
, other
)
135 def __gt__(self
, other
):
136 return StrictInequality(other
, self
)
137 #return sympify(other) < self
138 def __le__(self
, other
):
139 return Inequality(self
, other
)
140 def __ge__(self
, other
):
141 return sympify(other
) <= self
143 class NoRelMeths(object):
145 def __eq__(self
, other
):
146 return Equality(self
, other
)
147 def __ne__(self
, other
):
148 return Unequality(self
, other
)
149 def __lt__(self
, other
):
150 return hash(self
) < hash(other
)
151 raise TypeError, _no_binary_operation('<', self
, other
)
152 def __gt__(self
, other
):
153 return hash(self
) > hash(other
)
154 raise TypeError, _no_binary_operation('>', self
, other
)
155 def __le__(self
, other
):
156 raise TypeError, _no_binary_operation('<=', self
, other
)
157 def __ge__(self
, other
):
158 raise TypeError, _no_binary_operation('>=', self
, other
)