Skip to content

Commit bd181b9

Browse files
fix other side
1 parent 3994b63 commit bd181b9

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

test/test_value_array.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,15 @@ def test_ufunc() -> None:
274274
x = np.float64(0.42)
275275
y = tu.GHz * np.arange(4)[1:]
276276

277-
assert (x * y).allclose(y * x)
278-
assert (x / y).allclose(np.int64(1) / (y / x))
277+
assert np.multiply(x, y).allclose(np.multiply(y, x))
278+
assert np.divide(x, y).allclose(np.divide(np.int64(1), np.divide(y, x)))
279279

280280
with pytest.raises(UnitMismatchError):
281-
_ = x + y
281+
_ = np.add(x, y)
282+
with pytest.raises(UnitMismatchError):
283+
_ = np.add(y, x)
282284

283285
with pytest.raises(UnitMismatchError):
284-
_ = x - y
286+
_ = np.subtract(x, y)
287+
with pytest.raises(UnitMismatchError):
288+
_ = np.subtract(y, x)

tunits/core/cython/with_unit_value_array.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ class ValueArray(WithUnit):
9595

9696
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
9797
if method == "__call__":
98+
is_value = isinstance(inputs[0], ValueArray)
9899
if ufunc == np.add:
99-
return inputs[1].__radd__(inputs[0])
100+
return inputs[0].__add__(inputs[1]) if is_value else inputs[1].__radd__(inputs[0])
100101
if ufunc == np.subtract:
101-
return inputs[1].__rsub__(inputs[0])
102+
return inputs[0].__sub__(inputs[1]) if is_value else inputs[1].__rsub__(inputs[0])
102103
if ufunc == np.multiply:
103-
return inputs[1].__rmul__(inputs[0])
104+
return inputs[0].__mul__(inputs[1]) if is_value else inputs[1].__rmul__(inputs[0])
104105
if ufunc == np.divide:
105-
return inputs[1].__rtruediv__(inputs[0])
106+
return inputs[0].__truediv__(inputs[1]) if is_value else inputs[1].__rtruediv__(inputs[0])
106107
if ufunc == np.power:
107108
return inputs[0] ** inputs[1]
108109
if ufunc in [np.positive, np.negative, np.abs, np.fabs, np.conj]:

0 commit comments

Comments
 (0)