Skip to content

Commit

Permalink
Converts empty CompositeDimension to NON_DIMENSIONAL when produced fr…
Browse files Browse the repository at this point in the history
…om multiplication or division
  • Loading branch information
Maxcode123 committed Sep 19, 2024
1 parent 258ac67 commit d392515
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __mul__(self, other) -> "Property":
if isinstance(other, Property):
_other = self._unit_preconversion(other)
return Property(
self.value * _other.value, (self.unit * _other.unit).simplified()
self.value * _other.value, self._simplify_units(self.unit * _other.unit)
)
raise PropertyBinaryOperationError(
f"cannot multiply {self} with {other}; "
Expand Down Expand Up @@ -268,7 +268,7 @@ def __truediv__(self, other) -> "Property":
raise PropertyBinaryOperationError(
f"cannot divide {self} with {other}; denominator's value is zero. "
) from None
return Property(value, (self.unit / _other.unit).simplified())
return Property(value, self._simplify_units(self.unit / _other.unit))
raise PropertyBinaryOperationError(
f"cannot divide {self} with {other}; "
"denominator must be numeric or Property. "
Expand Down Expand Up @@ -299,7 +299,7 @@ def __rtruediv__(self, other) -> "Property":
raise PropertyBinaryOperationError(
f"cannot divide {self} with {other}; denominator's value is zero. "
) from None
return Property(value, (other.unit / self.unit).simplified())
return Property(value, self._simplify_units(other.unit / self.unit))
raise PropertyBinaryOperationError(
f"cannot divide {self} with {other}; "
"numerator must be numeric or Property. "
Expand Down Expand Up @@ -727,3 +727,15 @@ def _simple_unit_preconversion(self, unit: MeasurementUnit) -> MeasurementUnit:
multiplication or division with this property.
"""
return self._dimension_unit_preconversion(unit**1).unit

def _simplify_units(self, unit: CompositeDimension) -> UnitDescriptor:
"""
Simplifies the composite dimension and returns NON_DIMENSIONAL if the simplified
composite does not have units.
"""
unit = unit.simplified()

if unit.has_no_units():
return NonDimensionalUnit.NON_DIMENSIONAL

return unit
12 changes: 12 additions & 0 deletions src/property_utils/tests/properties/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def test_with_exponentiated_property(self):
def test_with_inverse_property(self):
self.assert_result("10.4 ")

@args({"other": Property(2, Unit1.A ** (-1))})
def test_produced_units_with_inverse_property(self):
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)

@args({"other": Property(4, Unit2.B)})
def test_with_other_propperty(self):
self.assert_result("20.8 A * B")
Expand Down Expand Up @@ -499,6 +503,10 @@ def test_with_exponentiated_property(self):
def test_with_same_property(self):
self.assert_result("3.0 ")

@args({"other": Property(2, Unit1.A)})
def test_produced_units_with_same_property(self):
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)

@args({"other": Property(2, Unit2.B)})
def test_with_other_propperty(self):
self.assert_result("3.0 A / B")
Expand Down Expand Up @@ -639,6 +647,10 @@ def test_with_exponentiated_property(self):
def test_with_same_property(self):
self.assert_result("0.2 ")

@args({"other": Property(2, Unit1.A)})
def test_produced_units_with_same_property(self):
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)

@args({"other": Property(2, Unit2.B)})
def test_with_other_propperty(self):
self.assert_result("0.2 B / A")
Expand Down

0 comments on commit d392515

Please sign in to comment.