1 """ Defines default methods for unary and binary operations.
4 from basic
import Basic
, cache_it
6 def _no_unary_operation(op
, obj
):
7 return 'unary operation `%s` not defined for %s' % (op
, obj
.__class
__.__name
__)
8 def _no_binary_operation(op
, obj1
, obj2
):
9 return 'binary operation `%s` not defined between %s and %s' \
10 % (op
, obj1
.__class__
.__name
__, obj2
.__class__
.__name
__)
12 class ArithMeths(object):
17 return Basic
.Integer(-1) * self
19 return Basic
.abs(self
)
20 def __add__(self
, other
):
21 return Basic
.Add(self
, other
)
22 def __radd__(self
, other
):
23 return Basic
.sympify(other
).__add
__(self
)
24 def __sub__(self
, other
):
25 return self
+ (-Basic
.sympify(other
))
26 def __rsub__(self
, other
):
27 return Basic
.sympify(other
).__sub
__(self
)
28 def __mul__(self
, other
):
29 # FIXME this is a dirty hack. matrix should be ordinary SymPy object
30 from sympy
.matrices
import Matrix
31 if isinstance(other
, Matrix
): return NotImplemented
32 return Basic
.Mul(self
, other
)
33 def __rmul__(self
, other
):
34 return Basic
.sympify(other
).__mul
__(self
)
35 def __pow__(self
, other
):
36 return Basic
.Pow(self
, other
)
37 def __rpow__(self
, other
):
38 return Basic
.sympify(other
).__pow
__(self
)
39 def __div__(self
, other
):
40 return self
* (Basic
.sympify(other
) ** Basic
.Integer(-1))
41 def __truediv__(self
, other
):
42 return self
.__div
__(other
)
43 def __rdiv__(self
, other
):
44 return Basic
.sympify(other
).__div
__(self
)
45 def __rtruediv__(self
, other
):
46 return self
.__rdiv
__(other
)
47 def _eval_power(self
, other
):
48 """ Evaluate Pow(self, other), return new object or None if
49 no evaluation can be carried out. This method can be called
50 only from Pow.__new__.
54 class NoArithMeths(object):
57 raise TypeError, _no_unary_operation('+', self
)
59 raise TypeError, _no_unary_operation('-', self
)
60 def __add__(self
, other
):
61 raise TypeError, _no_binary_operation('+', self
, other
)
62 def __radd__(self
, other
):
63 raise TypeError, _no_binary_operation('+', other
, self
)
64 def __sub__(self
, other
):
65 raise TypeError, _no_binary_operation('-', self
, other
)
66 def __rsub__(self
, other
):
67 raise TypeError, _no_binary_operation('-', other
, self
)
68 def __mul__(self
, other
):
69 raise TypeError, _no_binary_operation('*', self
, other
)
70 def __rmul__(self
, other
):
71 raise TypeError, _no_binary_operation('*', other
, self
)
72 def __div__(self
, other
):
73 raise TypeError, _no_binary_operation('/', self
, other
)
74 def __rdiv__(self
, other
):
75 raise TypeError, _no_binary_operation('/', other
, self
)
76 def __pow__(self
, other
):
77 raise TypeError, _no_binary_operation('**', self
, other
)
78 def __rpow__(self
, other
):
79 raise TypeError, _no_binary_operation('**', other
, self
)
80 def _eval_power(self
, other
):
83 class RelMeths(object):
85 def __eq__(self
, other
):
87 r
= Basic
.Equality(self
, other
)
88 except ValueError, msg
:
89 # temporary workaround:
90 print 'Failed to create Equality instance: %s, using repr equality test instead' % msg
91 r
= repr(self
)==repr(other
)
93 def __ne__(self
, other
):
94 return Basic
.Unequality(self
, other
)
95 def __lt__(self
, other
):
96 #return Basic.sympify(other) > self
97 return Basic
.StrictInequality(self
, other
)
98 def __gt__(self
, other
):
99 return Basic
.StrictInequality(other
, self
)
100 #return Basic.sympify(other) < self
101 def __le__(self
, other
):
102 return Basic
.Inequality(self
, other
)
103 def __ge__(self
, other
):
104 return Basic
.sympify(other
) <= self
106 class NoRelMeths(object):
108 def __eq__(self
, other
):
109 return Basic
.Equality(self
, other
)
110 def __ne__(self
, other
):
111 return Basic
.Unequality(self
, other
)
112 def __lt__(self
, other
):
113 return hash(self
) < hash(other
)
114 raise TypeError, _no_binary_operation('<', self
, other
)
115 def __gt__(self
, other
):
116 return hash(self
) > hash(other
)
117 raise TypeError, _no_binary_operation('>', self
, other
)
118 def __le__(self
, other
):
119 raise TypeError, _no_binary_operation('<=', self
, other
)
120 def __ge__(self
, other
):
121 raise TypeError, _no_binary_operation('>=', self
, other
)