Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions highs/highspy/highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,15 @@ def __ge__(self, other: Union[float, int, highs_var, highs_linear_expression]):
else:
return highs_linear_expression(self).__ge__(other)

# self / scalar
def __truediv__(self, other: Union[float, int, highs_linear_expression]):
expr = highs_linear_expression(self)
expr /= other
return expr

# scalar / self
def __rtruediv__(self, other: Union[float, int, highs_linear_expression]):
raise Exception("Only division of a linear expression by a scalar is allowed.")

# highs constraint
class highs_cons(object):
Expand Down Expand Up @@ -2243,6 +2252,32 @@ def __isub__(self, other: Union[float, int, highs_var, highs_linear_expression])

self.constant = (self.constant or 0.0) - float(other)
return self

# expr / scalar
def __truediv__(self, other: Union[float, int, highs_linear_expression]):
copy = highs_linear_expression(self)
copy /= other
return copy

# expr /= scalar
def __itruediv__(self, other: Union[float, int, highs_linear_expression]):
if isinstance(other, (float, int)):
divisor = float(other)

elif isinstance(other, highs_linear_expression) and other.idxs == [] and other.constant is not None:
divisor = float(other.constant)

else:
raise Exception("Only division by a scalar is allowed.")

if divisor == 0:
raise ZeroDivisionError("division by zero")

return self.__imul__(1.0 / divisor)

# scalar / expr
def __rtruediv__(self, other: Union[float, int, highs_var, highs_linear_expression]):
raise Exception("Only division of a linear expression by a scalar is allowed.")

def __imul__(self, other: Union[float, int, highs_var, highs_linear_expression]):
if isinstance(other, (float, int)):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_highspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,36 @@ def test_multiply(self):
e1 *= x - 4 <= 3
self.assertEqualExpr(e1, [x], [1], None, [-self.h.inf, 7])

def test_divide(self):
x, y = self.x[0:2]

expr = x / 4
self.assertEqualExpr(expr, [x], [0.25])

expr = (x + 2 * y + 8) / 4
self.assertEqualExpr(expr, [x, y], [0.25, 0.5], 2)

expr = (4 <= x + 2 * y <= 8) / 2
self.assertEqualExpr(expr, [x, y], [0.5, 1], None, [2, 4])

expr = (x + 2 * y) / highs_linear_expression(4)
self.assertEqualExpr(expr, [x, y], [0.25, 0.5])

expr = x + 2 * y + 8
expr /= 4
self.assertEqualExpr(expr, [x, y], [0.25, 0.5], 2)

expr = 4 <= x + 2 * y <= 8
expr /= -2
self.assertEqualExpr(expr, [x, y], [-0.5, -1], None, [-4, -2])

self.assertRaises(Exception, lambda: x / y)
self.assertRaises(Exception, lambda: (x + y) / (x + 1))
self.assertRaises(Exception, lambda: 2 / x)
self.assertRaises(Exception, lambda: 2 / (x + y))
self.assertRaises(ZeroDivisionError, lambda: x / 0)
self.assertRaises(ZeroDivisionError, lambda: (x + y) / highs_linear_expression(0))

def test_simplify(self):
x, y, z = self.x[0:3]

Expand Down
Loading