Skip to content

Commit dc7ce38

Browse files
authored
Implement __itruediv__ for Scaled functions (#11)
Implement `__itruediv__` for objective functions: `Objective` and `Combo` raise a `TypeError`, `Scaled` modifies the multiplier in-place and returns a copy of itself. Replace the old `__div__` for the new `__truediv__` and `__floordiv__`. Make `__floordiv__` to raise a `TypeError` for every class.
1 parent 1d8e39e commit dc7ce38

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/inversion_ideas/base/objective_function.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,25 @@ def __mul__(self, value) -> "Scaled":
9090
def __rmul__(self, value):
9191
return self.__mul__(value)
9292

93-
def __div__(self, denominator):
94-
return self * (1.0 / denominator)
95-
9693
def __truediv__(self, denominator):
9794
return self * (1.0 / denominator)
9895

99-
def __iadd__(self, other) -> "Combo": # noqa: PYI034
96+
def __floordiv__(self, denominator):
97+
msg = "Floor division is not implemented for objective functions."
98+
raise TypeError(msg)
99+
100+
def __iadd__(self, other) -> Self:
100101
msg = "Inplace addition is not implemented for this class."
101102
raise TypeError(msg)
102103

103-
def __imul__(self, other) -> "Scaled": # noqa: PYI034
104+
def __imul__(self, other) -> Self:
104105
msg = "Inplace multiplication is not implemented for this class."
105106
raise TypeError(msg)
106107

108+
def __itruediv__(self, value) -> Self:
109+
msg = "Inplace division is not implemented for this class."
110+
raise TypeError(msg)
111+
107112

108113
class Scaled(Objective):
109114
"""
@@ -171,6 +176,10 @@ def __imul__(self, value) -> Self:
171176
self.multiplier *= value
172177
return self
173178

179+
def __itruediv__(self, value) -> Self:
180+
self.multiplier /= value
181+
return self
182+
174183

175184
class Combo(Objective):
176185
"""

tests/test_objective_functions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class TestObjectiveOperations:
141141
- Error on imul for Combo.
142142
- Error on iadd for Scaled.
143143
- imul works ok for Scaled.
144+
- idiv works ok for Scaled.
144145
- iadd works ok for Combo.
145146
"""
146147

@@ -260,6 +261,27 @@ def test_imul_error(self, function_type):
260261
with pytest.raises(TypeError):
261262
phi *= 2.71
262263

264+
def test_idiv_scaled(self):
265+
a = Dummy(self.n_params)
266+
scalar = 3.14
267+
scaled = scalar * a
268+
scaled_bkp = scaled
269+
new_scalar = 4.0
270+
scaled /= new_scalar
271+
assert isinstance(scaled, Scaled)
272+
assert scaled is scaled_bkp # assert inplace operation
273+
assert scaled.function is a
274+
assert scaled.multiplier == scalar / new_scalar
275+
276+
@pytest.mark.parametrize("function_type", ["objective", "combo"])
277+
def test_idiv_error(self, function_type):
278+
phi = Dummy(self.n_params)
279+
if function_type == "combo":
280+
other = Dummy(self.n_params)
281+
phi = phi + other
282+
with pytest.raises(TypeError):
283+
phi /= 2.71
284+
263285

264286
def test_combo_flatten():
265287
"""

0 commit comments

Comments
 (0)