◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
44 changes: 35 additions & 9 deletions quantities/quantity.py
Original file line number Diff line number Diff line change
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

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)

@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)

@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)

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
Toggle all file notes Toggle all file annotations