Changeset 6076:10d38f936bdc


Ignore:
Timestamp:
09/02/07 15:03:02 (6 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Message:

Add lots of doctests to integer.pyx

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/rings/integer.pyx

    r6075 r6076  
    306306 
    307307    def __reduce__(self): 
     308        """ 
     309        This is used when pickling integers. 
     310         
     311        EXAMPLES: 
     312            sage: n = 5 
     313            sage: t = n.__reduce__(); t 
     314            (<built-in function make_integer>, ('5',)) 
     315            sage: t[0](*t[1]) 
     316            5 
     317            sage: loads(dumps(n)) == n 
     318            True 
     319        """ 
    308320        # This single line below took me HOURS to figure out. 
    309321        # It is the *trick* needed to pickle pyrex extension types. 
     
    398410 
    399411    def __repr__(self): 
     412        """ 
     413        Return string representation of this integer. 
     414 
     415        EXAMPLES: 
     416            sage: n = -5; n.__repr__() 
     417            '-5' 
     418        """ 
    400419        return self.str() 
    401420 
    402421    def _latex_(self): 
     422        """ 
     423        Return latex representation of this integer.  This is just the 
     424        underlying string representation and nothing more.  This 
     425        is called by the latex function. 
     426 
     427        EXAMPLES: 
     428            sage: n = -5; n._latex_() 
     429            '-5' 
     430            sage: latex(n) 
     431            -5 
     432        """ 
    403433        return self.str() 
    404434 
     
    11871217 
    11881218    def rational_reconstruction(self, Integer m): 
     1219        """ 
     1220        Return the rational reconstruction of this integer modulo m, i.e., 
     1221        the unique (if it exists) rational number that reduces to self 
     1222        modulo m and whose numerator and denominator is bounded by 
     1223        sqrt(m/2). 
     1224 
     1225        EXAMPLES: 
     1226            sage: (3/7)%100 
     1227            29 
     1228            sage: (29).rational_reconstruction(100) 
     1229            3/7 
     1230        """ 
    11891231        import rational 
    11901232        return rational.pyrex_rational_reconstruction(self, m) 
     
    12291271 
    12301272    def __int__(self): 
     1273        """ 
     1274        Return the Python int (or long) corresponding to this SAGE integer. 
     1275 
     1276        EXAMPLES: 
     1277            sage: n = 920938; n 
     1278            920938 
     1279            sage: int(n) 
     1280            920938 
     1281            sage: type(n.__int__()) 
     1282            <type 'int'> 
     1283            sage: n = 99028390823409823904823098490238409823490820938; n 
     1284            99028390823409823904823098490238409823490820938 
     1285            sage: type(n.__int__()) 
     1286            <type 'long'> 
     1287        """ 
    12311288        # TODO -- this crashes on sage.math, since it is evidently written incorrectly. 
    12321289        #return mpz_get_pyintlong(self.value) 
     
    12341291 
    12351292    def __long__(self): 
     1293        """ 
     1294        Return long integer corresponding to this SAGE integer. 
     1295 
     1296        EXAMPLES: 
     1297            sage: n = 9023408290348092849023849820934820938490234290; n 
     1298            9023408290348092849023849820934820938490234290 
     1299            sage: long(n) 
     1300            9023408290348092849023849820934820938490234290L 
     1301            sage: n = 920938; n 
     1302            920938 
     1303            sage: long(n) 
     1304            920938L 
     1305            sage: n.__long__() 
     1306            920938L 
     1307        """ 
    12361308        return mpz_get_pylong(self.value) 
    12371309 
     
    12401312 
    12411313    def __hash__(self): 
     1314        """ 
     1315        Return the hash of this integer. 
     1316 
     1317        This agrees with the Python hash of the corresponding 
     1318        Python int or long. 
     1319 
     1320        EXAMPLES: 
     1321            sage: n = -920384; n.__hash__() 
     1322            -920384 
     1323            sage: hash(int(n)) 
     1324            -920384 
     1325            sage: n = -920390823904823094890238490238484; n.__hash__() 
     1326            -873977844 
     1327            sage: hash(long(n)) 
     1328            -873977844 
     1329        """ 
    12421330        return mpz_pythonhash(self.value) 
    12431331 
     
    12521340                 * 'kash' -- use KASH computer algebra system (requires 
    12531341                             the optional kash package be installed) 
     1342 
     1343        EXAMPLES: 
     1344            sage: n = 2^100 - 1; n.factor() 
     1345            3 * 5^3 * 11 * 31 * 41 * 101 * 251 * 601 * 1801 * 4051 * 8101 * 268501 
    12541346        """ 
    12551347        import sage.rings.integer_ring 
     
    25532645 
    25542646def LCM_list(v): 
     2647    """ 
     2648    Return the LCM of a list v of integers.  Element of v is 
     2649    converted to a SAGE integer if it isn't one already. 
     2650 
     2651    This function is used, e.g., by rings/arith.py 
     2652 
     2653    INPUT: 
     2654        v -- list or tuple 
     2655 
     2656    OUTPUT: 
     2657        integer 
     2658 
     2659    EXAMPLES: 
     2660        sage: from sage.rings.integer import LCM_list 
     2661        sage: w = LCM_list([3,9,30]); w 
     2662        90 
     2663        sage: type(w) 
     2664        <type 'sage.rings.integer.Integer'> 
     2665 
     2666    The inputs are converted to SAGE integers.  
     2667        sage: w = LCM_list([int(3), int(9), '30']); w 
     2668        90 
     2669        sage: type(w) 
     2670        <type 'sage.rings.integer.Integer'> 
     2671    """ 
    25552672    cdef int i, n 
    25562673    cdef mpz_t z 
     
    25902707 
    25912708def GCD_list(v): 
     2709    """ 
     2710    Return the GCD of a list v of integers.  Element of v is 
     2711    converted to a SAGE integer if it isn't one already. 
     2712 
     2713    This function is used, e.g., by rings/arith.py 
     2714 
     2715    INPUT: 
     2716        v -- list or tuple 
     2717 
     2718    OUTPUT: 
     2719        integer 
     2720 
     2721    EXAMPLES: 
     2722        sage: from sage.rings.integer import GCD_list 
     2723        sage: w = GCD_list([3,9,30]); w 
     2724        3 
     2725        sage: type(w) 
     2726        <type 'sage.rings.integer.Integer'> 
     2727 
     2728    The inputs are converted to SAGE integers.  
     2729        sage: w = GCD_list([int(3), int(9), '30']); w 
     2730        3 
     2731        sage: type(w) 
     2732        <type 'sage.rings.integer.Integer'> 
     2733    """ 
    25922734    cdef int i, n 
    25932735    cdef mpz_t z 
     
    26312773 
    26322774def make_integer(s): 
     2775    """ 
     2776    Create a SAGE integer from the base-32 Python *string* s. 
     2777    This is used in unpickling integers. 
     2778 
     2779    EXAMPLES: 
     2780        sage: from sage.rings.integer import make_integer 
     2781        sage: make_integer('-29') 
     2782        -73 
     2783        sage: make_integer(29) 
     2784        Traceback (most recent call last): 
     2785        ... 
     2786        TypeError: expected string or Unicode object, sage.rings.integer.Integer found 
     2787    """ 
    26332788    cdef Integer r 
    26342789    r = PY_NEW(Integer) 
     
    26362791    return r 
    26372792 
    2638 # TODO: where is this used? Never doctested. Should I call IntegerRing.random_element()? 
    2639 from random import randint 
    2640 def random_integer(min=-2, max=2): 
    2641     cdef Integer x 
    2642     cdef int _min, _max, r 
    2643     try: 
    2644         _min = min 
    2645         _max = max 
    2646         x = PY_NEW(Integer) 
    2647         r = randint() % (_max - _min + 1) + _min 
    2648         mpz_set_si(x.value, r) 
    2649         return x 
    2650     except OverflowError: 
    2651         return Integer(randint(min,max)) 
    2652  
    26532793 
    26542794############### INTEGER CREATION CODE ##################### 
    2655 ######## There is nothing to see here, move along   ####### 
    26562795     
    26572796cdef extern from *: 
     
    29463085 
    29473086def time_alloc_list(n): 
     3087    """ 
     3088    Allocate n a list of n SAGE integers using PY_NEW. 
     3089    (Used for timing purposes.) 
     3090 
     3091    EXAMPLES: 
     3092       sage: from sage.rings.integer import time_alloc_list 
     3093       sage: v = time_alloc_list(100) 
     3094    """ 
    29483095    cdef int i 
    29493096    l = [] 
     
    29543101 
    29553102def time_alloc(n): 
     3103    """ 
     3104    Time allocating n integers using PY_NEW. 
     3105    Used for timing purposes. 
     3106 
     3107    EXAMPLES: 
     3108       sage: from sage.rings.integer import time_alloc 
     3109       sage: time_alloc(100) 
     3110    """ 
    29563111    cdef int i 
    29573112    for i from 0 <= i < n: 
     
    29593114 
    29603115def pool_stats(): 
    2961     print "Used pool %s / %s times" % (use_pool, total_alloc) 
    2962     print "Pool contains %s / %s items" % (integer_pool_count, integer_pool_size) 
     3116    """ 
     3117    Returns information about the Integer object pool. 
     3118 
     3119    EXAMPLES: 
     3120        sage: from sage.rings.integer import pool_stats 
     3121        sage: pool_stats()            # random-ish output 
     3122        Used pool 0 / 0 times 
     3123        Pool contains 3 / 100 items 
     3124    """ 
     3125    return ["Used pool %s / %s times" % (use_pool, total_alloc),  
     3126            "Pool contains %s / %s items" % (integer_pool_count, integer_pool_size)] 
    29633127 
    29643128cdef integer(x): 
Note: See TracChangeset for help on using the changeset viewer.