Ticket #3738: 3738-13-docs.patch

File 3738-13-docs.patch, 29.7 KB (added by robertwb, 11 years ago)
  • sage/categories/map.pxd

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1217755443 25200
    # Node ID 808aa5d3d7aaf1bf832b6bb9346ecd40ec5eecb3
    # Parent  8bced26ee39af87d0629b2346739ad16404835d5
    Lots and lots of doctests
    
    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/categories/map.pxd
    a b cdef class Map(Element): 
    1717
    1818    cpdef domain(self)
    1919    cpdef codomain(self)
     20   
     21    cdef _repr_type_str
    2022
    2123
    2224cdef class Section(Map):
  • sage/categories/map.pyx

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/categories/map.pyx
    a b cdef class Map(Element): 
    6969        return unpickle_map, (self.__class__, self._parent, _dict, self._extra_slots({}))
    7070   
    7171    def _repr_type(self):
    72         return "Generic"
     72        if self._repr_type_str is None:
     73            return "Generic"
     74        else:
     75            return self._repr_type_str
    7376
    7477    def _repr_defn(self):
    7578        return ""
  • sage/matrix/matrix_space.py

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/matrix/matrix_space.py
    a b class MatrixSpace_generic(parent_gens.Pa 
    866866            True       
    867867        """
    868868        return self.__is_sparse
     869       
     870    def is_finite(self):
     871        """
     872        EXAMPLES:
     873            sage: MatrixSpace(GF(101), 10000).is_finite()
     874            True
     875            sage: MatrixSpace(QQ, 2).is_finite()
     876            False
     877        """
     878        return self.base_ring().is_finite()
    869879
    870880    def gen(self, n):
    871881        """
  • sage/sets/set.py

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/sets/set.py
    a b class Set_object(Set_generic): 
    457457            return len(self.__object)
    458458        except TypeError:
    459459            raise NotImplementedError, "computation of cardinality of %s not yet implemented"%self.__object
     460           
     461    def is_finite(self):
     462        """
     463        EXAMPLES:
     464            sage: Set(QQ).is_finite()
     465            False
     466            sage: Set(GF(250037)).is_finite()
     467            True
     468            sage: Set(Integers(2^1000000)).is_finite()
     469            True
     470        """
     471        return self.__object.is_finite()
    460472
    461473    def object(self):
    462474        """
  • sage/structure/coerce_maps.pyx

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/structure/coerce_maps.pyx
    a b cdef class DefaultConvertMap(Map): 
    2626        self._force_use = force_use
    2727        if self._codomain._element_constructor is None:
    2828            raise RuntimeError, "BUG in coercion model, no element constructor for %s" % type(self._codomain)
     29        self._repr_type_str = "Coercion" if self._is_coercion else "Conversion"
    2930
    3031    cpdef Element _call_(self, x):
    3132        try:
    cdef class DefaultConvertMap(Map): 
    5455                print type(self._codomain._element_constructor), self._codomain._element_constructor
    5556            raise
    5657
    57     def _repr_type(self):
    58         return "Conversion"
    5958
    6059cdef class DefaultConvertMap_unique(DefaultConvertMap):
    6160    """
    cdef class DefaultConvertMap_unique(Defa 
    9493                print type(self._codomain._element_constructor), self._codomain._element_constructor
    9594            raise
    9695
    97     def _repr_type(self):
    98         return "Coercion" if self._is_coercion else "Conversion"
    9996
    10097cdef class NamedConvertMap(Map):
    10198    """
    cdef class NamedConvertMap(Map): 
    122119        self._coerce_cost = 400
    123120        self._force_use = force_use
    124121        self.method_name = method_name
     122        self._repr_type_str = "Conversion via %s method" % self.method_name
    125123   
    126124    cpdef Element _call_(self, x):
    127125        """
    cdef class NamedConvertMap(Map): 
    155153            e = m._call_(e)
    156154        return e
    157155
    158     def _repr_type(self):
    159         return "Conversion via %s method" % self.method_name
    160    
     156
    161157cdef class CallableConvertMap(Map):
    162158    cdef bint _parent_as_first_arg
    163159    cdef _func
    cdef class CallableConvertMap(Map): 
    202198            else:
    203199                parent_as_first_arg = True
    204200        self._parent_as_first_arg = parent_as_first_arg
     201        try:
     202            self._repr_type_str = "Conversion via %s" % self._func.__name__
     203        except AttributeError:
     204            self._repr_type_str = "Conversion via %s" % self._func
    205205   
    206206    cpdef Element _call_(self, x):
    207207        """
    cdef class CallableConvertMap(Map): 
    225225            RuntimeError: BUG in coercion model: <function foo at ...> returned None
    226226        """
    227227        cdef Element y
    228         if self._parent_as_first_arg:
    229             y = self._func(self._codomain, x)
    230         else:
    231             y = self._func(x)
     228        try:
     229            if self._parent_as_first_arg:
     230                y = self._func(self._codomain, x)
     231            else:
     232                y = self._func(x)
     233        except:
     234            if print_warnings:
     235                print self._func
     236                print self._codomain
     237            raise
    232238        if y is None:
    233239            raise RuntimeError, "BUG in coercion model: %s returned None" % (self._func)
    234240        elif y._parent is not self._codomain:
    235241            raise RuntimeError, "BUG in coercion model: %s returned element with wrong parent (expected %s got %s)" % (self._func, self._codomain, y._parent)
    236242        return y
    237 
    238     def _repr_type(self):
     243       
     244    cpdef Element _call_with_args(self, x, args=(), kwds={}):
     245        """
     246        TESTS:
     247            sage: from sage.structure.coerce_maps import CallableConvertMap
     248            sage: def foo(P, x, y): return x or y
     249            sage: f = CallableConvertMap(ZZ, ZZ, foo)
     250            sage: f(0, 3)
     251            3
     252            sage: f = CallableConvertMap(ZZ, QQ, foo)
     253            sage: f(0, 3)
     254            Traceback (most recent call last):
     255            ...
     256            RuntimeError: BUG in coercion model: <function foo at ...> returned element with wrong parent (expected Rational Field got Integer Ring)
     257            sage: f(None, None)
     258            Traceback (most recent call last):
     259            ...
     260            RuntimeError: BUG in coercion model: <function foo at ...> returned None
     261        """
     262        cdef Element y
    239263        try:
    240             return "Conversion via %s" % self._func.__name__
    241         except AttributeError:
    242             return "Conversion via %s" % self._func
     264            if self._parent_as_first_arg:
     265                y = self._func(self._codomain, x, *args, **kwds)
     266            else:
     267                y = self._func(x, *args, **kwds)
     268        except:
     269            if print_warnings:
     270                print self._func
     271                print self._codomain
     272            raise
     273        if y is None:
     274            raise RuntimeError, "BUG in coercion model: %s returned None" % (self._func)
     275        elif y._parent is not self._codomain:
     276            raise RuntimeError, "BUG in coercion model: %s returned element with wrong parent (expected %s got %s)" % (self._func, self._codomain, y._parent)
     277        return y
    243278           
    244279           
    245280cdef class CCallableConvertMap_class(Map):
    cdef class CCallableConvertMap_class(Map 
    254289        self._name = name
    255290
    256291    cpdef Element _call_(self, x):
     292        """
     293        TESTS:
     294            sage: from sage.structure.coerce_maps import test_CCallableConvertMap
     295            sage: f = test_CCallableConvertMap(QQ, 'test')
     296            sage: f(1/3)
     297            -8/27
     298        """
    257299        return self._func(self._codomain, x)
    258300
    259301    def _repr_type(self):
    cdef class ListMorphism(Map): 
    332374        Map.__init__(self, domain, real_morphism.codomain())
    333375        self._coerce_cost = real_morphism._coerce_cost + 3
    334376        self._real_morphism = real_morphism
     377        self._repr_type_str = "List"
    335378
    336379    cpdef Element _call_(self, x):
    337380        try:
    cdef class ListMorphism(Map): 
    347390            x = list(x)
    348391        return self._real_morphism._call_with_args(x, args, kwds)
    349392       
    350     def _repr_type(self):
    351         return "List"
    352393
    353394cdef class TryMap(Map):
    354     def __init__(self, morphism_preferred, morphism_backup, error_types=None):
    355         if morphism_preferred.parent() is not morphism_backup.parent():
     395    def __init__(self, Map morphism_preferred, Map morphism_backup, error_types=None):
     396        """
     397        TESTS:
     398            sage: sage.structure.coerce_maps.TryMap(RDF.coerce_map_from(QQ), RDF.coerce_map_from(ZZ))
     399            Traceback (most recent call last):
     400            ...
     401            TypeError: incorrectly matching parent
     402        """
     403        if (morphism_preferred.domain() is not morphism_backup.domain()
     404             or morphism_preferred.codomain() is not morphism_backup.codomain()):
    356405            raise TypeError, "incorrectly matching parent"
    357406        Map.__init__(self, morphism_preferred.parent())
    358407        self._map_p = morphism_preferred
    cdef class TryMap(Map): 
    363412            self._error_types = error_types
    364413
    365414    cpdef Element _call_(self, x):
     415        """
     416        EXAMPLES:
     417            sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x: 1/x)
     418            sage: map2 = QQ.coerce_map_from(ZZ)
     419            sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
     420            sage: map(3)
     421            1/3
     422            sage: map(-7)
     423            -1/7
     424            sage: map(0)
     425            0
     426        """
    366427        try:
    367428            return self._map_p._call_(x)
    368429        except self._error_types:
    369430            return self._map_b._call_(x)
    370431
    371432    cpdef Element _call_with_args(self, x, args=(), kwds={}):
     433        """
     434        EXAMPLES:
     435            sage: map1 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y:  y/x)
     436            sage: map2 = sage.structure.coerce_maps.CallableConvertMap(ZZ, QQ, lambda parent, x, y: 23/1)
     437            sage: map = sage.structure.coerce_maps.TryMap(map1, map2, error_types=(ZeroDivisionError,))
     438            sage: map._call_with_args(3, (2,))
     439            2/3
     440            sage: map._call_with_args(-7, (5,))
     441            -5/7
     442            sage: map._call_with_args(0, (1,))
     443            23
     444        """
    372445        try:
    373446            return self._map_p._call_with_args(x, args, kwds)
    374447        except self._error_types:
  • sage/structure/parent.pxd

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/structure/parent.pxd
    a b cdef class Parent(category_object.Catego 
    1818    cdef public _initial_coerce_list
    1919    cdef public _initial_action_list
    2020    cdef public _initial_convert_list
     21
     22    # Called from the __init__ method to set up coercion.
     23    cdef int init_coerce(self, bint warn=*) except -1
    2124   
    2225    # New New Coercion support functionality
    2326
    cdef class Parent(category_object.Catego 
    4043   
    4144    # coerce x into self
    4245    cpdef coerce(self, x)
     46
    4347    cpdef an_element(self)
    4448    cpdef _an_element_(self)
     49    cdef public object __an_element
    4550
    4651
    4752    # For internal use
    cdef class Parent(category_object.Catego 
    7681    # An optional single Morphism that describes a cannonical coercion out of self
    7782    cdef _embedding
    7883
    79 
    80     #########################################
    81 
    82     cdef public object __an_element
    83     cpdef _an_element_impl(self)
    84 
  • sage/structure/parent.pyx

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/structure/parent.pyx
    a b cdef class Parent(category_object.Catego 
    158158#        except:
    159159#            print "couldn't weakref", type(self)
    160160       
    161     def init_coerce(self, bint verbose=True):
     161    cdef int init_coerce(self, bint warn=True) except -1:
    162162        if self._coerce_from_hash is None:
    163             if verbose:
     163            if warn:
    164164                print "init_coerce() for ", type(self)
    165165                raise ZeroDivisionError, "hello"
    166166            self._initial_coerce_list = []
    cdef class Parent(category_object.Catego 
    175175            self._embedding = None
    176176           
    177177    def _introspect_coerce(self):
     178        """
     179        Used for debugging the coercion model.
     180       
     181        EXAMPLES:
     182            sage: sorted(QQ._introspect_coerce().items())
     183            [('_action_hash', {...}),
     184             ('_action_list', []),
     185             ('_coerce_from_hash', {...}),
     186             ('_coerce_from_list', []),
     187             ('_convert_from_hash', {}),
     188             ('_convert_from_list', []),
     189             ('_element_init_pass_parent', False),
     190             ('_embedding', None),
     191             ('_initial_action_list', []),
     192             ('_initial_coerce_list', []),
     193             ('_initial_convert_list', [])]
     194        """
    178195        return {
    179196            '_coerce_from_list': self._coerce_from_list,
    180197            '_coerce_from_hash': self._coerce_from_hash,
    cdef class Parent(category_object.Catego 
    190207        }
    191208           
    192209    def __getstate__(self):
     210        """
     211        Used for pickling.
     212       
     213        TESTS:
     214            sage: loads(dumps(RR['x'])) == RR['x']
     215            True
     216        """
    193217        d = CategoryObject.__getstate__(self)
    194218        d['_embedding'] = self._embedding
    195219        d['_element_constructor'] = self._element_constructor
    cdef class Parent(category_object.Catego 
    201225        return d
    202226
    203227    def __setstate__(self, d):
     228        """
     229        Used for pickling.
     230
     231        TESTS:
     232            sage: loads(dumps(CDF['x'])) == CDF['x']
     233            True
     234        """
    204235        CategoryObject.__setstate__(self, d)
    205236        try:
    206237            version = d['_pickle_version']
    cdef class Parent(category_object.Catego 
    344375        return True
    345376
    346377    def __len__(self):
    347         return len(self.list())
     378        """
     379        Returns the number of elements in self. This is the naive algorithm
     380        of listing self and counting the elements.
     381       
     382        EXAMPLES:
     383            sage: len(GF(5))
     384            5
     385            sage: len(MatrixSpace(GF(2), 3, 3))
     386            512
     387        """
     388        cdef Py_ssize_t total = 0
     389        if self.is_finite():
     390            for x in self:
     391                total += 1
     392            return total
     393        else:
     394            raise ValueError, "%s has infinite cardinality" % self
    348395
    349396    def __getitem__(self, n):
     397        """
     398        Returns the $n$-th item of self, by getting self as a list.
     399       
     400        EXAMPLES:
     401            sage: MatrixSpace(GF(3), 2, 2)[9]
     402            [0 2]
     403            [0 0]
     404            sage: MatrixSpace(GF(3), 2, 2)[0]
     405            [0 0]
     406            [0 0]
     407        """
    350408        return self.list()[n]
    351409
    352410    def __getslice__(self,  Py_ssize_t n,  Py_ssize_t m):
     411        """
     412        Returns the $n$-th through $m$-th items in self.
     413       
     414        EXAMPLES:
     415            sage: VectorSpace(GF(7), 3)[:10]
     416            [(0, 0, 0),
     417             (1, 0, 0),
     418             (2, 0, 0),
     419             (3, 0, 0),
     420             (4, 0, 0),
     421             (5, 0, 0),
     422             (6, 0, 0),
     423             (0, 1, 0),
     424             (1, 1, 0),
     425             (2, 1, 0)]
     426        """
    353427        return self.list()[int(n):int(m)]
    354428
    355429    #################################################################################
    cdef class Parent(category_object.Catego 
    584658        elif embedding is not None:
    585659            raise TypeError, "embedding must be a parent or map"
    586660           
    587     def get_embedding(self):
     661    def coerce_embedding(self):
     662        """
     663        Returns the embedding of self into some other parent, if such a parent
     664        exists.
     665       
     666        This does not mean that there are no coercion maps from self into other
     667        fields, this is simply a specific morphism specified out of self and
     668        ususally denotes a special relationship (e.g. sub-objects, choice of
     669        completion, etc.)
     670
     671        EXAMPLES:
     672        """
    588673        return self._embedding
    589674           
    590675    cpdef _generic_convert_map(self, S):
     676        r"""
     677        Returns the default conversion map based on the data provided to
     678        \code{_populate_coercion_lists_()}.
     679       
     680        This called by the default \code{_coerce_map_from_()} when \code{has_coerce_map_from()}
     681        returns \code{True} and can be useful for implementing one own's
     682        \code{_coerce_map_from_()}.
     683       
     684        If a \code{convert_method_name} is provided, it creates a \code{NamedConvertMap}, otherwise
     685        it creates a \code{DefaultConvertMap} or \code{DefaultConvertMap_unique} depending
     686        on whether or not init_no_parent is set.
     687       
     688        EXAMPLES:
     689        """
    591690        import coerce_maps
    592691        if self._convert_method_name is not None:
    593692            # handle methods like _integer_
    cdef class Parent(category_object.Catego 
    612711        """
    613712        Return True if there is a natural map from S to self.
    614713        Otherwise, return False.
     714       
     715        EXAMPLES:
     716            sage: RDF.has_coerce_map_from(QQ)
     717            True
     718            sage: RDF.has_coerce_map_from(QQ['x'])
     719            False
     720            sage: RDF['x'].has_coerce_map_from(QQ['x'])
     721            True
     722            sage: RDF['x,y'].has_coerce_map_from(QQ['x'])
     723            True
    615724        """
    616725        if S is self:
    617726            return True
    cdef class Parent(category_object.Catego 
    629738        return self._has_coerce_map_from_(S)
    630739
    631740    cpdef bint _has_coerce_map_from_(self, S) except -2:
     741        """
     742        Override this method to specify coercions beyond those
     743        specified in coerce_list.
     744       
     745        EXAMPLES:
     746            sage: R.<x> = ZZ[]
     747            sage: S.<y> = R[]
     748            sage: S._has_coerce_map_from_(ZZ)
     749            True
     750        """
    632751        # We first check (using some cython trickery) to see if coerce_map_from_ has been overridden.
    633752        # If it has, then we can just (by default) call it and see if it returns None or not.
    634753        # Otherwise we return False by default (so that no canonical coercions exist)
    cdef class Parent(category_object.Catego 
    641760            return False
    642761        # At this point we are guaranteed coerce_map_from is actually implemented
    643762        return self.coerce_map_from(S) is not None
     763
     764    cpdef _coerce_map_from_(self, S):
     765        """
     766        Override this method to specify coercions beyond those
     767        specified in coerce_list.
     768       
     769        This method must return a Map object going from S to self,
     770        or None if no such coercion exists.
     771        """
     772        if self.has_coerce_map_from(S):
     773            return self._generic_convert_map(S)
     774        return None
    644775       
    645776    cpdef coerce_map_from(self, S):
     777        """
     778        This returns a Map object to coerce from S to self if one exists,
     779        or None if no such coercion exists.
     780       
     781        EXAMPLES:
     782            sage: ZZ.coerce_map_from(int)
     783            Native morphism:
     784              From: Set of Python objects of type 'int'
     785              To:   Integer Ring
     786            sage: QQ.coerce_map_from(ZZ)
     787            Natural morphism:
     788              From: Integer Ring
     789              To:   Rational Field
     790        """
    646791        cdef map.Map mor
    647792        if S is self:
    648793            from sage.categories.homset import Hom
    cdef class Parent(category_object.Catego 
    749894                    return best_mor
    750895
    751896        return best_mor
    752            
    753 
    754     cpdef _coerce_map_from_(self, S):
    755         if self.has_coerce_map_from(S):
    756             return self._generic_convert_map(S)
    757         return None
     897       
    758898
    759899    cpdef convert_map_from(self, S):
     900        """
     901        This function returns a Map from S to self, which may or may not
     902        succeed on all inputs. If a coercion map from S to self exists,
     903        then the it will be returned. If a coercion from self to S exists,
     904        then it will attempt to return a section of that map.
     905       
     906        Under the new coercion model, this is the fastest way to convert
     907        elements of S to elements of self (short of manually constructing
     908        the elements) and is used by __call__.
     909       
     910        EXAMPLES:
     911            sage: m = ZZ.convert_map_from(QQ)
     912            sage: m(-35/7)
     913            -5
     914            sage: parent(m(-35/7))
     915            Integer Ring
     916        """
    760917        if self._convert_from_hash is None: # this is because parent.__init__() does not always get called
    761918            self.init_coerce()
    762919        try:
    cdef class Parent(category_object.Catego 
    797954        return mor
    798955
    799956    cpdef _convert_map_from_(self, S):
     957        """
     958        Override this method to provide additional conversions beyond those
     959        given in convert_list.
     960       
     961        This function is called after coercions are attempted.
     962       
     963        This MUST return a Map from S to self, or None. If None is returned
     964        then a generic map will be provided.
     965        """
    800966        return None
    801967
    802968    cpdef get_action(self, S, op=operator.mul, bint self_on_left=True):
    803969        """
     970        Returns an action of self on S or S on self.
     971       
     972        To provide additional actions, override _get_action_.
     973       
    804974        TESTS:
    805975            sage: M = QQ['y']^3
    806976            sage: M.get_action(ZZ['x']['y'])
    cdef class Parent(category_object.Catego 
    9541124               
    9551125
    9561126    cpdef _get_action_(self, S, op, bint self_on_left):
     1127        """
     1128        Override this method to provide an action of self on S or S on self
     1129        beyond what was specified in action_list.
     1130       
     1131        This must return an action which accepts an element of self and an
     1132        element of S (in the order specified by self_on_left).
     1133        """
    9571134        return None
    9581135
    9591136    def construction(self):
    9601137        """
    9611138        Returns a pair (functor, parent) such that functor(parent) return self.
    9621139        If this ring does not have a functorial construction, return None.
     1140       
     1141        EXAMPLES:
     1142            sage: QQ.construction()
     1143            (FractionField, Integer Ring)
     1144            sage: f, R = QQ['x'].construction()
     1145            sage: f
     1146            PolynomialFunctor
     1147            sage: R
     1148            Rational Field
     1149            sage: f(R)
     1150            Univariate Polynomial Ring in x over Rational Field
    9631151        """
    9641152        return None
    9651153
    cdef class Parent(category_object.Catego 
    9681156        Implementation of a function that returns an element (often non-trivial)
    9691157        of a parent object.  This is cached. Parent structures that are should
    9701158        override \code{_an_element_} instead.
     1159
     1160        EXAMPLES:
     1161            sage: CDF.an_element()
     1162            1.0*I
     1163            sage: ZZ[['t']].an_element()
     1164            t
    9711165        """
    9721166        if self.__an_element is None:
    9731167            self.__an_element = self._an_element_()
    9741168        return self.__an_element
    975        
    976     cpdef _an_element_impl(self):
    977         """
    978         COERCE TODO
    979         Here so that I didn't have to change parent.pxd.  Should be eliminated and _an_element_ made cpdef.  Also need to change:
    980         sage/rings/real_rqdf.pyx
    981         sage/libs/pari/gen.pyx
    982         """
    983         return self._an_element_()
    9841169       
    9851170    cpdef _an_element_(self):
    9861171        """
    9871172        Returns an element of self. Want it in sufficent generality
    9881173        that poorly-written functions won't work when they're not
    9891174        supposed to. This is cached so doesn't have to be super fast.
     1175       
     1176        EXAMPLES:
     1177            sage: QQ._an_element_()
     1178            1/2
     1179            sage: ZZ['x,y,z']._an_element_()
     1180            x
    9901181        """
    9911182        try:
    9921183            return self.gen(0)
    cdef class Set_generic(Parent): # Cannot 
    10831274        return self
    10841275       
    10851276    def __nonzero__(self):
    1086         return len(self) != 0
     1277        """
     1278        A set is considered True unless it is empty, in which case it is
     1279        considered to be False.
     1280       
     1281        EXAMPLES:
     1282            sage: bool(Set(QQ))
     1283            True
     1284            sage: bool(Set(GF(3)))
     1285            True
     1286        """
     1287        return not (self.is_finite() and len(self) == 0)
    10871288
    10881289
    10891290
    cdef class Set_PythonType_class(Set_gene 
    11211322        self._type = theType
    11221323
    11231324    def __call__(self, x):
     1325        """
     1326        This doesn't return Elements, but actual objects of the given type.
     1327       
     1328        EXAMPLES:
     1329            sage: S = sage.structure.parent.Set_PythonType(float)
     1330            sage: S(5)
     1331            5.0
     1332            sage: S(9/3)
     1333            3.0
     1334            sage: S(1/3)
     1335            0.333333333333333...
     1336        """
    11241337        return self._type(x)
    11251338
    11261339    def __hash__(self):
     1340        """
     1341        TESTS:
     1342            sage: S = sage.structure.parent.Set_PythonType(int)
     1343            sage: hash(S) == -hash(int)
     1344            True
     1345        """
    11271346        return -hash(self._type)
    11281347       
    11291348    cpdef int _cmp_(self, other) except -100:
     1349        """
     1350        Two Python type sets are considered the same if they contain the same
     1351        type.
     1352       
     1353        EXAMPLES:
     1354            sage: S = sage.structure.parent.Set_PythonType(int)
     1355            sage: S == S
     1356            True
     1357            sage: S == sage.structure.parent.Set_PythonType(float)
     1358            False
     1359        """
    11301360        if self is other:
    11311361            return 0
    11321362        if isinstance(other, Set_PythonType_class):
    cdef class Set_PythonType_class(Set_gene 
    11351365            return cmp(self._type, other)
    11361366           
    11371367    def __contains__(self, x):
     1368        """
     1369        Only things of the right type (or subtypes thereof) are considered to
     1370        belong to the set.
     1371       
     1372        EXAMPLES:
     1373            sage: S = sage.structure.parent.Set_PythonType(tuple)
     1374            sage: (1,2,3) in S
     1375            True
     1376            sage: () in S
     1377            True
     1378            sage: [1,2] in S
     1379            False
     1380        """
    11381381        return isinstance(x, self._type)
    11391382
    1140     def _latex_(self):
    1141         return self._repr_()
    1142 
    11431383    def _repr_(self):
     1384        """
     1385        EXAMPLES:
     1386            sage: sage.structure.parent.Set_PythonType(tuple)
     1387            Set of Python objects of type 'tuple'
     1388            sage: sage.structure.parent.Set_PythonType(Integer)
     1389            Set of Python objects of type 'sage.rings.integer.Integer'
     1390            sage: sage.structure.parent.Set_PythonType(Parent)
     1391            Set of Python objects of type 'sage.structure.parent.Parent'
     1392        """
    11441393        return "Set of Python objects of %s"%(str(self._type)[1:-1])
    11451394
    11461395    def object(self):
     1396        """
     1397        EXAMPLES:
     1398            sage: S = sage.structure.parent.Set_PythonType(tuple)
     1399            sage: S.object()
     1400            <type 'tuple'>
     1401        """
    11471402        return self._type
    11481403
    11491404    def cardinality(self):
     1405        """
     1406        EXAMPLES:
     1407            sage: S = sage.structure.parent.Set_PythonType(bool)
     1408            sage: S.cardinality()
     1409            2
     1410            sage: S = sage.structure.parent.Set_PythonType(int)
     1411            sage: S.cardinality()
     1412            4294967296                        # 32-bit
     1413            18446744073709551616              # 64-bit
     1414            sage: S = sage.structure.parent.Set_PythonType(float)
     1415            sage: S.cardinality()
     1416            18437736874454810627
     1417            sage: S = sage.structure.parent.Set_PythonType(long)
     1418            sage: S.cardinality()
     1419            +Infinity
     1420        """
     1421        from sage.rings.integer import Integer
     1422        two = Integer(2)
    11501423        if self._type is bool:
    1151             return 2
     1424            return two
     1425        elif self._type is int:
     1426            import sys
     1427            return two*sys.maxint + 2
     1428        elif self._type is float:
     1429            return 2 * two**52 * (two**11 - 1) + 3 # all NaN's are the same from Python's point of view
    11521430        else:
     1431            # probably
    11531432            import sage.rings.infinity
    11541433            return sage.rings.infinity.infinity
    11551434
    cdef bint _unregister_pair(x, y) except  
    11891468        pass
    11901469
    11911470
    1192 def coerce_graph(parents=None):
    1193     from sage.graphs.graph import DiGraph
    1194     G = DiGraph()
    1195     if parents is None:
    1196         parents = all_parents #.keys()
    1197     G.add_vertices(parents)
    1198     cdef Parent P
    1199     for P in parents:
    1200         for S, mor in P._coerce_from_hash.iteritems():
    1201             if S in parents:
    1202                 G.add_edge(P, S, mor)
    1203     return G
    1204 
    1205 
    1206 
    12071471empty_set = Set_generic()
    12081472
    12091473def normalize_names(ngens, names):
     1474    """
     1475    TESTS:
     1476        sage: sage.structure.parent.normalize_names(5, 'x')
     1477        ('x0', 'x1', 'x2', 'x3', 'x4')
     1478        sage: sage.structure.parent.normalize_names(2, ['x','y'])
     1479        ('x', 'y')
     1480    """
    12101481    return empty_set.normalize_names(ngens, names)
  • sage/structure/parent_old.pyx

    diff -r 8bced26ee39a -r 808aa5d3d7aa sage/structure/parent_old.pyx
    a b cdef class Parent(parent.Parent): 
    8989        # old
    9090        self._has_coerce_map_from = {}
    9191       
    92     def init_coerce(self, dummy=False):
    93         parent.Parent.init_coerce(self, dummy)
     92    cdef int init_coerce(self, bint warn=False) except -1:
     93        parent.Parent.init_coerce(self, warn)
    9494
    9595       
    9696    #################################################################################
    cdef class Parent(parent.Parent): 
    431431            raise NotImplementedError, "%s\nAlso, please make sure you have implemented has_coerce_map_from_impl or has_coerce_map_from_c_impl (or better _an_element_c_impl or _an_element_impl if possible) for %s"%(msg,self)
    432432        return True
    433433
    434     cpdef _an_element_impl(self):     # override this in Python
     434    def _an_element_impl(self):     # override this in Python
    435435        check_old_coerce(self)
    436436        return self._an_element_c_impl()
    437437