Expand Up
@@ -16,6 +16,9 @@
# e.g. PREFERRED = [pq.mV, pq.pA, pq.UnitQuantity('femtocoulomb', 1e-15*pq.C, 'fC')]
# Intended to be overwritten in down-stream packages
_np_version = tuple(map(int, np.__version__.split(".dev")[0].split(".")))
def validate_unit_quantity(value):
try:
assert isinstance(value, Quantity)
Expand Down
Expand Up
@@ -318,7 +321,7 @@ def __array_prepare__(self, obj, context=None):
return res
def __array_wrap__(self, obj, context=None, return_scalar=False):
_np_version = tuple(map(int, np.__version__.split(".dev")[0].split(".")))
# For NumPy < 2.0 we do old behavior
if _np_version < (2, 0, 0):
if not isinstance(obj, Quantity):
Expand All
@@ -342,7 +345,6 @@ def __add__(self, other):
@scale_other_units
def __radd__(self, other):
return np.add(other, self)
return super().__radd__(other)
@with_doc(np.ndarray.__iadd__)
@scale_other_units
Expand All
@@ -358,7 +360,6 @@ def __sub__(self, other):
@scale_other_units
def __rsub__(self, other):
return np.subtract(other, self)
return super().__rsub__(other)
@with_doc(np.ndarray.__isub__)
@scale_other_units
Expand All
@@ -378,22 +379,40 @@ def __imod__(self, other):
@with_doc(np.ndarray.__imul__)
@protected_multiplication
def __imul__(self, other):
return super().__imul__(other)
# the following is an inelegant fix for the removal of __array_prepare__ in NumPy 2.x
# the longer-term solution is probably to implement __array_ufunc__
# See:
# - https://numpy.org/devdocs/release/2.0.0-notes.html#array-prepare-is-removed
# - https://numpy.org/neps/nep-0013-ufunc-overrides.html
if _np_version < (2, 0, 0):
return super().__imul__(other)
else:
cself = self.copy()
cother = other.copy()
res = super().__imul__(other)
context = (np.multiply, (cself, cother, cself), 0)
return self.__array_prepare__(res, context=context)
@with_doc(np.ndarray.__rmul__)
def __rmul__(self, other):
return np.multiply(other, self)
return super().__rmul__(other)
@with_doc(np.ndarray.__itruediv__)
@protected_multiplication
def __itruediv__(self, other):
return super().__itruediv__(other)
# see comment above on __imul__
if _np_version < (2, 0, 0):
return super().__itruediv__(other)
else:
cself = self.copy()
cother = other.copy()
res = super().__itruediv__(other)
context = (np.true_divide, (cself, cother, cself), 0)
return self.__array_prepare__(res, context=context)
@with_doc(np.ndarray.__rtruediv__)
def __rtruediv__(self, other):
return np.true_divide(other, self)
return super().__rtruediv__(other)
@with_doc(np.ndarray.__pow__)
@check_uniform
Expand All
@@ -404,7 +423,15 @@ def __pow__(self, other):
@check_uniform
@protected_power
def __ipow__(self, other):
return super().__ipow__(other)
# see comment above on __imul__
if _np_version < (2, 0, 0):
return super().__ipow__(other)
else:
cself = self.copy()
cother = other.copy()
res = super().__ipow__(other)
context = (np.power, (cself, cother, cself), 0)
return self.__array_prepare__(res, context=context)
def __round__(self, decimals=0):
return np.around(self, decimals)
Expand Down
Expand Up
@@ -528,7 +555,6 @@ def sum(self, axis=None, dtype=None, out=None):
@with_doc(np.nansum)
def nansum(self, axis=None, dtype=None, out=None):
import numpy as np
return Quantity(
np.nansum(self.magnitude, axis, dtype, out),
self.dimensionality
Expand Down