Skip to content

Commit

Permalink
Change comparison of Quantity (to fix python-quantitiesgh-146)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Jan 28, 2018
1 parent 4a9d268 commit ec1ca74
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
37 changes: 21 additions & 16 deletions quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,43 +375,48 @@ def __setitem__(self, key, value):
value = value.rescale(self._dimensionality)
self.magnitude[key] = value

def _magnitude_of_other(self, other):
if not isinstance(other, Quantity):
other = other * unit_registry['dimensionless']
return other.rescale(self._dimensionality).magnitude

@with_doc(np.ndarray.__lt__)
@wrap_comparison
def __lt__(self, other):
return self.magnitude < other
return self.magnitude < self._magnitude_of_other(other)

@with_doc(np.ndarray.__le__)
@wrap_comparison
def __le__(self, other):
return self.magnitude <= other
return self.magnitude <= self._magnitude_of_other(other)

@with_doc(np.ndarray.__eq__)
def __eq__(self, other):
if isinstance(other, Quantity):
try:
other = other.rescale(self._dimensionality).magnitude
except ValueError:
return np.zeros(self.shape, '?')
return self.magnitude == other
try:
other_mag = self._magnitude_of_other(other)
except ValueError:
return np.zeros(self.shape, '?')
else:
return self.magnitude == other_mag

@with_doc(np.ndarray.__ne__)
def __ne__(self, other):
if isinstance(other, Quantity):
try:
other = other.rescale(self._dimensionality).magnitude
except ValueError:
return np.ones(self.shape, '?')
return self.magnitude != other
try:
other_mag = self._magnitude_of_other(other)
except ValueError:
return np.ones(self.shape, '?')
else:
return self.magnitude != other_mag

@with_doc(np.ndarray.__ge__)
@wrap_comparison
def __ge__(self, other):
return self.magnitude >= other
return self.magnitude >= self._magnitude_of_other(other)

@with_doc(np.ndarray.__gt__)
@wrap_comparison
def __gt__(self, other):
return self.magnitude > other
return self.magnitude > self._magnitude_of_other(other)

#I don't think this implementation is particularly efficient,
#perhaps there is something better
Expand Down
38 changes: 24 additions & 14 deletions quantities/tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def test_scalar_equality(self):
self.assertEqual(pq.J == 2*pq.kg*pq.m**2/pq.s**2, [False])

self.assertEqual(pq.J == pq.kg, [False])
self.assertTrue(1e3*pq.m == pq.km)
self.assertFalse(1e3*pq.m == [1e3])

def test_scalar_inequality(self):
self.assertEqual(pq.J != pq.erg, [True])
Expand Down Expand Up @@ -66,7 +68,7 @@ def test_array_equality(self):
)
self.assertQuantityEqual(
[1, 2, 3, 4]*pq.J == [1, 22, 3, 44],
[1, 0, 1, 0]
[0, 0, 0, 0]
)

def test_array_inequality(self):
Expand All @@ -80,7 +82,7 @@ def test_array_inequality(self):
)
self.assertQuantityEqual(
[1, 2, 3, 4]*pq.J != [1, 22, 3, 44],
[0, 1, 0, 1]
[1, 1, 1, 1]
)

def test_quantity_less_than(self):
Expand All @@ -92,9 +94,11 @@ def test_quantity_less_than(self):
[50, 100, 150]*pq.cm < [1, 1, 1]*pq.m,
[1, 0, 0]
)
self.assertQuantityEqual(
[1, 2, 33]*pq.J < [1, 22, 3],
[0, 1, 0]
self.assertRaises(
ValueError,
op.lt,
[1, 2, 33]*pq.J,
[1, 22, 3]
)
self.assertRaises(
ValueError,
Expand All @@ -112,9 +116,11 @@ def test_quantity_less_than_or_equal(self):
[50, 100, 150]*pq.cm <= [1, 1, 1]*pq.m,
[1, 1, 0]
)
self.assertQuantityEqual(
[1, 2, 33]*pq.J <= [1, 22, 3],
[1, 1, 0]
self.assertRaises(
ValueError,
op.le,
[1, 2, 33]*pq.J,
[1, 22, 3]
)
self.assertRaises(
ValueError,
Expand All @@ -132,9 +138,11 @@ def test_quantity_greater_than_or_equal(self):
[50, 100, 150]*pq.cm >= [1, 1, 1]*pq.m,
[0, 1, 1]
)
self.assertQuantityEqual(
[1, 2, 33]*pq.J >= [1, 22, 3],
[1, 0, 1]
self.assertRaises(
ValueError,
op.ge,
[1, 2, 33]*pq.J,
[1, 22, 3]
)
self.assertRaises(
ValueError,
Expand All @@ -152,9 +160,11 @@ def test_quantity_greater_than(self):
[50, 100, 150]*pq.cm > [1, 1, 1]*pq.m,
[0, 0, 1]
)
self.assertQuantityEqual(
[1, 2, 33]*pq.J > [1, 22, 3],
[0, 0, 1]
self.assertRaises(
ValueError,
op.gt,
[1, 2, 33]*pq.J,
[1, 22, 3]
)
self.assertRaises(
ValueError,
Expand Down

0 comments on commit ec1ca74

Please sign in to comment.