Skip to content

Commit

Permalink
add test / fix for lpdot
Browse files Browse the repository at this point in the history
  • Loading branch information
frrad committed Aug 21, 2024
1 parent 7286f40 commit a647edd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pulp/pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2234,12 +2234,15 @@ def lpSum(vector):


def lpDot(v1, v2):
v1_iterable = const.isiterable(v1) and not isinstance(v1, LpAffineExpression)
v2_iterable = const.isiterable(v2) and not isinstance(v2, LpAffineExpression)

"""Calculate the dot product of two lists of linear expressions"""
if not const.isiterable(v1) and not const.isiterable(v2):
if not v1_iterable and not v2_iterable:
return v1 * v2
elif not const.isiterable(v1):
elif not v1_iterable:
return lpDot([v1] * len(v2), v2)
elif not const.isiterable(v2):
elif not v2_iterable:
return lpDot(v1, [v2] * len(v1))
else:
return lpSum([lpDot(e1, e2) for e1, e2 in zip(v1, v2)])
8 changes: 8 additions & 0 deletions pulp/tests/test_lpdot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pulp import lpDot, LpVariable


def test_lpdot():
x = LpVariable(name="x")

product = lpDot([1], [2 * x])
assert product.toDict() == [{"name": "x", "value": 2}]

0 comments on commit a647edd

Please sign in to comment.