sympify: actually raise SympifyError on error (we used to raise ValueError)
[sympy.git] / sympy / core / methods.py
blob47102a7bec445214561d7916e0ba361a9d91c4ee
1 """ Defines default methods for unary and binary operations.
2 """
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):
19 def __pos__(self):
20 return self
21 def __neg__(self):
22 return S.NegativeOne * self
23 def __abs__(self):
24 return abs_(self)
25 def __add__(self, other):
26 try:
27 other = sympify(other)
28 except SympifyError:
29 return NotImplemented
30 if not isinstance(other, Basic):
31 return NotImplemented
32 return Add(self, other)
33 def __radd__(self, other):
34 return sympify(other).__add__(self)
35 def __sub__(self, other):
36 try:
37 other = sympify(other)
38 except SympifyError:
39 return NotImplemented
40 if not isinstance(other, Basic):
41 return NotImplemented
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
49 try:
50 other = sympify(other)
51 except SympifyError:
52 return NotImplemented
53 if not isinstance(other, Basic):
54 return NotImplemented
55 return Mul(self, other)
56 def __rmul__(self, other):
57 return sympify(other).__mul__(self)
58 def __pow__(self, other):
59 try:
60 other = sympify(other)
61 except SympifyError:
62 return NotImplemented
63 if not isinstance(other, Basic):
64 return NotImplemented
65 return Pow(self, other)
66 def __rpow__(self, other):
67 try:
68 other = sympify(other)
69 except SympifyError:
70 return NotImplemented
71 if not isinstance(other, Basic):
72 return NotImplemented
73 return other.__pow__(self)
74 def __div__(self, other):
75 try:
76 other = sympify(other)
77 except SympifyError:
78 return NotImplemented
79 if not isinstance(other, Basic):
80 return NotImplemented
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):
91 def __pos__(self):
92 raise TypeError, _no_unary_operation('+', self)
93 def __neg__(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):
116 return None
118 class RelMeths(object):
120 def __eq__(self, other):
121 try:
122 other = sympify(other)
123 except ValueError:
124 return False
125 return Equality(self, other)
126 def __ne__(self, other):
127 try:
128 other = sympify(other)
129 except ValueError:
130 return True
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)