Skip to content
Snippets Groups Projects
Commit d34f3a55 authored by Jean-Baptiste Bayle's avatar Jean-Baptiste Bayle
Browse files

Allow multiplication of ForEachObject with arrays

parent cf9bc90e
No related branches found
No related tags found
1 merge request!143Resolve "Bug when computing THE with respect to TCB"
Pipeline #216090 passed
...@@ -20,6 +20,10 @@ logger = logging.getLogger(__name__) ...@@ -20,6 +20,10 @@ logger = logging.getLogger(__name__)
class ForEachObject(abc.ABC): class ForEachObject(abc.ABC):
"""Abstract class which represents a dictionary holding a value for each object.""" """Abstract class which represents a dictionary holding a value for each object."""
# Used to bypass Numpy's vectorization and use
# this class `__rmul__()` method for multiplication from both sides
__array_priority__ = 10000
def __init__(self, values, concurrent=False): def __init__(self, values, concurrent=False):
"""Initialize an object with a value or a function of the mosa index. """Initialize an object with a value or a function of the mosa index.
......
...@@ -195,6 +195,33 @@ def test_multiplication(): ...@@ -195,6 +195,33 @@ def test_multiplication():
result = object_2 * object_3 result = object_2 * object_3
def test_multiplication_array():
"""Check that we can multiply a ForEachObject instance with an array."""
array = np.random.normal(size=10)
object_1 = ForEachAB({'A': 1, 'B': 2})
object_2 = ForEachAB({
'A': np.random.normal(size=10),
'B': np.random.normal(size=10),
})
result = object_1 * array
assert np.all(result['A'] == array)
assert np.all(result['B'] == 2 * array)
result = array * object_1
assert np.all(result['A'] == array)
assert np.all(result['B'] == 2 * array)
result = object_2 * array
assert np.all(result['A'] == object_2['A'] * array)
assert np.all(result['B'] == object_2['B'] * array)
result = array * object_2
assert np.all(result['A'] == object_2['A'] * array)
assert np.all(result['B'] == object_2['B'] * array)
def test_floor_division(): def test_floor_division():
"""Check that we can apply floor division on two ForEachObject subclasses, or a scalar.""" """Check that we can apply floor division on two ForEachObject subclasses, or a scalar."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment