Skip to content

Commit

Permalink
support np.array for modeling
Browse files Browse the repository at this point in the history
- implement @ operator for np.array
- check * is not used with np.array
- add __array_ufunc__ = None to allow CyLPExpr to take over np.array broadcasting (remove need for PyLPArray)
  • Loading branch information
sdementen authored Jan 28, 2022
1 parent e619c4b commit dcfe9f4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cylp/py/modeling/CyLPModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def identitySub(var):
return I(n)[var.indices, :]

class CyLPExpr:
__array_ufunc__ = None

operators = ('>=', '<=', '==', '+', '-', '*', 'u-', 'sum')

def __init__(self, opr='', left='', right=''):
Expand Down Expand Up @@ -209,17 +211,33 @@ def __eq__(self, other):
return v

def __rmul__(self, other):
if type(other)==np.ndarray:
raise ValueError("You should use @ when multiplying a variable with a numpy array")
v = CyLPExpr(opr="*", left=other, right=self)
v.expr = v
self.expr = v
return v

def __mul__(self, other):
if type(other)==np.ndarray:
raise ValueError("You should use @ when multiplying a variable with a numpy array")
v = CyLPExpr(opr="*", left=self, right=other)
v.expr = v
self.expr = v
return v

def __rmatmul__(self, other):
v = CyLPExpr(opr="*", left=other, right=self)
v.expr = v
self.expr = v
return v

def __matmul__(self, other):
v = CyLPExpr(opr="*", left=self, right=other)
v.expr = v
self.expr = v
return v

def __rsub__(self, other):
v = CyLPExpr(opr="-", left=other, right=self)
v.expr = v
Expand Down

0 comments on commit dcfe9f4

Please sign in to comment.