Skip to content

Commit

Permalink
Merge pull request #685 from hayesla/truediv
Browse files Browse the repository at this point in the history
Adding __rtruediv__
  • Loading branch information
DanRyanIrish authored Apr 23, 2024
2 parents b1a30c8 + 72cbaca commit 64b5f99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/685.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable rtruediv on`~ndcube.NDCube` such that a user can now do 1/`~ndcube.NDCube`.
3 changes: 3 additions & 0 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,9 @@ def __rmul__(self, value):
def __truediv__(self, value):
return self.__mul__(1/value)

def __rtruediv__(self, value):
return self.__pow__(-1).__mul__(value)

def __pow__(self, value):
new_data = self.data ** value
new_unit = self.unit if self.unit is None else self.unit ** value
Expand Down
14 changes: 14 additions & 0 deletions ndcube/tests/test_ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,20 @@ def test_cube_arithmetic_divide(ndcube_2d_ln_lt_units, value):
new_cube = ndcube_2d_ln_lt_units / value
check_arithmetic_value_and_units(new_cube, cube_quantity / value)

@pytest.mark.parametrize('value', [1, 2, -1])
def test_cube_arithmetic_rdivide(ndcube_2d_ln_lt_units, value):
cube_quantity = u.Quantity(ndcube_2d_ln_lt_units.data, ndcube_2d_ln_lt_units.unit)
with np.errstate(divide='ignore'):
new_cube = value / ndcube_2d_ln_lt_units
check_arithmetic_value_and_units(new_cube, value / cube_quantity)

@pytest.mark.parametrize('value', [1, 2, -1])
def test_cube_arithmetic_rdivide_uncertainty(ndcube_4d_unit_uncertainty, value):
cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit)
with pytest.warns(UserWarning, match="UnknownUncertainty does not support uncertainty propagation with correlation. Setting uncertainties to None."):
with np.errstate(divide='ignore'):
new_cube = value / ndcube_4d_unit_uncertainty
check_arithmetic_value_and_units(new_cube, value / cube_quantity)

def test_cube_arithmetic_neg(ndcube_2d_ln_lt_units):
check_arithmetic_value_and_units(
Expand Down

0 comments on commit 64b5f99

Please sign in to comment.