Ticket #9937: 9937_pari_prec.patch

File 9937_pari_prec.patch, 77.4 KB (added by jdemeyer, 10 years ago)
  • sage/functions/transcendental.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1302183878 -7200
    # Node ID 8c7d9114a6e968375a654c640da3eb3259efd9b4
    # Parent  888bff9937d7c08c962c8a526a60700b1d9632ef
    Fix PARI real precision
    - fix the initialization of the precision
    - make set_real_precision() also affect the PARI library interface
    - add .precision_bits() method to return precision in bits
    - use a C "long" for the precision argument in Cython code
    
    diff --git a/sage/functions/transcendental.py b/sage/functions/transcendental.py
    a b  
    241241    return (s/2 + 1).gamma()   *    (s-1)   * (R.pi()**(-s/2))  *  s.zeta()
    242242
    243243
    244 ##     # Use PARI on complex nubmer
    245 ##     prec = s.prec()
    246 ##     s = pari.new_with_bits_prec(s, prec)
    247 ##     pi = pari.pi()
    248 ##     w = (s/2 + 1).gamma() * (s-1) * pi **(-s/2) * s.zeta()
    249 ##     z = w._sage_()
    250 ##     if z.prec() < prec:
    251 ##         raise RuntimeError, "Error computing zeta_symmetric(%s) -- precision loss."%s
    252 ##     return z
    253 
    254 
    255244#def pi_approx(prec=53):
    256245#    """
    257246#    Return pi computed to prec bits of precision.
  • sage/interfaces/maxima_abstract.py

    diff --git a/sage/interfaces/maxima_abstract.py b/sage/interfaces/maxima_abstract.py
    a b  
    10021002       
    10031003        EXAMPLES::
    10041004       
    1005             sage: zeta_ptsx = [ (pari(1/2 + i*I/10).zeta().real()).precision(1) for i in range (70,150)] 
    1006             sage: zeta_ptsy = [ (pari(1/2 + i*I/10).zeta().imag()).precision(1) for i in range (70,150)] 
     1005            sage: zeta_ptsx = [ pari(1/2 + i*I/10).zeta().real() for i in range(70,150)]
     1006            sage: zeta_ptsy = [ pari(1/2 + i*I/10).zeta().imag() for i in range(70,150)]
    10071007            sage: maxima.plot_list(zeta_ptsx, zeta_ptsy)         # not tested
    10081008            sage: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "zeta.eps"]'
    10091009            sage: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts)      # not tested
     
    10431043            sage: yy = [ i/10.0 for i in range (-10,10)]
    10441044            sage: x0 = [ 0 for i in range (-10,10)]
    10451045            sage: y0 = [ 0 for i in range (-10,10)]
    1046             sage: zeta_ptsx1 = [ (pari(1/2+i*I/10).zeta().real()).precision(1) for i in range (10)]
    1047             sage: zeta_ptsy1 = [ (pari(1/2+i*I/10).zeta().imag()).precision(1) for i in range (10)]
     1046            sage: zeta_ptsx1 = [ pari(1/2+i*I/10).zeta().real() for i in range(10)]
     1047            sage: zeta_ptsy1 = [ pari(1/2+i*I/10).zeta().imag() for i in range(10)]
    10481048            sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]])       # not tested
    1049             sage: zeta_ptsx1 = [ (pari(1/2+i*I/10).zeta().real()).precision(1) for i in range (10,150)]
    1050             sage: zeta_ptsy1 = [ (pari(1/2+i*I/10).zeta().imag()).precision(1) for i in range (10,150)]
     1049            sage: zeta_ptsx1 = [ pari(1/2+i*I/10).zeta().real() for i in range(10,150)]
     1050            sage: zeta_ptsy1 = [ pari(1/2+i*I/10).zeta().imag() for i in range(10,150)]
    10511051            sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]])      # not tested
    10521052            sage: opts='[gnuplot_preamble, "set nokey"]'
    10531053            sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]],opts)    # not tested
  • sage/libs/pari/all.py

    diff --git a/sage/libs/pari/all.py b/sage/libs/pari/all.py
    a b  
    77allocatemem = pari.allocatemem
    88
    99PariError = _gen.PariError
    10 
    11 from gen import (prec_dec_to_words, prec_dec_to_bits,
    12                  prec_bits_to_words, prec_bits_to_dec,
    13                  prec_words_to_bits, prec_words_to_dec)
  • sage/libs/pari/gen.pyx

    diff --git a/sage/libs/pari/gen.pyx b/sage/libs/pari/gen.pyx
    a b  
    1818- Jeroen Demeyer (2011-11-12): rewrite various conversion routines
    1919  (#11611, #11854, #11952)
    2020
     21- Jeroen Demeyer (2012-01-23): Overhaul real precision (#9937)
    2122
    2223EXAMPLES::
    2324
     
    4041    sage: type(1 + pari(1))
    4142    <type 'sage.libs.pari.gen.gen'>   
    4243
    43 GUIDE TO REAL PRECISION AND THE PARI LIBRARY
    44 
    45 The default real precision in communicating with the Pari library
    46 is the same as the default Sage real precision, which is 53 bits.
    47 Inexact Pari objects are therefore printed by default to 15 decimal
    48 digits (even if they are actually more precise).
    49 
    50 Default precision example (53 bits, 15 significant decimals)::
    51 
    52     sage: a = pari(1.23); a
    53     1.23000000000000
    54     sage: a.sin()
    55     0.942488801931698
    56 
    57 Example with custom precision of 200 bits (60 significant
    58 decimals)::
    59 
    60     sage: R = RealField(200)
    61     sage: a = pari(R(1.23)); a   # only 15 significant digits printed
    62     1.23000000000000
    63     sage: R(a)         # but the number is known to precision of 200 bits
    64     1.2300000000000000000000000000000000000000000000000000000000
    65     sage: a.sin()      # only 15 significant digits printed
    66     0.942488801931698 
    67     sage: R(a.sin())   # but the number is known to precision of 200 bits
    68     0.94248880193169751002382356538924454146128740562765030213504
    69 
    70 It is possible to change the number of printed decimals::
    71 
    72     sage: R = RealField(200)    # 200 bits of precision in computations
    73     sage: old_prec = pari.set_real_precision(60)  # 60 decimals printed
    74     sage: a = pari(R(1.23)); a
    75     1.23000000000000000000000000000000000000000000000000000000000
    76     sage: a.sin()
    77     0.942488801931697510023823565389244541461287405627650302135038
    78     sage: pari.set_real_precision(old_prec)  # restore the default printing behavior
    79     60
    80 
    81 Unless otherwise indicated in the docstring, most Pari functions
    82 that return inexact objects use the precision of their arguments to
    83 decide the precision of the computation. However, if some of these
    84 arguments happen to be exact numbers (integers, rationals, etc.),
    85 an optional parameter indicates the precision (in bits) to which
    86 these arguments should be converted before the computation. If this
    87 precision parameter is missing, the default precision of 53 bits is
    88 used. The following first converts 2 into a real with 53-bit
    89 precision::
    90 
    91     sage: R = RealField()
    92     sage: R(pari(2).sin())
    93     0.909297426825682
    94 
    95 We can ask for a better precision using the optional parameter::
    96 
    97     sage: R = RealField(150)
    98     sage: R(pari(2).sin(precision=150))
    99     0.90929742682568169539601986591174484270225497
    100 
    101 Warning regarding conversions Sage - Pari - Sage: Some care must be
    102 taken when juggling inexact types back and forth between Sage and
    103 Pari. In theory, calling p=pari(s) creates a Pari object p with the
    104 same precision as s; in practice, the Pari library's precision is
    105 word-based, so it will go up to the next word. For example, a
    106 default 53-bit Sage real s will be bumped up to 64 bits by adding
    107 bogus 11 bits. The function p.python() returns a Sage object with
    108 exactly the same precision as the Pari object p. So
    109 pari(s).python() is definitely not equal to s, since it has 64 bits
    110 of precision, including the bogus 11 bits. The correct way of
    111 avoiding this is to coerce pari(s).python() back into a domain with
    112 the right precision. This has to be done by the user (or by Sage
    113 functions that use Pari library functions in gen.pyx). For
    114 instance, if we want to use the Pari library to compute sqrt(pi)
    115 with a precision of 100 bits::
     44TESTS:
     45
     46Check that output from PARI's ``print`` command is actually seen by
     47Sage (ticket #9636)::
     48
     49    sage: pari('print("test")')
     50    test
     51
     52.. _pari_real_precision:
     53
     54Real precision in the PARI library
     55==================================
     56
     57The PARI library has a concept of "real precision", the precision for
     58real numbers (i.e. floating point numbers).  Since for example elliptic
     59curves over `\QQ` also contain some real data (the period lattice),
     60this real precision applies to a lot of things in PARI.
     61
     62The default real precision in communicating with the PARI library is 64
     63bits (precision in PARI is always a multiple of the machine word size,
     64so the Sage default of 53 bits gets rounded up to 64 bits).  PARI real
     65numbers are by default printed with 15 decimal digits, this is
     66consistent with the 53 bits default of Sage.
     67
     68You can change the default precision using
     69:meth:`pari.set_real_precision() <PariInstance.set_real_precision>`.
     70The argument is a precision given in decimal digits::
     71
     72    sage: pari.set_real_precision(5)
     73    15
     74    sage: pari('Pi')
     75    3.1416
     76    sage: pari.set_real_precision(60)
     77    5
     78    sage: pari('Pi')
     79    3.14159265358979323846264338327950288419716939937510582097494
     80
     81The real precision in PARI is used for exactly the following three things:
     82
     83#. It determines the precision of PARI objects created by parsing
     84   strings::
     85
     86    sage: _ = pari.set_real_precision(5)
     87    sage: pari('1.2')
     88    1.2000
     89    sage: pari('sqrt(2)')
     90    1.4142
     91    sage: _ = pari.set_real_precision(20)
     92    sage: pari('1.2')
     93    1.2000000000000000000
     94    sage: pari('sqrt(2)')
     95    1.4142135623730950488
     96
     97#. It determines the default precision for PARI library functions when
     98   an *exact* input yields a *inexact* result::
     99
     100    sage: pari2 = pari(2)
     101    sage: _ = pari.set_real_precision(5)
     102    sage: pari2.sqrt()
     103    1.4142
     104    sage: _ = pari.set_real_precision(20)
     105    sage: pari2.sqrt()
     106    1.4142135623730950488
     107
     108#. It is an *upper bound* on the number of decimal digits shown when
     109   printing a PARI real number.  In this example, we compute a precise
     110   value of `\sqrt{2}` but it is printed with less precision::
     111
     112    sage: _ = pari.set_real_precision(70)
     113    sage: sqrt2 = pari(2).sqrt()
     114    sage: _ = pari.set_real_precision(20)
     115    sage: sqrt2
     116    1.4142135623730950488
     117    sage: _ = pari.set_real_precision(1000)
     118    sage: sqrt2
     119    1.41421356237309504880168872420969807856967187537694807317667973799073247846210
     120
     121The PARI real precision is **not** used for:
     122
     123#. Functions with a inexact argument passed (regardless of whether the
     124   precision of the argument is smaller or larger than the default).
     125   In this case, the precision of the argument determines the precision
     126   of the result::
     127
     128    sage: _ = pari.set_real_precision(10)
     129    sage: pari_real_2 = pari("2.0")
     130    sage: pari_exact_2 = pari("2")
     131    sage: _ = pari.set_real_precision(50)
     132    sage: pari_real_2
     133    2.000000000000000000
     134    sage: pari_real_2.sqrt()
     135    1.4142135623730950488
     136    sage: pari_exact_2.sqrt()
     137    1.4142135623730950488016887242096980785696718753769
     138
     139#. Conversions between PARI and Sage using the Sage library,
     140   i.e. not using strings::
     141
     142    sage: _ = pari.set_real_precision(1000)
     143    sage: RR(pi)._pari_()
     144    3.141592653589793116
     145
     146   Remark that this value of `\pi` is not quite correct, since we
     147   converted ``RR(pi)`` which has 53 bits of precision to PARI, which
     148   rounds the precision up to 64 bits.  Compare with::
     149
     150    sage: RealField(64)(pi)._pari_()
     151    3.141592653589793239
     152
     153Almost all PARI functions where the real precision matters have an
     154optional argument ``precision``.  This can be used to override the
     155default real precision.  Note that the ``precision`` argument is always
     156a precision *in bits* as opposed to the precision set using
     157:meth:`pari.set_real_precision() <PariInstance.set_real_precision>`,
     158which is in *decimal digits*.
     159As mentioned above, this precision only matters when the input is exact
     160(otherwise the precision of the input is used instead)::
     161
     162    sage: _ = pari.set_real_precision(50)
     163    sage: pari(2).sqrt()
     164    1.4142135623730950488016887242096980785696718753769
     165    sage: pari(2).sqrt(precision=64)
     166    1.4142135623730950488
     167
     168We reset the precision back to the default::
     169
     170    sage: _ = pari.set_real_precision(15)
     171
     172Conversions between Sage and PARI
     173=================================
     174
     175Some care must be taken when juggling inexact types back and forth
     176between Sage and PARI. In theory, calling ``p = pari(s)`` creates a
     177PARI object ``p`` with the same precision as ``s``; in practice, the
     178PARI library's precision is word-based, so it will go up to the next
     179word. For example, a default 53-bit Sage real s will be bumped up to
     18064 bits by adding bogus 11 bits. The function ``p.python()`` returns a
     181Sage object with exactly the same precision as the PARI object ``p``.
     182So ``pari(s).python()`` is definitely not equal to ``s``, since it has
     18364 bits of precision, including the bogus 11 bits. The correct way of
     184avoiding this is to coerce ``pari(s).python()`` back into a domain with
     185the right precision. This has to be done by the user. For instance, if
     186we want to use the PARI library to compute `\sqrt{\pi}` with a precision
     187of 100 bits::
    116188
    117189    sage: R = RealField(100)
    118190    sage: s = R(pi); s
     
    129201    sage: R(x) == s.sqrt()
    130202    True
    131203
     204All these conversions are completely independent of the current PARI
     205real precision.
     206
    132207Elliptic curves and precision: If you are working with elliptic
    133208curves and want to compute with a precision other than the default
    13420953 bits, you should use the precision parameter of ellinit()::
     
    138213    sage: eta1 = e.elleta()[0]
    139214    sage: R(eta1)
    140215    3.6054636014326520859158205642077267748102690
    141 
    142 Number fields and precision: TODO
    143 
    144 TESTS:
    145 
    146 Check that output from PARI's print command is actually seen by
    147 Sage (ticket #9636)::
    148 
    149     sage: pari('print("test")')
    150     test
    151 
    152216"""
    153217
    154218#*****************************************************************************
    155 #       Copyright (C) 2006,2010 William Stein <wstein@gmail.com>
     219#       Copyright (C) 2006, 2010 William Stein <wstein@gmail.com>
    156220#       Copyright (C) ???? Justin Walker
    157221#       Copyright (C) ???? Gonzalo Tornaria
    158222#       Copyright (C) 2010 Robert Bradshaw <robertwb@math.washington.edu>
    159 #       Copyright (C) 2010,2011 Jeroen Demeyer <jdemeyer@cage.ugent.be>
     223#       Copyright (C) 2010--2012 Jeroen Demeyer <jdemeyer@cage.ugent.be>
    160224#
    161225#  Distributed under the terms of the GNU General Public License (GPL)
    162226#  as published by the Free Software Foundation; either version 2 of
     
    174238from sage.structure.parent cimport Parent
    175239from sage.misc.randstate cimport randstate, current_randstate
    176240
    177 from sage.misc.misc_c import is_64_bit
    178 
    179241include 'pari_err.pxi'
    180242include '../../ext/stdsage.pxi'
    181243
     
    187249# rather than silently corrupting memory.
    188250cdef long mpz_t_offset = 1000000000
    189251
    190 # The unique running Pari instance.
     252# The unique running PARI instance.
    191253cdef PariInstance pari_instance, P
    192254pari_instance = PariInstance(16000000, 500000)
    193 P = pari_instance   # shorthand notation
     255P = pari_instance     # Shorthand notation
     256pari = pari_instance  # A copy accessible from external pure Python code
    194257
    195258# PariInstance.__init__ must not create gen objects because their parent is not constructed yet
    196259sig_on()
     
    205268
    206269cdef pari_sp mytop
    207270
    208 # real precision in decimal digits: see documentation for
    209 # get_real_precision() and set_real_precision().  This variable is used
    210 # in gp to set the precision of input quantities (e.g. sqrt(2)), and for
    211 # determining the number of digits to be printed.  It is *not* used as
    212 # a "default precision" for internal computations, which always use
    213 # the actual precision of arguments together (where relevant) with a
    214 # "prec" parameter.  In ALL cases (for real computations) the prec
    215 # parameter is a WORD precision and NOT decimal precision.  Pari reals
    216 # with word precision w have bit precision (of the mantissa) equal to
    217 # 32*(w-2) or 64*(w-2).
    218 #
    219 # Hence the only relevance of this parameter in Sage is (1) for the
    220 # output format of components of objects of type
    221 # 'sage.libs.pari.gen.gen'; (2) for setting the precision of pari
    222 # variables created from strings (e.g. via sage: pari('1.2')).
    223 #
    224 # WARNING: Many pari library functions take a last parameter "prec"
    225 # which should be a words precision.  In many cases this is redundant
    226 # and is simply ignored.  In our wrapping of these functions we use
    227 # the variable prec here for convenience only.
    228 cdef unsigned long prec
     271# Default real precision in PARI words (including the 2 special
     272# codewords).  Used for PARI library calls when no explicit "prec="
     273# argument is given to the Sage wrapper.
     274# This is set using pari.set_real_precision().
     275cdef long pari_default_prec
    229276
    230277#################################################################
    231278# conversions between various real precision models
    232279#################################################################
    233280
    234 def prec_bits_to_dec(int prec_in_bits):
     281DEF log_10_base_2 = 3.321928094887362
     282
     283cpdef long prec_bits_to_dec(long prec_in_bits):
    235284    r"""
    236285    Convert from precision expressed in bits to precision expressed in
    237     decimal.
     286    decimal, rounded down.
    238287   
    239288    EXAMPLES::
    240289   
     
    251300        (224, 67),
    252301        (256, 77)]
    253302    """
    254     log_2 = 0.301029995663981
    255     return int(prec_in_bits*log_2)
    256 
    257 def prec_dec_to_bits(int prec_in_dec):
     303    return int(prec_in_bits/log_10_base_2)
     304
     305cpdef long prec_dec_to_bits(long prec_in_dec):
    258306    r"""
    259307    Convert from precision expressed in decimal to precision expressed
    260     in bits.
     308    in bits, rounded up.
    261309   
    262310    EXAMPLES::
    263311   
    264312        sage: import sage.libs.pari.gen as gen
    265313        sage: gen.prec_dec_to_bits(15)
    266         49
     314        50
    267315        sage: [(n,gen.prec_dec_to_bits(n)) for n in range(10,100,10)]
    268         [(10, 33),
    269         (20, 66),
    270         (30, 99),
    271         (40, 132),
    272         (50, 166),
    273         (60, 199),
    274         (70, 232),
    275         (80, 265),
    276         (90, 298)]
     316        [(10, 34),
     317        (20, 67),
     318        (30, 100),
     319        (40, 133),
     320        (50, 167),
     321        (60, 200),
     322        (70, 233),
     323        (80, 266),
     324        (90, 299)]
    277325    """
    278     log_10 = 3.32192809488736   
    279     return int(prec_in_dec*log_10)
    280 
    281 def prec_bits_to_words(int prec_in_bits=0):
     326    return int(prec_in_dec*log_10_base_2 + 1.0)
     327
     328cpdef inline long prec_bits_to_words(long prec_in_bits):
    282329    r"""
    283     Convert from precision expressed in bits to pari real precision
    284     expressed in words. Note: this rounds up to the nearest word,
    285     adjusts for the two codewords of a pari real, and is
    286     architecture-dependent.
     330    Convert from precision expressed in bits to PARI real precision
     331    expressed in words.
     332
     333    INPUT:
     334
     335    - ``prec_in_bits`` -- Either a precision given in bits or zero.
     336      Zero is interpreted as "use the default real precision".
     337   
     338    OUTPUT:
     339
     340    A PARI real precision in words (including the two codewords of a
     341    PARI ``t_REAL``).  The output is architecture-dependent.
    287342   
    288343    EXAMPLES::
    289344   
     
    291346        sage: gen.prec_bits_to_words(70)
    292347        5   # 32-bit
    293348        4   # 64-bit
    294    
    295     ::
    296    
    297349        sage: [(32*n,gen.prec_bits_to_words(32*n)) for n in range(1,9)]
    298350        [(32, 3), (64, 4), (96, 5), (128, 6), (160, 7), (192, 8), (224, 9), (256, 10)] # 32-bit
    299351        [(32, 3), (64, 3), (96, 4), (128, 4), (160, 5), (192, 5), (224, 6), (256, 6)] # 64-bit
     352
     353    With an argument of zero, return the default precision of 64 bits::
     354
     355        sage: gen.prec_bits_to_words(0)
     356        4   # 32-bit
     357        3   # 64-bit
     358        sage: gen.prec_words_to_bits(gen.prec_bits_to_words(0))
     359        64
    300360    """
    301361    if not prec_in_bits:
    302         return prec
    303     cdef int wordsize
    304     wordsize = BITS_IN_LONG
     362        return pari_default_prec
     363    cdef long wordsize = BITS_IN_LONG
    305364   
    306365    # increase prec_in_bits to the nearest multiple of wordsize
    307     cdef int padded_bits
     366    cdef long padded_bits
    308367    padded_bits = (prec_in_bits + wordsize - 1) & ~(wordsize - 1)
    309     return int(padded_bits/wordsize + 2)
    310 
    311 pbw = prec_bits_to_words
    312 
    313 def prec_words_to_bits(int prec_in_words):
     368    return padded_bits/wordsize + 2
     369
     370# pbw is a short name for prec_bits_to_words()
     371cdef inline long pbw(long prec_in_bits):
     372    return prec_bits_to_words(prec_in_bits)
     373
     374cpdef inline long prec_words_to_bits(long prec_in_words):
    314375    r"""
    315     Convert from pari real precision expressed in words to precision
     376    Convert from PARI real precision expressed in words to precision
    316377    expressed in bits. Note: this adjusts for the two codewords of a
    317378    pari real, and is architecture-dependent.
    318379   
     
    326387        [(3, 32), (4, 64), (5, 96), (6, 128), (7, 160), (8, 192), (9, 224)]  # 32-bit
    327388        [(3, 64), (4, 128), (5, 192), (6, 256), (7, 320), (8, 384), (9, 448)] # 64-bit
    328389    """
    329     # see user's guide to the pari library, page 10
    330     return int((prec_in_words - 2) * BITS_IN_LONG)
    331 
    332 def prec_dec_to_words(int prec_in_dec):
    333     r"""
    334     Convert from precision expressed in decimal to precision expressed
    335     in words. Note: this rounds up to the nearest word, adjusts for the
    336     two codewords of a pari real, and is architecture-dependent.
    337    
    338     EXAMPLES::
    339    
    340         sage: import sage.libs.pari.gen as gen
    341         sage: gen.prec_dec_to_words(38)
    342         6   # 32-bit
    343         4   # 64-bit
    344         sage: [(n,gen.prec_dec_to_words(n)) for n in range(10,90,10)]
    345         [(10, 4), (20, 5), (30, 6), (40, 7), (50, 8), (60, 9), (70, 10), (80, 11)] # 32-bit
    346         [(10, 3), (20, 4), (30, 4), (40, 5), (50, 5), (60, 6), (70, 6), (80, 7)] # 64-bit
    347     """
    348     return prec_bits_to_words(prec_dec_to_bits(prec_in_dec))
    349 
    350 def prec_words_to_dec(int prec_in_words):
    351     r"""
    352     Convert from precision expressed in words to precision expressed in
    353     decimal. Note: this adjusts for the two codewords of a pari real,
    354     and is architecture-dependent.
    355    
    356     EXAMPLES::
    357    
    358         sage: import sage.libs.pari.gen as gen
    359         sage: gen.prec_words_to_dec(5)
    360         28   # 32-bit
    361         57   # 64-bit
    362         sage: [(n,gen.prec_words_to_dec(n)) for n in range(3,10)]
    363         [(3, 9), (4, 19), (5, 28), (6, 38), (7, 48), (8, 57), (9, 67)] # 32-bit
    364         [(3, 19), (4, 38), (5, 57), (6, 77), (7, 96), (8, 115), (9, 134)] # 64-bit
    365     """
    366     return prec_bits_to_dec(prec_words_to_bits(prec_in_words))
    367 
    368 # Also a copy of PARI accessible from external pure python code.
    369 pari = pari_instance
     390    # See user's guide to the pari library, page 10
     391    return (prec_in_words - 2) * BITS_IN_LONG
    370392
    371393# temp variables
    372394cdef GEN t0,t1,t2,t3,t4
     
    673695            return P.new_gen(gmod(selfgen.g, othergen.g))
    674696        return sage.structure.element.bin_op(self, other, operator.mod)
    675697
    676     def __pow__(self, n, m):
    677         t0GEN(self)
    678         t1GEN(n)
    679         sig_on()
    680         # Note: the prec parameter here has no effect when t0,t1 are
    681         # real; the precision of the result is the minimum of the
    682         # precisions of t0 and t1.  In any case the 3rd parameter to
    683         # gpow should be a word-precision, not a decimal precision.
    684         return P.new_gen(gpow(t0, t1, prec))
     698    def __pow__(gen self, n, m):
     699        """
     700        Return ``self`` raised to the power ``n``.
     701       
     702        EXAMPLES::
     703
     704            sage: pari(-5) ^ 3
     705            -125
     706            sage: z6 = pari("Mod(x, x^2 - x + 1)")
     707            sage: z6 ^ 3
     708            Mod(-1, x^2 - x + 1)
     709            sage: pari(2.0) ^ 100
     710            1.26765060022823 E30
     711
     712        For exact inputs with inexact output, the default real
     713        precision (see :ref:`pari_real_precision`) is used::
     714
     715            sage: _ = pari.set_real_precision(50)
     716            sage: pari(2) ^ (1/2)
     717            1.4142135623730950488016887242096980785696718753769
     718            sage: _ = pari.set_real_precision(15)
     719            sage: pari(2) ^ (1/2)
     720            1.41421356237310
     721        """
     722
     723        t0GEN(n)
     724        sig_on()
     725        return P.new_gen(gpow(self.g, t0, pbw(0)))
    685726
    686727    def __neg__(gen self):
    687728        sig_on()
    688729        return P.new_gen(gneg(self.g))
    689730
    690     def __xor__(gen self, n):
    691         raise RuntimeError, "Use ** for exponentiation, not '^', which means xor\n"+\
    692               "in Python, and has the wrong precedence."
    693 
    694731    def __rshift__(gen self, long n):
    695732        sig_on()
    696733        return P.new_gen(gshift(self.g, -n))
     
    17871824
    17881825    sage = python
    17891826
    1790 #  This older version illustrates how irrelevant the real_precision
    1791 #  global variable is in the pari library.  The output of this
    1792 #  function when self is a t_REAL is completely independent of the
    1793 #  precision parameter.  The new version above has no precision
    1794 #  parameter at all: the caller can coerce into a lower-precision
    1795 #  RealField if desired (or even in to a higher precision one, but
    1796 #  that will just pad with 0 bits).
    1797 
    1798 #     def python(self, precision=0, bits_prec=None):
    1799 #         """
    1800 #         Return Python eval of self.
    1801 #         """
    1802 #         if not bits_prec is None:
    1803 #             precision = int(bits_prec * 3.4) + 1
    1804 #         import sage.libs.pari.gen_py
    1805 #         cdef long orig_prec
    1806 #         if precision:
    1807 #             orig_prec = P.get_real_precision()
    1808 #             P.set_real_precision(precision)
    1809 #             print "self.precision() = ",self.precision()
    1810 #             x = sage.libs.pari.gen_py.python(self)
    1811 #             print "x.prec() = ",x.prec()
    1812 #             P.set_real_precision(orig_prec)
    1813 #             return x
    1814 #         else:
    1815 #             return sage.libs.pari.gen_py.python(self)
    1816 
    18171827    _sage_ = _eval_ = python
    18181828   
    18191829    def __long__(gen self):
     
    25852595        sig_on()
    25862596        return P.new_gen(gtopolyrev(self.g, P.get_var(v)))
    25872597   
    2588     def Qfb(gen a, b, c, D=0):
     2598    def Qfb(gen a, b, c, D=0, long precision=0):
    25892599        """
    25902600        Qfb(a,b,c,D=0.): Returns the binary quadratic form
    25912601       
     
    26342644        """
    26352645        t0GEN(b); t1GEN(c); t2GEN(D)
    26362646        sig_on()       
    2637         return P.new_gen(Qfb0(a.g, t0, t1, t2, prec))
     2647        return P.new_gen(Qfb0(a.g, t0, t1, t2, pbw(precision)))
    26382648       
    26392649   
    26402650    def Ser(gen x, v=-1, long seriesprecision = 16):
     
    34343444        sig_on()
    34353445        return P.new_gen(gconj(x.g))
    34363446   
    3437     def conjvec(gen x):
     3447    def conjvec(gen x, long precision=0):
    34383448        """
    34393449        conjvec(x): Returns the vector of all conjugates of the algebraic
    34403450        number x. An algebraic number is a polynomial over Q modulo an
     
    34563466            [1.44224957030741, -0.721124785153704 + 1.24902476648341*I, -0.721124785153704 - 1.24902476648341*I]~
    34573467        """
    34583468        sig_on()
    3459         return P.new_gen(conjvec(x.g, prec))
     3469        return P.new_gen(conjvec(x.g, pbw(precision)))
    34603470   
    34613471    def denominator(gen x):
    34623472        """
     
    37723782   
    37733783    def precision(gen x, long n=-1):
    37743784        """
    3775         precision(x,n): Change the precision of x to be n, where n is a
    3776         C-integer). If n is omitted, output the real precision of x.
    3777        
    3778         INPUT:
    3779        
    3780        
    3781         -  ``x`` - gen
    3782        
    3783         -  ``n`` - (optional) int
    3784        
    3785        
    3786         OUTPUT: nothing or gen if n is omitted
    3787        
    3788         EXAMPLES:
    3789         """
     3785        Change the precision of `x` to be `n` decimal digits.
     3786        If `n` is omitted, return the real precision of `x`, measured
     3787        in PARI words.
     3788       
     3789        INPUT:
     3790       
     3791        -  ``x`` -- gen
     3792       
     3793        -  ``n`` -- (long) number of decimal digits
     3794       
     3795        OUTPUT: The precision (in words) of `x`, or 0 if `x` is exact.
     3796        When `n` is given, a new gen with precision lowered to `n` digits.
     3797
     3798        .. WARNING::
     3799
     3800            This method is deprecated. It is kept only for backwards
     3801            compatibility and will be removed in a future release of Sage.
     3802            Use the :meth:`precision_bits()` or
     3803            :meth:`precision_decimal()` methods instead to determine the
     3804            precision or :meth:`set_precision()` to change the precision.
     3805        """
     3806        # Deprecated in Trac #9937
     3807        from sage.misc.misc import deprecation
     3808        deprecation("This method is deprecated.  It will be removed in a future release of Sage.  Use the .precision_bits() or .precision_decimal() methods instead to determine the precision or .set_precision() to change the precision.")
    37903809        if n <= -1:
    37913810            return precision(x.g)
    37923811        sig_on()
    37933812        return P.new_gen(precision0(x.g, n))
    37943813   
     3814    def precision_bits(gen x):
     3815        """
     3816        Return the precision in bits of `x`.  If `x` is a scalar, this
     3817        is simply the precision of `x`.  Otherwise, return the lowest
     3818        precision among components of `x`.
     3819       
     3820        INPUT:
     3821       
     3822        -  ``x`` -- gen
     3823       
     3824        OUTPUT: The precision (in bits) of `x`, or 0 if `x` is exact.
     3825       
     3826        EXAMPLES::
     3827
     3828            sage: pari(RealField(100)(1)).precision_bits()
     3829            128
     3830            sage: pari.pi(precision=1024).precision_bits()
     3831            1024
     3832            sage: pari("exp(1e-30)").precision_bits()
     3833            192
     3834
     3835        Objects which do not involve floating point numbers have precision zero::
     3836
     3837            sage: pari(42).precision_bits()
     3838            0
     3839            sage: pari("x^2 + 5*x + 1").precision_bits()
     3840            0
     3841            sage: GF(125,'a').gen()._pari_().precision_bits()
     3842            0
     3843            sage: pari("x^-1 + x^2 + O(x^10)").precision_bits()
     3844            0
     3845
     3846        For non-scalars, return the lowest precision in the components::
     3847
     3848            sage: z = pari("1 + 1.0*I"); z  # Real part is exact, imaginary part is floating point
     3849            1 + 1.00000000000000*I
     3850            sage: z.precision_bits()
     3851            64
     3852            sage: pi64 = pari.pi(precision=64)
     3853            sage: pi128 = pari.pi(precision=128)
     3854            sage: pari([pi64, pi128]).precision_bits()
     3855            64
     3856            sage: pari([1, pi128]).precision_bits()
     3857            128
     3858        """
     3859        cdef long p
     3860        sig_on()
     3861        p = gprecision(x.g)
     3862        sig_off()
     3863        # When x is exact, return zero
     3864        if p <= 0:
     3865            return 0
     3866        else:
     3867            return prec_words_to_bits(p)
     3868
     3869    def precision_decimal(gen x):
     3870        """
     3871        Return the precision in decimal digits of `x`.  If `x` is a scalar,
     3872        this is simply the precision of `x`.  Otherwise, return the lowest
     3873        precision among components of `x`.
     3874
     3875        See also :meth:`precision_bits()`.
     3876       
     3877        INPUT:
     3878       
     3879        -  ``x`` -- gen
     3880       
     3881        OUTPUT: The precision (in digits) of `x`, or 0 if `x` is exact.
     3882       
     3883        EXAMPLES::
     3884
     3885            sage: pari(RealField(100)(1)).precision_decimal()
     3886            38
     3887            sage: pi.n(digits=1000)._pari_().precision_decimal()
     3888            1001
     3889
     3890        Objects which do not involve floating point numbers have precision zero::
     3891
     3892            sage: pari(42).precision_decimal()
     3893            0
     3894        """
     3895        return prec_bits_to_dec(x.precision_bits())
     3896
     3897    def set_precision(gen x, long n):
     3898        """
     3899        Change the precision of `x` to be `n`.  Depending on the type
     3900        of `x`, the precision refers to a number of decimal digits or
     3901        a number of terms (for p-adics and power series).  For
     3902        non-scalar types (e.g. polynomials, matrices), the precision is
     3903        changed component-wise.
     3904       
     3905        INPUT:
     3906       
     3907        - ``x`` -- gen
     3908       
     3909        - ``n`` -- (long) new precision
     3910       
     3911        OUTPUT: A new gen with precision changed to `n`.
     3912       
     3913        EXAMPLES:
     3914
     3915        We illustrate ``set_precision()`` for real numbers::
     3916
     3917            sage: precise_pi = pari.pi(precision=256)
     3918            sage: precise_pi.precision_bits()
     3919            256
     3920            sage: pi38 = precise_pi.set_precision(38)  # 38 decimal digits is 128 bits
     3921            sage: pi38.precision_bits()
     3922            128
     3923            sage: precise_pi.set_precision(0)
     3924            Traceback (most recent call last):
     3925            ...
     3926            ValueError: Precision must be at least 1
     3927
     3928        We can also increase the real precision::
     3929
     3930            sage: pi1000 = precise_pi.set_precision(1000)
     3931            sage: pi1000.precision_bits()
     3932            3328
     3933
     3934        For p-adics and series, the precision refers to the number of
     3935        significant terms::
     3936
     3937            sage: a = pari('5^2 + O(5^10)'); a
     3938            5^2 + O(5^10)
     3939            sage: a.set_precision(20)
     3940            5^2 + O(5^22)
     3941            sage: a.set_precision(1)
     3942            5^2 + O(5^3)
     3943
     3944            sage: a = pari('1/(x+x^2) + O(x^4)'); a
     3945            x^-1 - 1 + x - x^2 + x^3 + O(x^4)
     3946            sage: a.set_precision(2)
     3947            x^-1 - 1 + O(x)
     3948            sage: a.set_precision(10)
     3949            x^-1 - 1 + x - x^2 + x^3 + O(x^9)
     3950        """
     3951        if n <= 0:
     3952            raise ValueError("Precision must be at least 1")
     3953        sig_on()
     3954        return P.new_gen(gprec(x.g, n))
     3955   
    37953956    def random(gen N):
    37963957        r"""
    37973958        ``random(N=2^31)``: Return a pseudo-random integer
     
    42334394        Returns the absolute value of x (its modulus, if x is complex).
    42344395        Rational functions are not allowed. Contrary to most transcendental
    42354396        functions, an exact argument is not converted to a real number
    4236         before applying abs and an exact result is returned if possible.
     4397        before applying ``abs`` and an exact result is returned if possible.
    42374398       
    42384399        EXAMPLES::
    42394400       
     
    42434404       
    42444405        If x is a polynomial, returns -x if the leading coefficient is real
    42454406        and negative else returns x. For a power series, the constant
    4246         coefficient is considered instead.
    4247        
    4248         EXAMPLES::
     4407        coefficient is considered instead::
    42494408       
    42504409            sage: pari('x-1.2*x^2').abs()
    42514410            1.20000000000000*x^2 - x
    42524411        """
    42534412        sig_on()
    4254         # the prec parameter here has no effect
    4255         return P.new_gen(gabs(x.g, prec))
     4413        # prec seems to be ignored here
     4414        return P.new_gen(gabs(x.g, pbw(0)))
    42564415   
    42574416    def acos(gen x, long precision=0):
    42584417        r"""
     
    42804439        sig_on()
    42814440        return P.new_gen(gacos(x.g, pbw(precision)))
    42824441
    4283     def acosh(gen x, precision=0):
     4442    def acosh(gen x, long precision=0):
    42844443        r"""
    42854444        The principal branch of `\cosh^{-1}(x)`, so that
    42864445        `\Im(\mathrm{acosh}(x))` belongs to `[0,Pi]`. If
     
    43054464        sig_on()
    43064465        return P.new_gen(gach(x.g, pbw(precision)))
    43074466
    4308     def agm(gen x, y, precision=0):
     4467    def agm(gen x, y, long precision=0):
    43094468        r"""
    43104469        The arithmetic-geometric mean of x and y. In the case of complex or
    43114470        negative numbers, the principal square root is always chosen.
     
    43354494        sig_on()
    43364495        return P.new_gen(agm(x.g, t0, pbw(precision)))
    43374496
    4338     def arg(gen x, precision=0):
     4497    def arg(gen x, long precision=0):
    43394498        r"""
    43404499        arg(x): argument of x,such that `-\pi < \arg(x) \leq \pi`.
    43414500       
     
    43534512        sig_on()
    43544513        return P.new_gen(garg(x.g, pbw(precision)))
    43554514
    4356     def asin(gen x, precision=0):
     4515    def asin(gen x, long precision=0):
    43574516        r"""
    43584517        The principal branch of `\sin^{-1}(x)`, so that
    43594518        `\RR e(\mathrm{asin}(x))` belongs to `[-\pi/2,\pi/2]`. If
     
    43754534        sig_on()
    43764535        return P.new_gen(gasin(x.g, pbw(precision)))
    43774536
    4378     def asinh(gen x, precision=0):
     4537    def asinh(gen x, long precision=0):
    43794538        r"""
    43804539        The principal branch of `\sinh^{-1}(x)`, so that
    43814540        `\Im(\mathrm{asinh}(x))` belongs to `[-\pi/2,\pi/2]`.
     
    43964555        sig_on()
    43974556        return P.new_gen(gash(x.g, pbw(precision)))
    43984557
    4399     def atan(gen x, precision=0):
     4558    def atan(gen x, long precision=0):
    44004559        r"""
    44014560        The principal branch of `\tan^{-1}(x)`, so that
    44024561        `\RR e(\mathrm{atan}(x))` belongs to `]-\pi/2, \pi/2[`.
     
    44174576        sig_on()
    44184577        return P.new_gen(gatan(x.g, pbw(precision)))
    44194578
    4420     def atanh(gen x, precision=0):
     4579    def atanh(gen x, long precision=0):
    44214580        r"""
    44224581        The principal branch of `\tanh^{-1}(x)`, so that
    44234582        `\Im(\mathrm{atanh}(x))` belongs to `]-\pi/2,\pi/2]`. If
     
    44564615        sig_on()
    44574616        return P.new_gen(bernfrac(x))
    44584617
    4459     def bernreal(gen x):
     4618    def bernreal(gen x, long precision=0):
    44604619        r"""
    44614620        The Bernoulli number `B_x`, as for the function bernfrac,
    44624621        but `B_x` is returned as a real number (with the current
     
    44684627            54.9711779448622
    44694628        """
    44704629        sig_on()
    4471         # the argument prec has no effect
    4472         return P.new_gen(bernreal(x, prec))
     4630        return P.new_gen(bernreal(x, pbw(precision)))
    44734631
    44744632    def bernvec(gen x):
    44754633        r"""
     
    44924650        sig_on()
    44934651        return P.new_gen(bernvec(x))
    44944652
    4495     def besselh1(gen nu, x, precision=0):
     4653    def besselh1(gen nu, x, long precision=0):
    44964654        r"""
    44974655        The `H^1`-Bessel function of index `\nu` and
    44984656        argument `x`.
     
    45124670        sig_on()
    45134671        return P.new_gen(hbessel1(nu.g, t0, pbw(precision)))
    45144672
    4515     def besselh2(gen nu, x, precision=0):
     4673    def besselh2(gen nu, x, long precision=0):
    45164674        r"""
    45174675        The `H^2`-Bessel function of index `\nu` and
    45184676        argument `x`.
     
    45324690        sig_on()
    45334691        return P.new_gen(hbessel2(nu.g, t0, pbw(precision)))
    45344692
    4535     def besselj(gen nu, x, precision=0):
     4693    def besselj(gen nu, x, long precision=0):
    45364694        r"""
    45374695        Bessel J function (Bessel function of the first kind), with index
    45384696        `\nu` and argument `x`. If `x` converts to
     
    45554713        sig_on()
    45564714        return P.new_gen(jbessel(nu.g, t0, pbw(precision)))
    45574715
    4558     def besseljh(gen nu, x, precision=0):
     4716    def besseljh(gen nu, x, long precision=0):
    45594717        """
    45604718        J-Bessel function of half integral index (Spherical Bessel
    45614719        function of the first kind). More precisely, besseljh(n,x) computes
     
    45804738        sig_on()
    45814739        return P.new_gen(jbesselh(nu.g, t0, pbw(precision)))
    45824740
    4583     def besseli(gen nu, x, precision=0):
     4741    def besseli(gen nu, x, long precision=0):
    45844742        r"""
    45854743        Bessel I function (Bessel function of the second kind), with index
    45864744        `\nu` and argument `x`. If `x` converts to
     
    46064764        sig_on()
    46074765        return P.new_gen(ibessel(nu.g, t0, pbw(precision)))
    46084766
    4609     def besselk(gen nu, x, long flag=0, precision=0):
     4767    def besselk(gen nu, x, long flag=0, long precision=0):
    46104768        """
    46114769        nu.besselk(x, flag=0): K-Bessel function (modified Bessel function
    46124770        of the second kind) of index nu, which can be complex, and argument
     
    46494807        sig_on()
    46504808        return P.new_gen(kbessel(nu.g, t0, pbw(precision)))
    46514809
    4652     def besseln(gen nu, x, precision=0):
     4810    def besseln(gen nu, x, long precision=0):
    46534811        """
    46544812        nu.besseln(x): Bessel N function (Spherical Bessel function of the
    46554813        second kind) of index nu and argument x.
     
    46704828        sig_on()
    46714829        return P.new_gen(nbessel(nu.g, t0, pbw(precision)))
    46724830
    4673     def cos(gen x, precision=0):
     4831    def cos(gen x, long precision=0):
    46744832        """
    46754833        The cosine function.
    46764834       
     
    46924850        sig_on()
    46934851        return P.new_gen(gcos(x.g, pbw(precision)))
    46944852
    4695     def cosh(gen x, precision=0):
     4853    def cosh(gen x, long precision=0):
    46964854        """
    46974855        The hyperbolic cosine function.
    46984856       
     
    47144872        sig_on()
    47154873        return P.new_gen(gch(x.g, pbw(precision)))
    47164874
    4717     def cotan(gen x, precision=0):
     4875    def cotan(gen x, long precision=0):
    47184876        """
    47194877        The cotangent of x.
    47204878       
     
    47414899        sig_on()
    47424900        return P.new_gen(gcotan(x.g, pbw(precision)))
    47434901
    4744     def dilog(gen x, precision=0):
     4902    def dilog(gen x, long precision=0):
    47454903        r"""
    47464904        The principal branch of the dilogarithm of `x`, i.e. the
    47474905        analytic continuation of the power series
     
    47634921        sig_on()
    47644922        return P.new_gen(dilog(x.g, pbw(precision)))
    47654923
    4766     def eint1(gen x, long n=0, precision=0):
     4924    def eint1(gen x, long n=0, long precision=0):
    47674925        r"""
    47684926        x.eint1(n): exponential integral E1(x):
    47694927       
     
    47934951        else:
    47944952            return P.new_gen(veceint1(x.g, stoi(n), pbw(precision)))
    47954953
    4796     def erfc(gen x, precision=0):
     4954    def erfc(gen x, long precision=0):
    47974955        r"""
    47984956        Return the complementary error function:
    47994957       
     
    48164974        sig_on()
    48174975        return P.new_gen(gerfc(x.g, pbw(precision)))
    48184976
    4819     def eta(gen x, flag=0, precision=0):
     4977    def eta(gen x, flag=0, long precision=0):
    48204978        r"""
    48214979        x.eta(flag=0): if flag=0, `\eta` function without the
    48224980        `q^{1/24}`; otherwise `\eta` of the complex number
     
    48465004            return P.new_gen(trueeta(x.g, pbw(precision)))
    48475005        return P.new_gen(eta(x.g, pbw(precision)))
    48485006
    4849     def exp(gen self, precision=0):
     5007    def exp(gen self, long precision=0):
    48505008        """
    48515009        x.exp(): exponential of x.
    48525010       
     
    48675025        sig_on()       
    48685026        return P.new_gen(gexp(self.g, pbw(precision)))
    48695027
    4870     def gamma(gen s, precision=0):
     5028    def gamma(gen s, long precision=0):
    48715029        """
    48725030        s.gamma(precision): Gamma function at s.
    48735031       
     
    48965054        sig_on()
    48975055        return P.new_gen(ggamma(s.g, pbw(precision)))
    48985056       
    4899     def gammah(gen s, precision=0):
     5057    def gammah(gen s, long precision=0):
    49005058        """
    49015059        s.gammah(): Gamma function evaluated at the argument x+1/2.
    49025060       
     
    49185076        sig_on()
    49195077        return P.new_gen(ggamd(s.g, pbw(precision)))
    49205078
    4921     def hyperu(gen a, b, x, precision=0):
     5079    def hyperu(gen a, b, x, long precision=0):
    49225080        r"""
    49235081        a.hyperu(b,x): U-confluent hypergeometric function.
    49245082       
     
    49385096        sig_on()
    49395097        return P.new_gen(hyperu(a.g, t0, t1, pbw(precision)))
    49405098
    4941     def incgam(gen s, x, y=None, precision=0):
     5099    def incgam(gen s, x, y=None, long precision=0):
    49425100        r"""
    49435101        s.incgam(x, y, precision): incomplete gamma function. y is optional
    49445102        and is the precomputed value of gamma(s).
     
    49625120            t1GEN(y)
    49635121            return P.new_gen(incgam0(s.g, t0, t1, pbw(precision)))
    49645122
    4965     def incgamc(gen s, x, precision=0):
     5123    def incgamc(gen s, x, long precision=0):
    49665124        r"""
    49675125        s.incgamc(x): complementary incomplete gamma function.
    49685126       
     
    49885146        sig_on()
    49895147        return P.new_gen(incgamc(s.g, t0, pbw(precision)))
    49905148   
    4991     def log(gen x, precision=0):
     5149    def log(gen x, long precision=0):
    49925150        r"""
    49935151        x.log(): natural logarithm of x.
    49945152       
     
    50315189        sig_on()       
    50325190        return P.new_gen(glog(x.g, pbw(precision)))
    50335191
    5034     def lngamma(gen x, precision=0):
     5192    def lngamma(gen x, long precision=0):
    50355193        r"""
    50365194        This method is deprecated, please use :meth:`.log_gamma` instead.
    50375195
     
    50475205        deprecation("The method lngamma() is deprecated. Use log_gamma() instead.")
    50485206        return x.log_gamma(precision)
    50495207
    5050     def log_gamma(gen x, precision=0):
     5208    def log_gamma(gen x, long precision=0):
    50515209        r"""
    50525210        Logarithm of the gamma function of x.
    50535211       
     
    50735231        sig_on()
    50745232        return P.new_gen(glngamma(x.g, pbw(precision)))
    50755233
    5076     def polylog(gen x, long m, flag=0, precision=0):
     5234    def polylog(gen x, long m, flag=0, long precision=0):
    50775235        """
    50785236        x.polylog(m,flag=0): m-th polylogarithm of x. flag is optional, and
    50795237        can be 0: default, 1: D_m -modified m-th polylog of x, 2:
     
    51015259        sig_on()
    51025260        return P.new_gen(polylog0(m, x.g, flag, pbw(precision)))
    51035261
    5104     def psi(gen x, precision=0):
     5262    def psi(gen x, long precision=0):
    51055263        r"""
    51065264        x.psi(): psi-function at x.
    51075265       
     
    51215279        sig_on()
    51225280        return P.new_gen(gpsi(x.g, pbw(precision)))
    51235281
    5124     def sin(gen x, precision=0):
     5282    def sin(gen x, long precision=0):
    51255283        """
    51265284        x.sin(): The sine of x.
    51275285       
     
    51415299        sig_on()
    51425300        return P.new_gen(gsin(x.g, pbw(precision)))
    51435301
    5144     def sinh(gen x, precision=0):
     5302    def sinh(gen x, long precision=0):
    51455303        """
    51465304        The hyperbolic sine function.
    51475305       
     
    51885346        return P.new_gen(gsqr(x.g))
    51895347   
    51905348
    5191     def sqrt(gen x, precision=0):
     5349    def sqrt(gen x, long precision=0):
    51925350        """
    51935351        x.sqrt(precision): The square root of x.
    51945352       
     
    52055363        sig_on()
    52065364        return P.new_gen(gsqrt(x.g, pbw(precision)))
    52075365
    5208     def sqrtn(gen x, n, precision=0):
     5366    def sqrtn(gen x, n, long precision=0):
    52095367        r"""
    52105368        x.sqrtn(n): return the principal branch of the n-th root of x,
    52115369        i.e., the one such that
     
    52655423        ans = P.new_gen_noclear(gsqrtn(x.g, t0, &zetan, pbw(precision)))
    52665424        return ans, P.new_gen(zetan)
    52675425
    5268     def tan(gen x, precision=0):
     5426    def tan(gen x, long precision=0):
    52695427        """
    52705428        x.tan() - tangent of x
    52715429       
     
    52855443        sig_on()
    52865444        return P.new_gen(gtan(x.g, pbw(precision)))
    52875445
    5288     def tanh(gen x, precision=0):
     5446    def tanh(gen x, long precision=0):
    52895447        """
    52905448        x.tanh() - hyperbolic tangent of x
    52915449       
     
    53255483        sig_on()
    53265484        return P.new_gen(teich(x.g))
    53275485
    5328     def theta(gen q, z, precision=0):
     5486    def theta(gen q, z, long precision=0):
    53295487        """
    53305488        q.theta(z): Jacobi sine theta-function.
    53315489       
     
    53445502        sig_on()
    53455503        return P.new_gen(theta(q.g, t0, pbw(precision)))
    53465504
    5347     def thetanullk(gen q, long k, precision=0):
     5505    def thetanullk(gen q, long k, long precision=0):
    53485506        """
    53495507        q.thetanullk(k): return the k-th derivative at z=0 of theta(q,z).
    53505508       
     
    53615519        sig_on()
    53625520        return P.new_gen(thetanullk(q.g, k, pbw(precision)))
    53635521
    5364     def weber(gen x, flag=0, precision=0):
     5522    def weber(gen x, flag=0, long precision=0):
    53655523        r"""
    53665524        x.weber(flag=0): One of Weber's f functions of x. flag is optional,
    53675525        and can be 0: default, function
     
    53925550        sig_on()
    53935551        return P.new_gen(weber0(x.g, flag, pbw(precision)))
    53945552
    5395     def zeta(gen s, precision=0):
     5553    def zeta(gen s, long precision=0):
    53965554        """
    53975555        zeta(s): zeta function at s with s a complex or a p-adic number.
    53985556       
     
    56655823    # 5: Elliptic curve functions
    56665824    ##################################################
    56675825
    5668     def ellinit(self, int flag=0, precision=0):
     5826    def ellinit(self, int flag=0, long precision=0):
    56695827        """
    56705828        Return the Pari elliptic curve object with Weierstrass coefficients
    56715829        given by self, a list with 5 elements.
     
    60146172            return self.new_gen(g)
    60156173
    60166174
    6017     def ellbil(self, z0, z1):
     6175    def ellbil(self, z0, z1, long precision=0):
    60186176        """
    60196177        e.ellbil(z0, z1): return the value of the canonical bilinear form
    60206178        on z0 and z1.
     
    60366194        """
    60376195        t0GEN(z0); t1GEN(z1)
    60386196        sig_on()
    6039         # the prec argument has no effect
    6040         return self.new_gen(bilhell(self.g, t0, t1, prec))
     6197        return self.new_gen(bilhell(self.g, t0, t1, pbw(precision)))
    60416198
    60426199    def ellchangecurve(self, ch):
    60436200        """
     
    60706227        sig_on()
    60716228        return self.new_gen(ellchangecurve(self.g, t0))
    60726229
    6073     def elleta(self):
     6230    def elleta(self, long precision=0):
    60746231        """
    60756232        e.elleta(): return the vector [eta1,eta2] of quasi-periods
    60766233        associated with the period lattice e.omega() of the elliptic curve
     
    60896246            True
    60906247        """
    60916248        sig_on()
    6092         # the prec argument has no effect
    6093         return self.new_gen(elleta(self.g, prec))
    6094 
    6095     def ellheight(self, a, flag=2, precision=0):
     6249        return self.new_gen(elleta(self.g, pbw(precision)))
     6250
     6251    def ellheight(self, a, flag=2, long precision=0):
    60966252        """
    60976253        e.ellheight(a, flag=2): return the global Neron-Tate height of the
    60986254        point a on the elliptic curve e.
     
    61396295        sig_on()
    61406296        return self.new_gen(ellheight0(self.g, t0, flag, pbw(precision)))
    61416297
    6142     def ellheightmatrix(self, x):
     6298    def ellheightmatrix(self, x, long precision=0):
    61436299        """
    61446300        e.ellheightmatrix(x): return the height matrix for the vector x of
    61456301        points on the elliptic curve e.
     
    61666322        t0GEN(x)
    61676323        sig_on()
    61686324        # the argument prec has no effect
    6169         return self.new_gen(mathell(self.g, t0, prec))
     6325        return self.new_gen(mathell(self.g, t0, pbw(precision)))
    61706326
    61716327    def ellisoncurve(self, x):
    61726328        """
     
    63516507        sig_on()
    63526508        return self.new_gen(elllocalred(self.g, t0))
    63536509
    6354     def elllseries(self, s, A=1):
     6510    def elllseries(self, s, A=1, long precision=0):
    63556511        """
    63566512        e.elllseries(s, A=1): return the value of the `L`-series of
    63576513        the elliptic curve e at the complex number s.
     
    63916547        """
    63926548        t0GEN(s); t1GEN(A)
    63936549        sig_on()
    6394         # the argument prec has no effect
    6395         return self.new_gen(elllseries(self.g, t0, t1, prec))
     6550        return self.new_gen(elllseries(self.g, t0, t1, pbw(precision)))
    63966551
    63976552    def ellminimalmodel(self):
    63986553        """
     
    64636618        sig_on()
    64646619        return self.new_gen(orderell(self.g, t0))
    64656620   
    6466     def ellordinate(self, x):
     6621    def ellordinate(self, x, long precision=0):
    64676622        """
    64686623        e.ellordinate(x): return the `y`-coordinates of the points
    64696624        on the elliptic curve e having x as `x`-coordinate.
     
    64926647        """
    64936648        t0GEN(x)
    64946649        sig_on()
    6495         # the prec argument has no effect
    6496         return self.new_gen(ellordinate(self.g, t0, prec))
     6650        return self.new_gen(ellordinate(self.g, t0, pbw(precision)))
    64976651
    64986652    def ellpointtoz(self, P, long precision=0):
    64996653        """
     
    66216775        sig_off()
    66226776        return rootno
    66236777
    6624     def ellsigma(self, z, flag=0):
     6778    def ellsigma(self, z, flag=0, long precision=0):
    66256779        """
    66266780        e.ellsigma(z, flag=0): return the value at the complex point z of
    66276781        the Weierstrass `\sigma` function associated to the
     
    66366790        """
    66376791        t0GEN(z)
    66386792        sig_on()
    6639         # the prec argument has no effect
    6640         return self.new_gen(ellsigma(self.g, t0, flag, prec))
     6793        return self.new_gen(ellsigma(self.g, t0, flag, pbw(precision)))
    66416794
    66426795    def ellsub(self, z0, z1):
    66436796        """
     
    67146867        sig_on()
    67156868        return self.new_gen(elltors0(self.g, flag))
    67166869
    6717     def ellzeta(self, z):
     6870    def ellzeta(self, z, long precision=0):
    67186871        """
    67196872        e.ellzeta(z): return the value at the complex point z of the
    67206873        Weierstrass `\zeta` function associated with the elliptic
     
    67466899        """
    67476900        t0GEN(z)
    67486901        sig_on()
    6749         # the prec argument has no effect
    6750         return self.new_gen(ellzeta(self.g, t0, prec))
    6751 
    6752     def ellztopoint(self, z):
     6902        return self.new_gen(ellzeta(self.g, t0, pbw(precision)))
     6903
     6904    def ellztopoint(self, z, long precision=0):
    67536905        """
    67546906        e.ellztopoint(z): return the point on the elliptic curve e
    67556907        corresponding to the complex number z, under the usual complex
     
    67806932            [0]
    67816933        """
    67826934        t0GEN(z)
    6783         try:
    6784             dprec = prec_words_to_dec(z.precision())
    6785         except AttributeError:
    6786             dprec = prec
    6787         sig_on()
    6788         # the prec argument has no effect
    6789         return self.new_gen(pointell(self.g, t0, dprec))
     6935        sig_on()
     6936        return self.new_gen(pointell(self.g, t0, pbw(precision)))
    67906937
    67916938    def omega(self):
    67926939        """
     
    68296976        """
    68306977        return self[12]
    68316978
    6832     def ellj(self):
    6833         try:
    6834             dprec = prec_words_to_dec(self.precision())
    6835         except AttributeError:
    6836             dprec = prec
    6837         sig_on()
    6838         return P.new_gen(jell(self.g, dprec))
     6979    def ellj(self, long precision=0):
     6980        sig_on()
     6981        return P.new_gen(jell(self.g, pbw(precision)))
    68396982
    68406983
    68416984    ###########################################
     
    68637006        sig_off()
    68647007        return n
    68657008   
    6866     def bnfinit(self, long flag=0, tech=None):
     7009    def bnfinit(self, long flag=0, tech=None, long precision=0):
    68677010        if tech is None:
    68687011            sig_on()
    6869             return P.new_gen(bnfinit0(self.g, flag, <GEN>0, prec))
     7012            return P.new_gen(bnfinit0(self.g, flag, <GEN>0, pbw(precision)))
    68707013        else:
    68717014            t0GEN(tech)
    68727015            sig_on()
    6873             return P.new_gen(bnfinit0(self.g, flag, t0, prec))
     7016            return P.new_gen(bnfinit0(self.g, flag, t0, pbw(precision)))
    68747017   
    68757018    def bnfisintnorm(self, x):
    68767019        t0GEN(x)
     
    78317974        sig_on()
    78327975        return self.new_gen(reduceddiscsmith(self.g))
    78337976
    7834     def polgalois(self):
     7977    def polgalois(self, long precision=0):
    78357978        """
    78367979        f.polgalois(): Galois group of the polynomial f
    78377980        """
    78387981        sig_on()
    7839         return self.new_gen(polgalois(self.g, prec))
     7982        return self.new_gen(polgalois(self.g, pbw(precision)))
    78407983
    78417984    def nfgaloisconj(self, long flag=0, denom=None, long precision=0):
    78427985        r"""
     
    79418084        sig_on()
    79428085        return self.new_gen(polresultant0(self.g, t0, self.get_var(var), flag))
    79438086
    7944     def polroots(self, flag=0, precision=0):
     8087    def polroots(self, flag=0, long precision=0):
    79458088        """
    79468089        polroots(x,flag=0): complex roots of the polynomial x. flag is
    79478090        optional, and can be 0: default, uses Schonhage's method modified
     
    80188161        sig_on()
    80198162        return self.new_gen(recip(self.g))
    80208163   
    8021     def thueinit(self, flag=0):
    8022         sig_on()
    8023         return self.new_gen(thueinit(self.g, flag, prec))
     8164    def thueinit(self, flag=0, long precision=0):
     8165        sig_on()
     8166        return self.new_gen(thueinit(self.g, flag, pbw(precision)))
    80248167
    80258168
    80268169    def rnfisnorminit(self, polrel, long flag=2):
     
    88468989        sig_on()
    88478990        return self.new_gen(listput(self.g, t0, n))
    88488991
    8849 
    8850 
    8851     def elleisnum(self, long k, int flag=0):
     8992    def elleisnum(self, long k, int flag=0, long precision=0):
    88528993        """
    88538994        om.elleisnum(k, flag=0): om=[om1,om2] being a 2-component vector
    88548995        giving a basis of a lattice L and k an even positive integer,
     
    88879028            2.15314248576078 E50
    88889029        """
    88899030        sig_on()
    8890         # the argument prec has no effect
    8891         return self.new_gen(elleisnum(self.g, k, flag, prec))
    8892 
    8893     def ellwp(gen self, z='z', long n=20, long flag=0):
     9031        return self.new_gen(elleisnum(self.g, k, flag, pbw(precision)))
     9032
     9033    def ellwp(gen self, z='z', long n=20, long flag=0, long precision=0):
    88949034        """
    88959035        Return the value or the series expansion of the Weierstrass
    88969036        `P`-function at `z` on the lattice `self` (or the lattice
     
    89579097        """
    89589098        t0GEN(z)
    89599099        sig_on()
    8960         cdef long dprec
    8961         dprec = gprecision(t0)
    8962         if dprec:
    8963             dprec = prec_words_to_dec(dprec)
    8964         else:
    8965             dprec = prec
    8966         return self.new_gen(ellwp0(self.g, t0, flag, n+2, dprec))
     9100        return self.new_gen(ellwp0(self.g, t0, flag, n+2, pbw(precision)))
    89679101
    89689102    def ellchangepoint(self, y):
    89699103        """
     
    90619195       
    90629196        INPUT:
    90639197       
    9064        
    90659198        -  ``size`` - long, the number of bytes for the initial
    90669199           PARI stack (see note below)
    90679200       
    90689201        -  ``maxprime`` - unsigned long, upper limit on a
    90699202           precomputed prime number table (default: 500000)
    90709203       
    9071        
    90729204        .. note::
    90739205       
    9074            In py_pari, the PARI stack is different than in gp or the
    9075            PARI C library. In Python, instead of the PARI stack
    9076            holding the results of all computations, it *only* holds
    9077            the results of an individual computation. Each time a new
    9078            Python/PARI object is computed, it it copied to its own
    9079            space in the Python heap, and the memory it occupied on the
    9080            PARI stack is freed. Thus it is not necessary to make the
    9081            stack very large. Also, unlike in PARI, if the stack does
    9082            overflow, in most cases the PARI stack is automatically
    9083            increased and the relevant step of the computation rerun.
     9206           In Sage, the PARI stack behaves differently from the PARI
     9207           library. In Sage, the PARI stack is only used for temporary
     9208           values used during computations. At the end of a
     9209           computation, the result is copied to its own space in the
     9210           Python heap, and the memory it occupied on the PARI stack is
     9211           freed. This happens using :meth:`new_gen()`. Thus it is
     9212           probably not necessary to make the stack very large. Also,
     9213           unlike in PARI, if the stack does overflow, in most cases
     9214           the PARI stack is automatically increased and the relevant
     9215           step of the computation rerun.
    90849216       
    90859217           This design obviously involves some performance penalties
    90869218           over the way PARI works, but it scales much better and is
     
    90969228        if bot:
    90979229            return  # pari already initialized.
    90989230       
    9099         global num_primes, avma, top, bot, prec
     9231        global num_primes, avma, top, bot, pari_default_prec
    91009232
    91019233        # The size here doesn't really matter, because we will allocate
    91029234        # our own stack anyway. We ask PARI not to set up signal handlers.
     
    91109242        pari_free(<void*>bot); bot = 0
    91119243        init_stack(size)
    91129244
     9245        # Set real precision to 15 decimal digits
    91139246        GP_DATA.fmt.prettyp = 0
    9114 
    9115         # how do I get the following to work? seems to be a circular import
    9116         #from sage.rings.real_mpfr import RealField
    9117         #prec_bits = RealField().prec()
    9118         prec = prec_bits_to_words(53)
    9119         GP_DATA.fmt.sigd = prec_bits_to_dec(53)
     9247        sd_realprecision("15", d_SILENT)
     9248        pari_default_prec = prec_bits_to_words(prec_dec_to_bits(15))
    91209249
    91219250        # Set printing functions
    91229251        global pariOut
     
    91839312        _x = t0heap[i]
    91849313        return _x.g
    91859314       
    9186         # TODO: Refactor code out of __call__ so it...
    9187        
    9188         s = str(x)
    9189         cdef GEN g
    9190         sig_on()
    9191         g = gp_read_str(s)
    9192         sig_off()
    9193         return g
    9194        
    9195 
    91969315    def set_real_precision(self, long n):
    91979316        """
    9198         Sets the PARI default real precision.
    9199        
    9200         This is used both for creation of new objects from strings and for
    9201         printing. It is the number of digits *IN DECIMAL* in which real
    9202         numbers are printed. It also determines the precision of objects
    9203         created by parsing strings (e.g. pari('1.2')), which is *not* the
    9204         normal way of creating new pari objects in Sage. It has *no*
    9205         effect on the precision of computations within the pari library.
    9206        
    9207         Returns the previous PARI real precision.
    9208         """
    9209         cdef unsigned long k
    9210        
    9211         k = GP_DATA.fmt.sigd
     9317        Sets the PARI precision for real numbers in decimal digits.
     9318       
     9319        This is used for three things:
     9320
     9321        #. it determines the precision of objects created by parsing
     9322           strings (e.g. ``pari('1.2')``).
     9323
     9324        #. it determines the precision for PARI library functions when
     9325           an exact input yields a inexact result (e.g. ``pari(2).sqrt()``
     9326           but not ``pari(2.0).sqrt()``).
     9327
     9328        #. it is an upper bound on the number of decimal digits shown
     9329           when printing a PARI real number.
     9330
     9331        For more details and examples, see :ref:`pari_real_precision`.
     9332       
     9333        INPUT:
     9334
     9335        - ``n`` -- Number of decimal digits
     9336
     9337        OUTPUT:
     9338
     9339        The previous PARI real precision.
     9340
     9341        EXAMPLES:
     9342
     9343        The default precision is 15 decimal digits::
     9344
     9345            sage: pari("Pi")
     9346            3.14159265358979
     9347            sage: oldprec = pari.set_real_precision(50)
     9348            sage: oldprec
     9349            15
     9350            sage: pari.get_real_precision()
     9351            50
     9352
     9353        We compute a more precise value of pi::
     9354
     9355            sage: precise_pi = pari("Pi")
     9356            sage: precise_pi
     9357            3.1415926535897932384626433832795028841971693993751
     9358
     9359        After resetting the precision back to the default value,
     9360        `precise_pi` stays the same but is printed with less digits::
     9361
     9362            sage: pari.set_real_precision(oldprec)
     9363            50
     9364            sage: precise_pi
     9365            3.14159265358979
     9366        """
    92129367        s = str(n)
    9213         sig_on()
    9214         sd_realprecision(s, 2)
    9215         sig_off()
    9216         return int(k)  # Python int
     9368        cdef long prev_precision
     9369        sig_on()
     9370        prev_precision = itos(sd_realprecision(NULL, d_RETURN))
     9371        sd_realprecision(s, d_SILENT)
     9372        global pari_default_prec
     9373        pari_default_prec = prec_bits_to_words(prec_dec_to_bits(n))
     9374        self.clear_stack()
     9375        return prev_precision
    92179376
    92189377    def get_real_precision(self):
    92199378        """
    9220         Returns the current PARI default real precision.
    9221        
    9222         This is used both for creation of new objects from strings and for
    9223         printing. It is the number of digits *IN DECIMAL* in which real
    9224         numbers are printed. It also determines the precision of objects
    9225         created by parsing strings (e.g. pari('1.2')), which is *not* the
    9226         normal way of creating new pari objects in Sage. It has *no*
    9227         effect on the precision of computations within the pari library.
    9228         """
    9229         return GP_DATA.fmt.sigd
     9379        Returns the PARI precision for real numbers in decimal digits.
     9380       
     9381        OUTPUT:
     9382
     9383        The current PARI real precision.
     9384
     9385        EXAMPLES::
     9386
     9387            sage: pari.get_real_precision()
     9388            15
     9389        """
     9390        cdef long prev_precision
     9391        sig_on()
     9392        prev_precision = itos(sd_realprecision(NULL, d_RETURN))
     9393        self.clear_stack()
     9394        return prev_precision
    92309395
    92319396    def set_series_precision(self, long n):
    92329397        global precdl
    92339398        precdl = n
    92349399
    92359400    def get_series_precision(self):
     9401        global precdl
    92369402        return precdl
    92379403       
    92389404
     
    96659831            return self(x)
    96669832        except (TypeError, AttributeError):
    96679833            raise TypeError, "no canonical coercion of %s into PARI"%x
    9668         if isinstance(x, gen):
    9669             return x
    9670         raise TypeError, "x must be a PARI object"
    96719834
    96729835    cdef _an_element_c_impl(self):  # override this in Cython
    96739836        return self.PARI_ZERO
    96749837
    9675     def new_with_bits_prec(self, s, long precision):
    9676         r"""
    9677         pari.new_with_bits_prec(self, s, precision) creates s as a PARI
    9678         gen with (at most) precision *bits* of precision.
    9679         """
    9680         cdef unsigned long old_prec
    9681         old_prec = GP_DATA.fmt.sigd
    9682         precision = prec_bits_to_dec(precision)
    9683         if not precision:
    9684             precision = old_prec
    9685         self.set_real_precision(precision)
    9686         x = self(s)
    9687         self.set_real_precision(old_prec)
    9688         return x
    9689        
    9690 
    9691 
    96929838    cdef long get_var(self, v):
    96939839        """
    96949840        Converts a Python string into a PARI variable reference number. Or
     
    989010036            self.init_primes(max(2*num_primes,20*n))
    989110037            return self.nth_prime(n)
    989210038
    9893     def euler(self, precision=0):
     10039    def euler(self, long precision=0):
    989410040        """
    989510041        Return Euler's constant to the requested real precision (in bits).
    989610042       
     
    990410050        sig_on()
    990510051        return self.new_gen(mpeuler(pbw(precision)))
    990610052
    9907     def pi(self, precision=0):
     10053    def pi(self, long precision=0):
    990810054        """
    990910055        Return the value of the constant pi to the requested real precision
    991010056        (in bits).
  • sage/libs/pari/gen_py.py

    diff --git a/sage/libs/pari/gen_py.py b/sage/libs/pari/gen_py.py
    a b  
     1"""
     2Convert PARI GENs to/from Sage.
     3
     4AUTHORS:
     5
     6- Jeroen Demeyer (2011-11-12): fix conversion of Python types to PARI
     7  (#11952)
     8
     9- Jeroen Demeyer (2011-11-19): Overhaul real precision (#9937)
     10
     11"""
     12
     13#*****************************************************************************
     14#       Copyright (C) ???? William Stein <wstein@gmail.com>
     15#       Copyright (C) 2011 Jeroen Demeyer <jdemeyer@cage.ugent.be>
     16#
     17#  Distributed under the terms of the GNU General Public License (GPL)
     18#  as published by the Free Software Foundation; either version 2 of
     19#  the License, or (at your option) any later version.
     20#                  http://www.gnu.org/licenses/
     21#*****************************************************************************
     22
    123import sage.libs.pari.gen as gen
    224from sage.misc.sage_eval import sage_eval
    325
    426from sage.rings.all import *
    5 I = ComplexField().gen()
    627
    728def pari(x):
    829    """
     
    3051        sage: a = pari(1/2); a, a.type()
    3152        (1/2, 't_FRAC')
    3253
    33     Conversion from reals uses the real's own precision, here 53 bits (the default)::
     54    Conversion from reals uses the real's own precision, which is
     55    53 bits by default.  However, PARI always works with whole machine
     56    words, so we get 64 bits of precision in PARI::
    3457
    35         sage: a = pari(1.2); a, a.type(), a.precision()
    36         (1.20000000000000, 't_REAL', 4) # 32-bit
    37         (1.20000000000000, 't_REAL', 3) # 64-bit
     58        sage: a = pari(1.2); a, a.type(), a.precision_bits()
     59        (1.20000000000000, 't_REAL', 64)
    3860
    39     Conversion from strings uses the current pari real precision.  By
    40     default this is 4 words, 38 digits, 128 bits on 64-bit machines
    41     and 5 words, 19 digits, 64 bits on 32-bit machines. ::
     61    Conversion from a more precise real number.  Note that the number
     62    is still printed with 15 digits, but it is actually computed with
     63    more bits::
     64
     65        sage: a = pari(RealField(128)(1.2)); a, a.type(), a.precision_bits()
     66        (1.20000000000000, 't_REAL', 128)
     67
     68    Conversion from strings uses the current PARI real precision.  By
     69    default this is 64 bits.  Adding PARI's two extra codewords, this
     70    gives 3 words on 64-bit machines and 4 words on 32-bit machines::
    4271   
    43         sage: a = pari('1.2'); a, a.type(), a.precision()
    44         (1.20000000000000, 't_REAL', 5) # 32-bit
    45         (1.20000000000000, 't_REAL', 4) # 64-bit
     72        sage: a = pari('1.2'); a, a.type(), a.precision_bits()
     73        (1.20000000000000, 't_REAL', 64)
    4674
    4775    Conversion from matrices is supported, but not from vectors; use
    4876    lists instead::
     
    93121
    94122def python(z, locals=None):
    95123    """
    96     Return the closest python/Sage equivalent of the given pari object.
     124    Return the closest Python/Sage equivalent of the given PARI object.
    97125
    98126    INPUT:
    99127
    100         - `z` -- pari object
     128    - ``z`` -- PARI object
    101129
    102         - `locals` -- optional dictionary used in fallback cases that
    103           involve sage_eval
     130    - ``locals`` -- optional dictionary used in fallback cases that
     131      involve sage_eval
    104132
    105133    The component parts of a t_COMPLEX may be t_INT, t_REAL, t_INTMOD,
    106134    t_FRAC, t_PADIC.  The components need not have the same type
     
    108136    t_REAL().  They are converted as follows:
    109137   
    110138    t_INT:    ZZ[i]
    111     t_FRAC:   QQ(i) 
     139    t_FRAC:   QQ(i)
    112140    t_REAL:   ComplexField(prec) for equivalent precision
    113141    t_INTMOD, t_PADIC: raise NotImplementedError
    114142
    115     EXAMPLES:
     143    EXAMPLES::
     144
    116145        sage: a = pari('(3+I)').python(); a
    117146        i + 3
    118147        sage: a.parent()
     
    129158        Rational Field
    130159
    131160        sage: a = pari('1.234').python(); a
    132         1.234000000000000000000000000           # 32-bit
    133         1.2340000000000000000000000000000000000 # 64-bit
     161        1.23400000000000000
    134162        sage: a.parent()
    135         Real Field with 96 bits of precision    # 32-bit
    136         Real Field with 128 bits of precision   # 64-bit
     163        Real Field with 64 bits of precision
    137164
    138165        sage: a = pari('(3+I)/2').python(); a
    139166        1/2*i + 3/2
     
    142169
    143170    Conversion of complex numbers: the next example is converting from
    144171    an element of the Symbolic Ring, which goes via the string
    145     representation and hence the precision is architecture-dependent::
     172    representation::
    146173   
    147174        sage: I = SR(I)
    148175        sage: a = pari(1.0+2.0*I).python(); a
    149         1.000000000000000000000000000 + 2.000000000000000000000000000*I  # 32-bit
    150         1.0000000000000000000000000000000000000 + 2.0000000000000000000000000000000000000*I # 64-bit
     176        1.00000000000000000 + 2.00000000000000000*I
    151177        sage: type(a)
    152178        <type 'sage.rings.complex_number.ComplexNumber'>
    153179        sage: a.parent()
    154         Complex Field with 96 bits of precision # 32-bit
    155         Complex Field with 128 bits of precision # 64-bit
     180        Complex Field with 64 bits of precision
    156181
    157     For architecture-independent complex numbers, start from a
    158     suitable ComplexField:
     182    Starting instead from a suitable ComplexField::
     183
    159184        sage: z = pari(CC(1.0+2.0*I)); z
    160185        1.00000000000000 + 2.00000000000000*I
    161186        sage: a=z.python(); a
     
    208233    """
    209234    t = z.type()
    210235    if t == "t_REAL":
    211         return RealField(gen.prec_words_to_bits(z.precision()))(z)
     236        return RealField(z.precision_bits())(z)
    212237    elif t == "t_FRAC":
    213238         Q = RationalField()
    214239         return Q(z)
     
    221246        if tx in ["t_INTMOD", "t_PADIC"] or ty in ["t_INTMOD", "t_PADIC"]:
    222247            raise NotImplementedError, "No conversion to python available for t_COMPLEX with t_INTMOD or t_PADIC components"
    223248        if tx == "t_REAL" or ty == "t_REAL":
    224             xprec = z.real().precision() # will be 0 if exact
    225             yprec = z.imag().precision() # will be 0 if exact
    226             if xprec == 0:
    227                 prec = gen.prec_words_to_bits(yprec)
    228             elif yprec == 0:
    229                 prec = gen.prec_words_to_bits(xprec)
    230             else:
    231                 prec = max(gen.prec_words_to_bits(xprec),gen.prec_words_to_bits(yprec))
     249            prec = z.precision_bits()
    232250            R = RealField(prec)
    233251            C = ComplexField(prec)
    234252            return C(R(z.real()), R(z.imag()))
    235253        if tx == "t_FRAC" or ty == "t_FRAC":
    236254            return QuadraticField(-1,'i')([python(c) for c in list(z)])
    237255        if tx == "t_INT" or ty == "t_INT":
    238             return QuadraticField(-1,'i').ring_of_integers()([python(c) for c in list(z)])           
     256            return QuadraticField(-1,'i').ring_of_integers()([python(c) for c in list(z)])
    239257        raise NotImplementedError, "No conversion to python available for t_COMPLEX with components %s"%(tx,ty)
    240258    elif t == "t_VEC":
    241259        return [python(x) for x in z.python_list()]
  • sage/matrix/matrix1.pyx

    diff --git a/sage/matrix/matrix1.pyx b/sage/matrix/matrix1.pyx
    a b  
    99    sage: TestSuite(A).run()
    1010"""
    1111
    12 ################################################################################
     12#*****************************************************************************
    1313#       Copyright (C) 2005, 2006 William Stein <wstein@gmail.com>
    1414#
    15 #  Distributed under the terms of the GNU General Public License (GPL).
    16 The full text of the GPL is available at:
    17 #
     15#  Distributed under the terms of the GNU General Public License (GPL)
     16as published by the Free Software Foundation; either version 2 of
     17#  the License, or (at your option) any later version.
    1818#                  http://www.gnu.org/licenses/
    19 ################################################################################
     19#*****************************************************************************
    2020
    2121include "../ext/stdsage.pxi"
    2222include "../ext/python.pxi"
     
    5959
    6060    def _pari_(self):
    6161        """
    62         Return the Pari matrix corresponding to self.
     62        Return the PARI matrix corresponding to self.
    6363
    6464        EXAMPLES::
    6565
     
    7777        This function preserves precision for entries of inexact type (e.g.
    7878        reals)::
    7979
    80             sage: R = RealField(4)       # 4 bits of precision
     80            sage: R = RealField(120)       # 120 bits of precision
    8181            sage: a = matrix(R, 2, [1, 2, 3, 1]); a
    82             [1.0 2.0]
    83             [3.0 1.0]
     82            [1.0000000000000000000000000000000000 2.0000000000000000000000000000000000]
     83            [3.0000000000000000000000000000000000 1.0000000000000000000000000000000000]
    8484            sage: b = pari(a); b
    85             [1.000000000, 2.000000000; 3.000000000, 1.000000000] # 32-bit
    86             [1.00000000000000, 2.00000000000000; 3.00000000000000, 1.00000000000000] # 64-bit
    87             sage: b[0][0].precision()    # in words
    88             3
     85            [1.00000000000000, 2.00000000000000; 3.00000000000000, 1.00000000000000]
     86            sage: b[0][0].precision_bits()  # PARI always works with whole machine words
     87            128
    8988        """
    9089        from sage.libs.pari.gen import pari
    9190        return pari.matrix(self._nrows, self._ncols, self._list())
  • sage/rings/polynomial/polynomial_element.pyx

    diff --git a/sage/rings/polynomial/polynomial_element.pyx b/sage/rings/polynomial/polynomial_element.pyx
    a b  
    28532853        from sage.rings.integer_ring import is_IntegerRing
    28542854        from sage.rings.rational_field import is_RationalField
    28552855
    2856         n = None
    2857 
    28582856        if is_IntegerModRing(R) or is_IntegerRing(R) or is_RationalField(R):
    28592857
    28602858            try:
     
    29242922            return self._factor_pari_helper(G)
    29252923
    29262924        elif is_RealField(R):
    2927             n = pari.set_real_precision(int(3.5*R.prec()) + 1)
    29282925            G = list(self._pari_with_name().factor())
    29292926
    29302927        elif sage.rings.complex_double.is_ComplexDoubleField(R):
     
    29732970        elif sage.rings.complex_field.is_ComplexField(R):
    29742971            # This is a hack to make the polynomial have complex coefficients, since
    29752972            # otherwise PARI will factor over RR.
    2976             n = pari.set_real_precision(int(3.5*R.prec()) + 1)           
    29772973            if self.leading_coefficient() != R.gen():
    29782974                G = list((pari(R.gen())*self._pari_with_name()).factor())
    29792975            else:
     
    29852981        if G is None:
    29862982            raise NotImplementedError
    29872983       
    2988         return self._factor_pari_helper(G, n)
    2989 
    2990     def _factor_pari_helper(self, G, n=None, unit=None):
     2984        return self._factor_pari_helper(G)
     2985
     2986    def _factor_pari_helper(self, G, unit=None):
    29912987        """
    29922988        Fix up and normalize a factorization that came from PARI.
    29932989       
     
    30523048                F.append((R(unit), ZZ(1)))
    30533049                unit = R.base_ring().one_element()
    30543050       
    3055         if not n is None:
    3056             pari.set_real_precision(n)  # restore precision
    30573051        return Factorization(F, unit)
    30583052
    30593053    @coerce_binop
  • sage/rings/real_mpfi.pyx

    diff --git a/sage/rings/real_mpfi.pyx b/sage/rings/real_mpfi.pyx
    a b  
    26612661
    26622662#     def _complex_number_(self):
    26632663#         return sage.rings.complex_field.ComplexField(self.prec())(self)
    2664 
    2665 #     def _pari_(self):
    2666 #         return sage.libs.pari.all.pari.new_with_bits_prec(str(self), (<RealIntervalField>self._parent).__prec)
    26672664   
    26682665    def unique_floor(self):
    26692666        """
     
    42644261#             0.000000000000000
    42654262#             sage: R(1).zeta()
    42664263#             +infinity
    4267 
    4268 #         Computing zeta using PARI is much more efficient in difficult cases.
    4269 #         Here's how to compute zeta with at least a given precision:
    4270        
    4271 #              sage: z = pari.new_with_bits_prec(2, 53).zeta(); z
    4272 #              1.644934066848226436472415167              # 32-bit
    4273 #              1.6449340668482264364724151666460251892    # 64-bit
    4274 
    4275 #         Note that the number of bits of precision in the constructor only
    4276 #         affects the internal precision of the pari number, not the number
    4277 #         of digits that gets displayed.  To increase that you must
    4278 #         use \code{pari.set_real_precision}.
    4279        
    4280 #              sage: type(z)
    4281 #              <type 'sage.libs.pari.gen.gen'>
    4282 #              sage: R(z)
    4283 #              1.64493406684822
    42844264#         """
    42854265#         cdef RealIntervalFieldElement x
    42864266#         x = self._new()
  • sage/rings/real_mpfr.pyx

    diff --git a/sage/rings/real_mpfr.pyx b/sage/rings/real_mpfr.pyx
    a b  
    11871187            sage: rt2
    11881188            1.41421356237310
    11891189            sage: rt2.python()
    1190             1.414213562373095048801688724              # 32-bit
    1191             1.4142135623730950488016887242096980786    # 64-bit
     1190            1.41421356237309505
    11921191            sage: rt2.python().prec()
    1193             96                                         # 32-bit
    1194             128                                        # 64-bit
     1192            64
    11951193            sage: pari(rt2.python()) == rt2
    11961194            True
    11971195            sage: for i in xrange(1, 1000):
  • sage/schemes/elliptic_curves/ell_point.py

    diff --git a/sage/schemes/elliptic_curves/ell_point.py b/sage/schemes/elliptic_curves/ell_point.py
    a b  
    29932993        E_pari = E_work.pari_curve(prec=working_prec)
    29942994        log_pari = E_pari.ellpointtoz(pt_pari, precision=working_prec)
    29952995
    2996         while prec_words_to_bits(log_pari.precision()) < precision:
     2996        while log_pari.precision_bits() < precision:
    29972997            # result is not precise enough, re-compute with double
    2998             # precision. if the base field is not QQ, this
     2998            # the precision. if the base field is not QQ, this
    29992999            # requires modifying the precision of the embedding,
    30003000            # the curve, and the point
    30013001            working_prec = 2*working_prec
  • sage/schemes/elliptic_curves/ell_rational_field.py

    diff --git a/sage/schemes/elliptic_curves/ell_rational_field.py b/sage/schemes/elliptic_curves/ell_rational_field.py
    a b  
    632632        ::
    633633       
    634634            sage: E = EllipticCurve('37a1').pari_curve()
    635             sage: E[14].python().prec()
     635            sage: E[14].python().precision()
    636636            64
    637             sage: [a.precision() for a in E]
    638             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4] # 32-bit
    639             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3] # 64-bit
     637            sage: [a.precision_bits() for a in E]
     638            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 64, 64, 64, 64, 64]
    640639       
    641640        This shows that the bug uncovered by trac #4715 is fixed::
    642641