|
26 | 26 |
|
27 | 27 |
|
28 | 28 | class _tensor_py_operators: |
| 29 | + # These can't work because Python requires native output types |
| 30 | + def __bool__(self): |
| 31 | + raise TypeError( |
| 32 | + "TensorVariable cannot be converted to Python boolean. " |
| 33 | + "Call `.astype(bool)` for the symbolic equivalent." |
| 34 | + ) |
| 35 | + |
| 36 | + def __index__(self): |
| 37 | + raise TypeError( |
| 38 | + "TensorVariable cannot be converted to Python integer. " |
| 39 | + "Call `.astype(int)` for the symbolic equivalent." |
| 40 | + ) |
| 41 | + |
| 42 | + def __int__(self): |
| 43 | + raise TypeError( |
| 44 | + "TensorVariable cannot be converted to Python integer. " |
| 45 | + "Call `.astype(int)` for the symbolic equivalent." |
| 46 | + ) |
| 47 | + |
| 48 | + def __float__(self): |
| 49 | + raise TypeError( |
| 50 | + "TensorVariables cannot be converted to Python float. " |
| 51 | + "Call `.astype(float)` for the symbolic equivalent." |
| 52 | + ) |
| 53 | + |
| 54 | + def __complex__(self): |
| 55 | + raise TypeError( |
| 56 | + "TensorVariables cannot be converted to Python complex number. " |
| 57 | + "Call `.astype(complex)` for the symbolic equivalent." |
| 58 | + ) |
| 59 | + |
29 | 60 | def __abs__(self): |
30 | 61 | return pt.math.abs(self) |
31 | 62 |
|
32 | 63 | def __neg__(self): |
33 | 64 | return pt.math.neg(self) |
34 | 65 |
|
35 | | - # These won't work because Python requires an int return value |
36 | | - # def __int__(self): return convert_to_int32(self) |
37 | | - # def __float__(self): return convert_to_float64(self) |
38 | | - # def __complex__(self): return convert_to_complex128(self) |
39 | | - |
40 | | - _is_nonzero = True |
41 | | - |
42 | 66 | def __lt__(self, other): |
43 | | - rval = pt.math.lt(self, other) |
44 | | - rval._is_nonzero = False |
45 | | - return rval |
| 67 | + return pt.math.lt(self, other) |
46 | 68 |
|
47 | 69 | def __le__(self, other): |
48 | | - rval = pt.math.le(self, other) |
49 | | - rval._is_nonzero = False |
50 | | - return rval |
| 70 | + return pt.math.le(self, other) |
51 | 71 |
|
52 | 72 | def __gt__(self, other): |
53 | | - rval = pt.math.gt(self, other) |
54 | | - rval._is_nonzero = False |
55 | | - return rval |
| 73 | + return pt.math.gt(self, other) |
56 | 74 |
|
57 | 75 | def __ge__(self, other): |
58 | | - rval = pt.math.ge(self, other) |
59 | | - rval._is_nonzero = False |
60 | | - return rval |
61 | | - |
62 | | - def __bool__(self): |
63 | | - # This is meant to prohibit stuff like a < b < c, which is internally |
64 | | - # implemented as (a < b) and (b < c). The trouble with this is the |
65 | | - # side-effect that checking for a non-NULL a by typing "if a: ..." |
66 | | - # uses the same __nonzero__ method. We want these both to work, but |
67 | | - # it seems impossible. Currently, all vars evaluate to nonzero except |
68 | | - # the return values of comparison operators, which raise this |
69 | | - # exception. If you can think of a better solution, go for it! |
70 | | - # |
71 | | - # __bool__ is Python 3.x data model. __nonzero__ is Python 2.x. |
72 | | - if self._is_nonzero: |
73 | | - return True |
74 | | - else: |
75 | | - raise TypeError("Variables do not support boolean operations.") |
| 76 | + return pt.math.ge(self, other) |
76 | 77 |
|
77 | 78 | def __invert__(self): |
78 | 79 | return pt.math.invert(self) |
|
0 commit comments