Move invtrig tests from test_functions.py to test_trigonometric.py
[sympy.git] / sympy / core / methods.py
blob9deb93ed8b1bee108f397dd6047fecd6908c40cd
1 """ Defines default methods for unary and binary operations.
2 """
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):
14 def __pos__(self):
15 return self
16 def __neg__(self):
17 return Basic.Integer(-1) * self
18 def __abs__(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__.
51 """
52 return None
54 class NoArithMeths(object):
56 def __pos__(self):
57 raise TypeError, _no_unary_operation('+', self)
58 def __neg__(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):
81 return None
83 class RelMeths(object):
85 def __eq__(self, other):
86 try:
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)
92 return r
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)