|
17 | 17 |
|
18 | 18 | TO_STRING_VERBOSE = False
|
19 | 19 |
|
20 |
| -_eq = 0 |
21 |
| -_le = 1 |
22 |
| -_lt = 2 |
23 |
| - |
24 | 20 | # logical propositions
|
25 | 21 | _and = 0
|
26 | 22 | _or = 1
|
@@ -83,6 +79,120 @@ class ExpressionType(enums.Enum):
|
83 | 79 | LOGICAL = 2
|
84 | 80 |
|
85 | 81 |
|
| 82 | +class NUMERIC_ARG_TYPE(enums.IntEnum): |
| 83 | + MUTABLE = -2 |
| 84 | + ASNUMERIC = -1 |
| 85 | + INVALID = 0 |
| 86 | + NATIVE = 1 |
| 87 | + NPV = 2 |
| 88 | + PARAM = 3 |
| 89 | + VAR = 4 |
| 90 | + MONOMIAL = 5 |
| 91 | + LINEAR = 6 |
| 92 | + SUM = 7 |
| 93 | + OTHER = 8 |
| 94 | + |
| 95 | + |
| 96 | +class RELATIONAL_ARG_TYPE(enums.IntEnum, metaclass=enums.ExtendedEnumType): |
| 97 | + __base_enum__ = NUMERIC_ARG_TYPE |
| 98 | + |
| 99 | + INEQUALITY = 100 |
| 100 | + INVALID_RELATIONAL = 101 |
| 101 | + |
| 102 | + |
| 103 | +def _invalid(*args): |
| 104 | + return NotImplemented |
| 105 | + |
| 106 | + |
| 107 | +def _recast_mutable(expr): |
| 108 | + expr.make_immutable() |
| 109 | + if expr._nargs > 1: |
| 110 | + return expr |
| 111 | + elif not expr._nargs: |
| 112 | + return 0 |
| 113 | + else: |
| 114 | + return expr._args_[0] |
| 115 | + |
| 116 | + |
| 117 | +def _unary_op_dispatcher_type_mapping(dispatcher, updates, TYPES=NUMERIC_ARG_TYPE): |
| 118 | + # |
| 119 | + # Special case (wrapping) operators |
| 120 | + # |
| 121 | + def _asnumeric(a): |
| 122 | + a = a.as_numeric() |
| 123 | + return dispatcher[a.__class__](a) |
| 124 | + |
| 125 | + def _mutable(a): |
| 126 | + a = _recast_mutable(a) |
| 127 | + return dispatcher[a.__class__](a) |
| 128 | + |
| 129 | + mapping = { |
| 130 | + TYPES.ASNUMERIC: _asnumeric, |
| 131 | + TYPES.MUTABLE: _mutable, |
| 132 | + TYPES.INVALID: _invalid, |
| 133 | + } |
| 134 | + |
| 135 | + mapping.update(updates) |
| 136 | + return mapping |
| 137 | + |
| 138 | + |
| 139 | +def _binary_op_dispatcher_type_mapping(dispatcher, updates, TYPES=NUMERIC_ARG_TYPE): |
| 140 | + # |
| 141 | + # Special case (wrapping) operators |
| 142 | + # |
| 143 | + def _any_asnumeric(a, b): |
| 144 | + b = b.as_numeric() |
| 145 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 146 | + |
| 147 | + def _asnumeric_any(a, b): |
| 148 | + a = a.as_numeric() |
| 149 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 150 | + |
| 151 | + def _asnumeric_asnumeric(a, b): |
| 152 | + a = a.as_numeric() |
| 153 | + b = b.as_numeric() |
| 154 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 155 | + |
| 156 | + def _any_mutable(a, b): |
| 157 | + b = _recast_mutable(b) |
| 158 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 159 | + |
| 160 | + def _mutable_any(a, b): |
| 161 | + a = _recast_mutable(a) |
| 162 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 163 | + |
| 164 | + def _mutable_mutable(a, b): |
| 165 | + if a is b: |
| 166 | + # Note: _recast_mutable is an in-place operation: make sure |
| 167 | + # that we don't call it twice on the same object. |
| 168 | + a = b = _recast_mutable(a) |
| 169 | + else: |
| 170 | + a = _recast_mutable(a) |
| 171 | + b = _recast_mutable(b) |
| 172 | + return dispatcher[a.__class__, b.__class__](a, b) |
| 173 | + |
| 174 | + mapping = {} |
| 175 | + |
| 176 | + # Because ASNUMERIC and MUTABLE re-call the dispatcher, we want to |
| 177 | + # resolve ASNUMERIC first, MUTABLE second, and INVALID last. That |
| 178 | + # means we will add them to the dispatcher dict in opposite order so |
| 179 | + # "higher priority" callbacks override lower priority ones. |
| 180 | + |
| 181 | + mapping.update({(i, TYPES.INVALID): _invalid for i in TYPES}) |
| 182 | + mapping.update({(TYPES.INVALID, i): _invalid for i in TYPES}) |
| 183 | + |
| 184 | + mapping.update({(i, TYPES.MUTABLE): _any_mutable for i in TYPES}) |
| 185 | + mapping.update({(TYPES.MUTABLE, i): _mutable_any for i in TYPES}) |
| 186 | + mapping[TYPES.MUTABLE, TYPES.MUTABLE] = _mutable_mutable |
| 187 | + |
| 188 | + mapping.update({(i, TYPES.ASNUMERIC): _any_asnumeric for i in TYPES}) |
| 189 | + mapping.update({(TYPES.ASNUMERIC, i): _asnumeric_any for i in TYPES}) |
| 190 | + mapping[TYPES.ASNUMERIC, TYPES.ASNUMERIC] = _asnumeric_asnumeric |
| 191 | + |
| 192 | + mapping.update(updates) |
| 193 | + return mapping |
| 194 | + |
| 195 | + |
86 | 196 | @deprecated(
|
87 | 197 | """The clone counter has been removed and will always return 0.
|
88 | 198 |
|
|
0 commit comments