Ticket #10053: trac_10053_equality_testing_forms.patch

File trac_10053_equality_testing_forms.patch, 6.6 KB (added by jvkersch, 11 years ago)
  • sage/tensor/coordinate_patch.py

    # HG changeset patch
    # User Joris Vankerschaver <joris.vankerschaver@gmail.com>
    # Date 1286339483 25200
    # Node ID d0961d2f45f35b58c99d91d3ae2d796eb37b4284
    # Parent  1089f3d46aca6352a4b17654e4f3107573b98a8b
    #10053: Equality instead of comparison for differential forms
    
    diff -r 1089f3d46aca -r d0961d2f45f3 sage/tensor/coordinate_patch.py
    a b  
    111111
    112112
    113113   
    114     def __cmp__(self, other):   
     114    def __eq__(self, other):   
    115115        """
    116         Compare self and other.  Return equality if and only if other
    117         has the same coordinates as self, in the same order.
     116        Return equality if and only if other has the same coordinates
     117        as self, in the same order.
    118118   
    119119        EXAMPLES::
    120120
     
    126126            Open subset of R^2 with coordinates u, v
    127127            sage: U = CoordinatePatch((x, y, z)); U
    128128            Open subset of R^3 with coordinates x, y, z
    129             sage: cmp(U, S)
    130             0
    131             sage: cmp(U, U)
    132             0
    133             sage: cmp(U, T)
    134             1
     129            sage: U == S
     130            True
     131            sage: U == U
     132            True
     133            sage: U == T
     134            False
    135135
    136136        Note that the order of the coordinates matters::
    137137
     
    140140            Open subset of R^3 with coordinates x, y, z
    141141            sage: T = CoordinatePatch((x, z, y)); T
    142142            Open subset of R^3 with coordinates x, z, y
    143             sage: cmp(S, T)
    144             -1
     143            sage: S == T
     144            False
    145145        """       
    146146
    147         # Convert tuples of coordinates to strings before comparing
    148         # (see Trac 10041)
     147        return str(self._coordinates) == str(other._coordinates)
    149148
    150         return cmp(str(self._coordinates), str(other._coordinates))
     149
     150    def __ne__(self, other):   
     151        """
     152        Test whether two coordinate patches are not equal.
     153   
     154        EXAMPLES::
     155
     156            sage: x, y, z = var('x, y, z')
     157            sage: S = CoordinatePatch((x, y, z)); S
     158            Open subset of R^3 with coordinates x, y, z
     159            sage: u, v = var('u, v')
     160            sage: T = CoordinatePatch((u, v)); T
     161            Open subset of R^2 with coordinates u, v
     162            sage: S != T
     163            True
     164        """       
     165
     166        return not self.__eq__(other)
     167
    151168       
    152169       
    153170    def coordinates(self):
  • sage/tensor/differential_form_element.py

    diff -r 1089f3d46aca -r d0961d2f45f3 sage/tensor/differential_form_element.py
    a b  
    541541            True
    542542        """
    543543        if type(other) == type(self):
    544             return self.__dict__ == other.__dict__
    545         return False
     544            if self._degree != other._degree:
     545                return False
     546            else:
     547                # TODO: the following two lines are where most of the
     548                # execution time is spent. 
     549                self._cleanup()
     550                other._cleanup()
     551
     552                if len(self._components) != len(other._components):
     553                    return False
     554
     555
     556                # We compare the component dictionary of both differential
     557                # forms, keeping in mind that the set of keys is
     558                # lexicographically ordered, so that we can simply iterate
     559                # over both dictionaries in one go and compare (key, value)
     560                # pairs as we go along.
     561
     562                for (key1, val1), (key2, val2) in \
     563                        zip(self._components.iteritems(), \
     564                            other._components.iteritems()):
     565                    if key1 != key2 or str(val1) != str(val2):
     566                        return False
     567                return True
     568        else:
     569            return False
    546570       
    547571   
    548     #def __ne__(self, other):
    549     #    return not self.__eq__(other)
     572    def __ne__(self, other):
     573        r"""
     574        Test whether two differential forms are not equal.
     575
     576        EXAMPLES::
     577
     578            sage: F = DifferentialForms(); F
     579            Algebra of differential forms in the variables x, y, z
     580            sage: f = DifferentialForm(F, 2)
     581            sage: f[1,2] = x; f
     582            x*dy/\dz
     583            sage: g = DifferentialForm(F, 3)
     584            sage: g[0, 1, 2] = 1; g
     585            dx/\dy/\dz
     586            sage: f != g
     587            True
     588        """
     589
     590
     591        return not self.__eq__(other)
    550592
    551593
    552594    def _neg_(self):
     
    679721        zeros = []
    680722       
    681723        for comp in self._components:
    682             if self._components[comp] == 0:
     724            if self._components[comp].is_zero():
    683725                zeros.append(comp)
    684726               
    685727        for comp in zeros: 
  • sage/tensor/differential_forms.py

    diff -r 1089f3d46aca -r d0961d2f45f3 sage/tensor/differential_forms.py
    a b  
    111111                                category = GradedAlgebrasWithBasis(SR))
    112112
    113113
    114     def __cmp__(self, other):
     114    def __eq__(self, other):
    115115        """
    116         Compare self with other.
     116        Return True if self is equal to other.
    117117
    118118        EXAMPLES::
    119119
     
    129129            Algebra of differential forms in the variables p, q
    130130            sage: H = DifferentialForms(U); H
    131131            Algebra of differential forms in the variables x, y, z
    132             sage: cmp(F, G)
    133             1
    134             sage: cmp(F, H)
    135             0
     132            sage: F == G
     133            False
     134            sage: F == H
     135            True
    136136        """
    137137
    138138        if type(other) == type(self):
    139             return cmp(self._patch, other._patch)
     139            return self._patch == other._patch
    140140        else:
    141             return -1
     141            return False
     142
     143
     144    def __ne__(self, other):
     145        """
     146        Return True if self is not equal to other.
     147
     148        EXAMPLES::
     149
     150            sage: x, y, z = var('x, y, z')
     151            sage: U = CoordinatePatch((x, y, z)); U
     152            Open subset of R^3 with coordinates x, y, z
     153            sage: F = DifferentialForms(U); F
     154            Algebra of differential forms in the variables x, y, z
     155            sage: p, q = var('p, q')
     156            sage: V = CoordinatePatch((p, q)); V
     157            Open subset of R^2 with coordinates p, q
     158            sage: G = DifferentialForms(V); G
     159            Algebra of differential forms in the variables p, q
     160            sage: F != G
     161            True
     162        """
     163
     164        return not self.__eq__(other)
     165
    142166
    143167   
    144168    def ngens(self):