Skip to content

Commit

Permalink
recursive for iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimosts committed Sep 9, 2024
1 parent fd045fd commit 8d42a1e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 5 additions & 2 deletions cpmpy/expressions/python_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import builtins # to use the original Python-builtins

from .utils import is_false_cst, is_true_cst, is_any_list
from .variables import NDVarArray
from .variables import NDVarArray, cpm_array
from .core import Expression, Operator
from .globalfunctions import Minimum, Maximum, Abs
from ..exceptions import CPMpyException
Expand Down Expand Up @@ -136,7 +136,10 @@ def abs(element):
if the element given is not a CPMpy expression, the built-in is called
else an Absolute functional global constraint is constructed.
"""
if is_any_list(element) or not isinstance(element, Expression):
if is_any_list(element):
return cpm_array([abs(elem) for elem in element])

if isinstance(element, Expression):
return builtins.abs(element)

return Abs(element)
4 changes: 1 addition & 3 deletions tests/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ def test_abs(self):

model = cp.Model(cp.abs(iv[0]).decompose_comparison('!=', 4))
self.assertTrue(model.solve())
self.assertNotEqual(str(cp.abs(iv[0].value())), '4')

self.assertRaises(CPMpyException)
self.assertNotEqual(str(cp.abs(iv[0].value())), '4')

0 comments on commit 8d42a1e

Please sign in to comment.