[yt-svn] commit/yt: ngoldbaum: Merged in dHannasch/yt_grid_traversal (pull request #1966)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Feb 3 09:16:45 PST 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/03adb8fb971d/
Changeset:   03adb8fb971d
Branch:      yt
User:        ngoldbaum
Date:        2016-02-03 17:16:40+00:00
Summary:     Merged in dHannasch/yt_grid_traversal (pull request #1966)

Compare YTArrays without copying them
Affected #:  2 files

diff -r 323a5fd343c253c2211eb385d8f5a87a241ad7d1 -r 03adb8fb971da512e3b17bfb39ad595eb59819ec yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -382,6 +382,7 @@
     a1 = YTArray([1, 2, 3], 'cm')
     a2 = YTArray([2, 1, 3], 'cm')
     a3 = YTArray([.02, .01, .03], 'm')
+    dimless = np.array([2,1,3])
 
     ops = (
         np.less,
@@ -403,12 +404,22 @@
 
     for op, answer in zip(ops, answers):
         yield operate_and_compare, a1, a2, op, answer
+    for op, answer in zip(ops, answers):
+        yield operate_and_compare, a1, dimless, op, answer
 
     for op in ops:
         yield assert_raises, YTUfuncUnitError, op, a1, a3
 
     for op, answer in zip(ops, answers):
         yield operate_and_compare, a1, a3.in_units('cm'), op, answer
+    
+    # Check that comparisons with dimensionless quantities work in both directions.
+    yield operate_and_compare, a3, dimless, np.less, [True, True, True]
+    yield operate_and_compare, dimless, a3, np.less, [False, False, False]
+    yield assert_equal, a1 < 2, [True, False, False]
+    yield assert_equal, a1 < 2, np.less(a1, 2)
+    yield assert_equal, 2 < a1, [False, False, True]
+    yield assert_equal, 2 < a1, np.less(2, a1)
 
 
 def test_unit_conversions():

diff -r 323a5fd343c253c2211eb385d8f5a87a241ad7d1 -r 03adb8fb971da512e3b17bfb39ad595eb59819ec yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1048,15 +1048,16 @@
     #
 
     # @todo: outsource to a single method with an op argument.
+
     def __lt__(self, other):
         """ Test if this is less than the object on the right. """
-        oth = validate_comparison_units(self, other, 'less_than')
-        return np.array(self).__lt__(np.array(oth))
+        oth = validate_comparison_units(self, other, 'less_than') # converts if possible
+        return super(YTArray, self).__lt__(oth)
 
     def __le__(self, other):
         """ Test if this is less than or equal to the object on the right. """
         oth = validate_comparison_units(self, other, 'less_than or equal')
-        return np.array(self).__le__(np.array(oth))
+        return super(YTArray, self).__le__(oth)
 
     def __eq__(self, other):
         """ Test if this is equal to the object on the right. """
@@ -1065,7 +1066,7 @@
             # self is a YTArray, so it can't be None.
             return False
         oth = validate_comparison_units(self, other, 'equal')
-        return np.array(self).__eq__(np.array(oth))
+        return super(YTArray, self).__eq__(oth)
 
     def __ne__(self, other):
         """ Test if this is not equal to the object on the right. """
@@ -1073,19 +1074,19 @@
         if other is None:
             return True
         oth = validate_comparison_units(self, other, 'not equal')
-        return np.array(self).__ne__(np.array(oth))
+        return super(YTArray, self).__ne__(oth)
 
     def __ge__(self, other):
         """ Test if this is greater than or equal to other. """
         # Check that the other is a YTArray.
         oth = validate_comparison_units(self, other, 'greater than or equal')
-        return np.array(self).__ge__(np.array(oth))
+        return super(YTArray, self).__ge__(oth)
 
     def __gt__(self, other):
         """ Test if this is greater than the object on the right. """
         # Check that the other is a YTArray.
         oth = validate_comparison_units(self, other, 'greater than')
-        return np.array(self).__gt__(np.array(oth))
+        return super(YTArray, self).__gt__(oth)
 
     #
     # End comparison operators
@@ -1164,7 +1165,9 @@
                     unit2 = 1.0
             unit_operator = self._ufunc_registry[context[0]]
             if unit_operator in (preserve_units, comparison_unit, arctan2_unit):
-                if unit1 != unit2:
+                # Allow comparisons with dimensionless quantities.
+                if (unit1 != unit2 and
+                    not unit2.is_dimensionless and not unit1.is_dimensionless):
                     if not unit1.same_dimensions_as(unit2):
                         raise YTUnitOperationError(context[0], unit1, unit2)
                     else:

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list