diff --git a/pulp/constants.py b/pulp/constants.py index 00fd751b..976bb457 100644 --- a/pulp/constants.py +++ b/pulp/constants.py @@ -87,15 +87,6 @@ LpCplexLPLineSize = 78 -def isiterable(obj): - try: - obj = iter(obj) - except: - return False - else: - return True - - class PulpError(Exception): """ Pulp Exception Class diff --git a/pulp/pulp.py b/pulp/pulp.py index 79b473b1..a05a964f 100644 --- a/pulp/pulp.py +++ b/pulp/pulp.py @@ -2264,13 +2264,17 @@ def lpSum(vector): return LpAffineExpression().addInPlace(vector) +def _vector_like(obj): + return isinstance(obj, Iterable) and not isinstance(obj, LpAffineExpression) + + def lpDot(v1, v2): """Calculate the dot product of two lists of linear expressions""" - if not const.isiterable(v1) and not const.isiterable(v2): + if not _vector_like(v1) and not _vector_like(v2): return v1 * v2 - elif not const.isiterable(v1): + elif not _vector_like(v1): return lpDot([v1] * len(v2), v2) - elif not const.isiterable(v2): + elif not _vector_like(v2): return lpDot(v1, [v2] * len(v1)) else: return lpSum([lpDot(e1, e2) for e1, e2 in zip(v1, v2)]) diff --git a/pulp/tests/test_lpdot.py b/pulp/tests/test_lpdot.py new file mode 100644 index 00000000..7e48af3f --- /dev/null +++ b/pulp/tests/test_lpdot.py @@ -0,0 +1,22 @@ +from pulp import lpDot, LpVariable + + +def test_lpdot(): + x = LpVariable(name="x") + + product = lpDot(1, 2 * x) + assert product.toDict() == [{"name": "x", "value": 2}] + + +def test_pulp_002(): + """ + Test the lpDot operation + """ + x = LpVariable("x") + y = LpVariable("y") + z = LpVariable("z") + a = [1, 2, 3] + assert dict(lpDot([x, y, z], a)) == {x: 1, y: 2, z: 3} + assert dict(lpDot([2 * x, 2 * y, 2 * z], a)) == {x: 2, y: 4, z: 6} + assert dict(lpDot([x + y, y + z, z], a)) == {x: 1, y: 3, z: 5} + assert dict(lpDot(a, [x + y, y + z, z])) == {x: 1, y: 3, z: 5}