Skip to content

Commit

Permalink
Enable modulo calculation on VariableView
Browse files Browse the repository at this point in the history
  • Loading branch information
mstimberg committed Mar 1, 2023
1 parent 738f258 commit 9050cc7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions brian2/core/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,9 @@ def __rtruediv__(self, other):
def __rfloordiv__(self, other):
return np.asanyarray(other) // self.get_item(slice(None), level=1)

def __mod__(self, other):
return self.get_item(slice(None), level=1) % np.asanyarray(other)

def __pow__(self, power, modulo=None):
if modulo is not None:
return self.get_item(slice(None), level=1) ** power % modulo
Expand Down
3 changes: 3 additions & 0 deletions brian2/tests/test_neurongroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ def test_variableview_calculations():
"""
x : 1
y : volt
idx : integer
""",
)
G.x = np.arange(10)
G.y = np.arange(10)[::-1] * mV
G.idx = np.arange(10, dtype=int)
assert_allclose(G.x * G.y, np.arange(10) * np.arange(10)[::-1] * mV)
assert_allclose(-G.x, -np.arange(10))
assert_allclose(-G.y, -np.arange(10)[::-1] * mV)
Expand All @@ -113,6 +115,7 @@ def test_variableview_calculations():
assert_allclose(G.y * 3, 3 * np.arange(10)[::-1] * mV)
assert_allclose(G.x / 2.0, np.arange(10) / 2.0)
assert_allclose(G.y / 2, np.arange(10)[::-1] * mV / 2)
assert_equal(G.idx % 2, np.arange(10, dtype=int) % 2)
assert_allclose(G.x + 2, 2 + np.arange(10))
assert_allclose(G.y + 2 * mV, 2 * mV + np.arange(10)[::-1] * mV)
assert_allclose(2 + G.x, 2 + np.arange(10))
Expand Down

0 comments on commit 9050cc7

Please sign in to comment.