Ticket #9937: 9937_pari_prec.patch
File 9937_pari_prec.patch, 77.4 KB (added by , 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 241 241 return (s/2 + 1).gamma() * (s-1) * (R.pi()**(-s/2)) * s.zeta() 242 242 243 243 244 ## # Use PARI on complex nubmer245 ## 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."%s252 ## return z253 254 255 244 #def pi_approx(prec=53): 256 245 # """ 257 246 # 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 1002 1002 1003 1003 EXAMPLES:: 1004 1004 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)] 1007 1007 sage: maxima.plot_list(zeta_ptsx, zeta_ptsy) # not tested 1008 1008 sage: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "zeta.eps"]' 1009 1009 sage: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts) # not tested … … 1043 1043 sage: yy = [ i/10.0 for i in range (-10,10)] 1044 1044 sage: x0 = [ 0 for i in range (-10,10)] 1045 1045 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)] 1048 1048 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)] 1051 1051 sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]]) # not tested 1052 1052 sage: opts='[gnuplot_preamble, "set nokey"]' 1053 1053 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 7 7 allocatemem = pari.allocatemem 8 8 9 9 PariError = _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 18 18 - Jeroen Demeyer (2011-11-12): rewrite various conversion routines 19 19 (#11611, #11854, #11952) 20 20 21 - Jeroen Demeyer (2012-01-23): Overhaul real precision (#9937) 21 22 22 23 EXAMPLES:: 23 24 … … 40 41 sage: type(1 + pari(1)) 41 42 <type 'sage.libs.pari.gen.gen'> 42 43 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:: 44 TESTS: 45 46 Check that output from PARI's ``print`` command is actually seen by 47 Sage (ticket #9636):: 48 49 sage: pari('print("test")') 50 test 51 52 .. _pari_real_precision: 53 54 Real precision in the PARI library 55 ================================== 56 57 The PARI library has a concept of "real precision", the precision for 58 real numbers (i.e. floating point numbers). Since for example elliptic 59 curves over `\QQ` also contain some real data (the period lattice), 60 this real precision applies to a lot of things in PARI. 61 62 The default real precision in communicating with the PARI library is 64 63 bits (precision in PARI is always a multiple of the machine word size, 64 so the Sage default of 53 bits gets rounded up to 64 bits). PARI real 65 numbers are by default printed with 15 decimal digits, this is 66 consistent with the 53 bits default of Sage. 67 68 You can change the default precision using 69 :meth:`pari.set_real_precision() <PariInstance.set_real_precision>`. 70 The 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 81 The 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 121 The 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 153 Almost all PARI functions where the real precision matters have an 154 optional argument ``precision``. This can be used to override the 155 default real precision. Note that the ``precision`` argument is always 156 a precision *in bits* as opposed to the precision set using 157 :meth:`pari.set_real_precision() <PariInstance.set_real_precision>`, 158 which is in *decimal digits*. 159 As 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 168 We reset the precision back to the default:: 169 170 sage: _ = pari.set_real_precision(15) 171 172 Conversions between Sage and PARI 173 ================================= 174 175 Some care must be taken when juggling inexact types back and forth 176 between Sage and PARI. In theory, calling ``p = pari(s)`` creates a 177 PARI object ``p`` with the same precision as ``s``; in practice, the 178 PARI library's precision is word-based, so it will go up to the next 179 word. For example, a default 53-bit Sage real s will be bumped up to 180 64 bits by adding bogus 11 bits. The function ``p.python()`` returns a 181 Sage object with exactly the same precision as the PARI object ``p``. 182 So ``pari(s).python()`` is definitely not equal to ``s``, since it has 183 64 bits of precision, including the bogus 11 bits. The correct way of 184 avoiding this is to coerce ``pari(s).python()`` back into a domain with 185 the right precision. This has to be done by the user. For instance, if 186 we want to use the PARI library to compute `\sqrt{\pi}` with a precision 187 of 100 bits:: 116 188 117 189 sage: R = RealField(100) 118 190 sage: s = R(pi); s … … 129 201 sage: R(x) == s.sqrt() 130 202 True 131 203 204 All these conversions are completely independent of the current PARI 205 real precision. 206 132 207 Elliptic curves and precision: If you are working with elliptic 133 208 curves and want to compute with a precision other than the default 134 209 53 bits, you should use the precision parameter of ellinit():: … … 138 213 sage: eta1 = e.elleta()[0] 139 214 sage: R(eta1) 140 215 3.6054636014326520859158205642077267748102690 141 142 Number fields and precision: TODO143 144 TESTS:145 146 Check that output from PARI's print command is actually seen by147 Sage (ticket #9636)::148 149 sage: pari('print("test")')150 test151 152 216 """ 153 217 154 218 #***************************************************************************** 155 # Copyright (C) 2006, 2010 William Stein <wstein@gmail.com>219 # Copyright (C) 2006, 2010 William Stein <wstein@gmail.com> 156 220 # Copyright (C) ???? Justin Walker 157 221 # Copyright (C) ???? Gonzalo Tornaria 158 222 # Copyright (C) 2010 Robert Bradshaw <robertwb@math.washington.edu> 159 # Copyright (C) 2010 ,2011Jeroen Demeyer <jdemeyer@cage.ugent.be>223 # Copyright (C) 2010--2012 Jeroen Demeyer <jdemeyer@cage.ugent.be> 160 224 # 161 225 # Distributed under the terms of the GNU General Public License (GPL) 162 226 # as published by the Free Software Foundation; either version 2 of … … 174 238 from sage.structure.parent cimport Parent 175 239 from sage.misc.randstate cimport randstate, current_randstate 176 240 177 from sage.misc.misc_c import is_64_bit178 179 241 include 'pari_err.pxi' 180 242 include '../../ext/stdsage.pxi' 181 243 … … 187 249 # rather than silently corrupting memory. 188 250 cdef long mpz_t_offset = 1000000000 189 251 190 # The unique running P ariinstance.252 # The unique running PARI instance. 191 253 cdef PariInstance pari_instance, P 192 254 pari_instance = PariInstance(16000000, 500000) 193 P = pari_instance # shorthand notation 255 P = pari_instance # Shorthand notation 256 pari = pari_instance # A copy accessible from external pure Python code 194 257 195 258 # PariInstance.__init__ must not create gen objects because their parent is not constructed yet 196 259 sig_on() … … 205 268 206 269 cdef pari_sp mytop 207 270 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(). 275 cdef long pari_default_prec 229 276 230 277 ################################################################# 231 278 # conversions between various real precision models 232 279 ################################################################# 233 280 234 def prec_bits_to_dec(int prec_in_bits): 281 DEF log_10_base_2 = 3.321928094887362 282 283 cpdef long prec_bits_to_dec(long prec_in_bits): 235 284 r""" 236 285 Convert from precision expressed in bits to precision expressed in 237 decimal .286 decimal, rounded down. 238 287 239 288 EXAMPLES:: 240 289 … … 251 300 (224, 67), 252 301 (256, 77)] 253 302 """ 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 305 cpdef long prec_dec_to_bits(long prec_in_dec): 258 306 r""" 259 307 Convert from precision expressed in decimal to precision expressed 260 in bits .308 in bits, rounded up. 261 309 262 310 EXAMPLES:: 263 311 264 312 sage: import sage.libs.pari.gen as gen 265 313 sage: gen.prec_dec_to_bits(15) 266 49314 50 267 315 sage: [(n,gen.prec_dec_to_bits(n)) for n in range(10,100,10)] 268 [(10, 3 3),269 (20, 6 6),270 (30, 99),271 (40, 13 2),272 (50, 16 6),273 (60, 199),274 (70, 23 2),275 (80, 26 5),276 (90, 29 8)]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)] 277 325 """ 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 328 cpdef inline long prec_bits_to_words(long prec_in_bits): 282 329 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. 287 342 288 343 EXAMPLES:: 289 344 … … 291 346 sage: gen.prec_bits_to_words(70) 292 347 5 # 32-bit 293 348 4 # 64-bit 294 295 ::296 297 349 sage: [(32*n,gen.prec_bits_to_words(32*n)) for n in range(1,9)] 298 350 [(32, 3), (64, 4), (96, 5), (128, 6), (160, 7), (192, 8), (224, 9), (256, 10)] # 32-bit 299 351 [(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 300 360 """ 301 361 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 305 364 306 365 # increase prec_in_bits to the nearest multiple of wordsize 307 cdef intpadded_bits366 cdef long padded_bits 308 367 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() 371 cdef inline long pbw(long prec_in_bits): 372 return prec_bits_to_words(prec_in_bits) 373 374 cpdef inline long prec_words_to_bits(long prec_in_words): 314 375 r""" 315 Convert from parireal precision expressed in words to precision376 Convert from PARI real precision expressed in words to precision 316 377 expressed in bits. Note: this adjusts for the two codewords of a 317 378 pari real, and is architecture-dependent. 318 379 … … 326 387 [(3, 32), (4, 64), (5, 96), (6, 128), (7, 160), (8, 192), (9, 224)] # 32-bit 327 388 [(3, 64), (4, 128), (5, 192), (6, 256), (7, 320), (8, 384), (9, 448)] # 64-bit 328 389 """ 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 370 392 371 393 # temp variables 372 394 cdef GEN t0,t1,t2,t3,t4 … … 673 695 return P.new_gen(gmod(selfgen.g, othergen.g)) 674 696 return sage.structure.element.bin_op(self, other, operator.mod) 675 697 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))) 685 726 686 727 def __neg__(gen self): 687 728 sig_on() 688 729 return P.new_gen(gneg(self.g)) 689 730 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 694 731 def __rshift__(gen self, long n): 695 732 sig_on() 696 733 return P.new_gen(gshift(self.g, -n)) … … 1787 1824 1788 1825 sage = python 1789 1826 1790 # This older version illustrates how irrelevant the real_precision1791 # global variable is in the pari library. The output of this1792 # function when self is a t_REAL is completely independent of the1793 # precision parameter. The new version above has no precision1794 # parameter at all: the caller can coerce into a lower-precision1795 # RealField if desired (or even in to a higher precision one, but1796 # 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) + 11804 # import sage.libs.pari.gen_py1805 # cdef long orig_prec1806 # 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 x1814 # else:1815 # return sage.libs.pari.gen_py.python(self)1816 1817 1827 _sage_ = _eval_ = python 1818 1828 1819 1829 def __long__(gen self): … … 2585 2595 sig_on() 2586 2596 return P.new_gen(gtopolyrev(self.g, P.get_var(v))) 2587 2597 2588 def Qfb(gen a, b, c, D=0 ):2598 def Qfb(gen a, b, c, D=0, long precision=0): 2589 2599 """ 2590 2600 Qfb(a,b,c,D=0.): Returns the binary quadratic form 2591 2601 … … 2634 2644 """ 2635 2645 t0GEN(b); t1GEN(c); t2GEN(D) 2636 2646 sig_on() 2637 return P.new_gen(Qfb0(a.g, t0, t1, t2, p rec))2647 return P.new_gen(Qfb0(a.g, t0, t1, t2, pbw(precision))) 2638 2648 2639 2649 2640 2650 def Ser(gen x, v=-1, long seriesprecision = 16): … … 3434 3444 sig_on() 3435 3445 return P.new_gen(gconj(x.g)) 3436 3446 3437 def conjvec(gen x ):3447 def conjvec(gen x, long precision=0): 3438 3448 """ 3439 3449 conjvec(x): Returns the vector of all conjugates of the algebraic 3440 3450 number x. An algebraic number is a polynomial over Q modulo an … … 3456 3466 [1.44224957030741, -0.721124785153704 + 1.24902476648341*I, -0.721124785153704 - 1.24902476648341*I]~ 3457 3467 """ 3458 3468 sig_on() 3459 return P.new_gen(conjvec(x.g, p rec))3469 return P.new_gen(conjvec(x.g, pbw(precision))) 3460 3470 3461 3471 def denominator(gen x): 3462 3472 """ … … 3772 3782 3773 3783 def precision(gen x, long n=-1): 3774 3784 """ 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.") 3790 3809 if n <= -1: 3791 3810 return precision(x.g) 3792 3811 sig_on() 3793 3812 return P.new_gen(precision0(x.g, n)) 3794 3813 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 3795 3956 def random(gen N): 3796 3957 r""" 3797 3958 ``random(N=2^31)``: Return a pseudo-random integer … … 4233 4394 Returns the absolute value of x (its modulus, if x is complex). 4234 4395 Rational functions are not allowed. Contrary to most transcendental 4235 4396 functions, an exact argument is not converted to a real number 4236 before applying absand an exact result is returned if possible.4397 before applying ``abs`` and an exact result is returned if possible. 4237 4398 4238 4399 EXAMPLES:: 4239 4400 … … 4243 4404 4244 4405 If x is a polynomial, returns -x if the leading coefficient is real 4245 4406 and negative else returns x. For a power series, the constant 4246 coefficient is considered instead. 4247 4248 EXAMPLES:: 4407 coefficient is considered instead:: 4249 4408 4250 4409 sage: pari('x-1.2*x^2').abs() 4251 4410 1.20000000000000*x^2 - x 4252 4411 """ 4253 4412 sig_on() 4254 # the prec parameter here has no effect4255 return P.new_gen(gabs(x.g, p rec))4413 # prec seems to be ignored here 4414 return P.new_gen(gabs(x.g, pbw(0))) 4256 4415 4257 4416 def acos(gen x, long precision=0): 4258 4417 r""" … … 4280 4439 sig_on() 4281 4440 return P.new_gen(gacos(x.g, pbw(precision))) 4282 4441 4283 def acosh(gen x, precision=0):4442 def acosh(gen x, long precision=0): 4284 4443 r""" 4285 4444 The principal branch of `\cosh^{-1}(x)`, so that 4286 4445 `\Im(\mathrm{acosh}(x))` belongs to `[0,Pi]`. If … … 4305 4464 sig_on() 4306 4465 return P.new_gen(gach(x.g, pbw(precision))) 4307 4466 4308 def agm(gen x, y, precision=0):4467 def agm(gen x, y, long precision=0): 4309 4468 r""" 4310 4469 The arithmetic-geometric mean of x and y. In the case of complex or 4311 4470 negative numbers, the principal square root is always chosen. … … 4335 4494 sig_on() 4336 4495 return P.new_gen(agm(x.g, t0, pbw(precision))) 4337 4496 4338 def arg(gen x, precision=0):4497 def arg(gen x, long precision=0): 4339 4498 r""" 4340 4499 arg(x): argument of x,such that `-\pi < \arg(x) \leq \pi`. 4341 4500 … … 4353 4512 sig_on() 4354 4513 return P.new_gen(garg(x.g, pbw(precision))) 4355 4514 4356 def asin(gen x, precision=0):4515 def asin(gen x, long precision=0): 4357 4516 r""" 4358 4517 The principal branch of `\sin^{-1}(x)`, so that 4359 4518 `\RR e(\mathrm{asin}(x))` belongs to `[-\pi/2,\pi/2]`. If … … 4375 4534 sig_on() 4376 4535 return P.new_gen(gasin(x.g, pbw(precision))) 4377 4536 4378 def asinh(gen x, precision=0):4537 def asinh(gen x, long precision=0): 4379 4538 r""" 4380 4539 The principal branch of `\sinh^{-1}(x)`, so that 4381 4540 `\Im(\mathrm{asinh}(x))` belongs to `[-\pi/2,\pi/2]`. … … 4396 4555 sig_on() 4397 4556 return P.new_gen(gash(x.g, pbw(precision))) 4398 4557 4399 def atan(gen x, precision=0):4558 def atan(gen x, long precision=0): 4400 4559 r""" 4401 4560 The principal branch of `\tan^{-1}(x)`, so that 4402 4561 `\RR e(\mathrm{atan}(x))` belongs to `]-\pi/2, \pi/2[`. … … 4417 4576 sig_on() 4418 4577 return P.new_gen(gatan(x.g, pbw(precision))) 4419 4578 4420 def atanh(gen x, precision=0):4579 def atanh(gen x, long precision=0): 4421 4580 r""" 4422 4581 The principal branch of `\tanh^{-1}(x)`, so that 4423 4582 `\Im(\mathrm{atanh}(x))` belongs to `]-\pi/2,\pi/2]`. If … … 4456 4615 sig_on() 4457 4616 return P.new_gen(bernfrac(x)) 4458 4617 4459 def bernreal(gen x ):4618 def bernreal(gen x, long precision=0): 4460 4619 r""" 4461 4620 The Bernoulli number `B_x`, as for the function bernfrac, 4462 4621 but `B_x` is returned as a real number (with the current … … 4468 4627 54.9711779448622 4469 4628 """ 4470 4629 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))) 4473 4631 4474 4632 def bernvec(gen x): 4475 4633 r""" … … 4492 4650 sig_on() 4493 4651 return P.new_gen(bernvec(x)) 4494 4652 4495 def besselh1(gen nu, x, precision=0):4653 def besselh1(gen nu, x, long precision=0): 4496 4654 r""" 4497 4655 The `H^1`-Bessel function of index `\nu` and 4498 4656 argument `x`. … … 4512 4670 sig_on() 4513 4671 return P.new_gen(hbessel1(nu.g, t0, pbw(precision))) 4514 4672 4515 def besselh2(gen nu, x, precision=0):4673 def besselh2(gen nu, x, long precision=0): 4516 4674 r""" 4517 4675 The `H^2`-Bessel function of index `\nu` and 4518 4676 argument `x`. … … 4532 4690 sig_on() 4533 4691 return P.new_gen(hbessel2(nu.g, t0, pbw(precision))) 4534 4692 4535 def besselj(gen nu, x, precision=0):4693 def besselj(gen nu, x, long precision=0): 4536 4694 r""" 4537 4695 Bessel J function (Bessel function of the first kind), with index 4538 4696 `\nu` and argument `x`. If `x` converts to … … 4555 4713 sig_on() 4556 4714 return P.new_gen(jbessel(nu.g, t0, pbw(precision))) 4557 4715 4558 def besseljh(gen nu, x, precision=0):4716 def besseljh(gen nu, x, long precision=0): 4559 4717 """ 4560 4718 J-Bessel function of half integral index (Spherical Bessel 4561 4719 function of the first kind). More precisely, besseljh(n,x) computes … … 4580 4738 sig_on() 4581 4739 return P.new_gen(jbesselh(nu.g, t0, pbw(precision))) 4582 4740 4583 def besseli(gen nu, x, precision=0):4741 def besseli(gen nu, x, long precision=0): 4584 4742 r""" 4585 4743 Bessel I function (Bessel function of the second kind), with index 4586 4744 `\nu` and argument `x`. If `x` converts to … … 4606 4764 sig_on() 4607 4765 return P.new_gen(ibessel(nu.g, t0, pbw(precision))) 4608 4766 4609 def besselk(gen nu, x, long flag=0, precision=0):4767 def besselk(gen nu, x, long flag=0, long precision=0): 4610 4768 """ 4611 4769 nu.besselk(x, flag=0): K-Bessel function (modified Bessel function 4612 4770 of the second kind) of index nu, which can be complex, and argument … … 4649 4807 sig_on() 4650 4808 return P.new_gen(kbessel(nu.g, t0, pbw(precision))) 4651 4809 4652 def besseln(gen nu, x, precision=0):4810 def besseln(gen nu, x, long precision=0): 4653 4811 """ 4654 4812 nu.besseln(x): Bessel N function (Spherical Bessel function of the 4655 4813 second kind) of index nu and argument x. … … 4670 4828 sig_on() 4671 4829 return P.new_gen(nbessel(nu.g, t0, pbw(precision))) 4672 4830 4673 def cos(gen x, precision=0):4831 def cos(gen x, long precision=0): 4674 4832 """ 4675 4833 The cosine function. 4676 4834 … … 4692 4850 sig_on() 4693 4851 return P.new_gen(gcos(x.g, pbw(precision))) 4694 4852 4695 def cosh(gen x, precision=0):4853 def cosh(gen x, long precision=0): 4696 4854 """ 4697 4855 The hyperbolic cosine function. 4698 4856 … … 4714 4872 sig_on() 4715 4873 return P.new_gen(gch(x.g, pbw(precision))) 4716 4874 4717 def cotan(gen x, precision=0):4875 def cotan(gen x, long precision=0): 4718 4876 """ 4719 4877 The cotangent of x. 4720 4878 … … 4741 4899 sig_on() 4742 4900 return P.new_gen(gcotan(x.g, pbw(precision))) 4743 4901 4744 def dilog(gen x, precision=0):4902 def dilog(gen x, long precision=0): 4745 4903 r""" 4746 4904 The principal branch of the dilogarithm of `x`, i.e. the 4747 4905 analytic continuation of the power series … … 4763 4921 sig_on() 4764 4922 return P.new_gen(dilog(x.g, pbw(precision))) 4765 4923 4766 def eint1(gen x, long n=0, precision=0):4924 def eint1(gen x, long n=0, long precision=0): 4767 4925 r""" 4768 4926 x.eint1(n): exponential integral E1(x): 4769 4927 … … 4793 4951 else: 4794 4952 return P.new_gen(veceint1(x.g, stoi(n), pbw(precision))) 4795 4953 4796 def erfc(gen x, precision=0):4954 def erfc(gen x, long precision=0): 4797 4955 r""" 4798 4956 Return the complementary error function: 4799 4957 … … 4816 4974 sig_on() 4817 4975 return P.new_gen(gerfc(x.g, pbw(precision))) 4818 4976 4819 def eta(gen x, flag=0, precision=0):4977 def eta(gen x, flag=0, long precision=0): 4820 4978 r""" 4821 4979 x.eta(flag=0): if flag=0, `\eta` function without the 4822 4980 `q^{1/24}`; otherwise `\eta` of the complex number … … 4846 5004 return P.new_gen(trueeta(x.g, pbw(precision))) 4847 5005 return P.new_gen(eta(x.g, pbw(precision))) 4848 5006 4849 def exp(gen self, precision=0):5007 def exp(gen self, long precision=0): 4850 5008 """ 4851 5009 x.exp(): exponential of x. 4852 5010 … … 4867 5025 sig_on() 4868 5026 return P.new_gen(gexp(self.g, pbw(precision))) 4869 5027 4870 def gamma(gen s, precision=0):5028 def gamma(gen s, long precision=0): 4871 5029 """ 4872 5030 s.gamma(precision): Gamma function at s. 4873 5031 … … 4896 5054 sig_on() 4897 5055 return P.new_gen(ggamma(s.g, pbw(precision))) 4898 5056 4899 def gammah(gen s, precision=0):5057 def gammah(gen s, long precision=0): 4900 5058 """ 4901 5059 s.gammah(): Gamma function evaluated at the argument x+1/2. 4902 5060 … … 4918 5076 sig_on() 4919 5077 return P.new_gen(ggamd(s.g, pbw(precision))) 4920 5078 4921 def hyperu(gen a, b, x, precision=0):5079 def hyperu(gen a, b, x, long precision=0): 4922 5080 r""" 4923 5081 a.hyperu(b,x): U-confluent hypergeometric function. 4924 5082 … … 4938 5096 sig_on() 4939 5097 return P.new_gen(hyperu(a.g, t0, t1, pbw(precision))) 4940 5098 4941 def incgam(gen s, x, y=None, precision=0):5099 def incgam(gen s, x, y=None, long precision=0): 4942 5100 r""" 4943 5101 s.incgam(x, y, precision): incomplete gamma function. y is optional 4944 5102 and is the precomputed value of gamma(s). … … 4962 5120 t1GEN(y) 4963 5121 return P.new_gen(incgam0(s.g, t0, t1, pbw(precision))) 4964 5122 4965 def incgamc(gen s, x, precision=0):5123 def incgamc(gen s, x, long precision=0): 4966 5124 r""" 4967 5125 s.incgamc(x): complementary incomplete gamma function. 4968 5126 … … 4988 5146 sig_on() 4989 5147 return P.new_gen(incgamc(s.g, t0, pbw(precision))) 4990 5148 4991 def log(gen x, precision=0):5149 def log(gen x, long precision=0): 4992 5150 r""" 4993 5151 x.log(): natural logarithm of x. 4994 5152 … … 5031 5189 sig_on() 5032 5190 return P.new_gen(glog(x.g, pbw(precision))) 5033 5191 5034 def lngamma(gen x, precision=0):5192 def lngamma(gen x, long precision=0): 5035 5193 r""" 5036 5194 This method is deprecated, please use :meth:`.log_gamma` instead. 5037 5195 … … 5047 5205 deprecation("The method lngamma() is deprecated. Use log_gamma() instead.") 5048 5206 return x.log_gamma(precision) 5049 5207 5050 def log_gamma(gen x, precision=0):5208 def log_gamma(gen x, long precision=0): 5051 5209 r""" 5052 5210 Logarithm of the gamma function of x. 5053 5211 … … 5073 5231 sig_on() 5074 5232 return P.new_gen(glngamma(x.g, pbw(precision))) 5075 5233 5076 def polylog(gen x, long m, flag=0, precision=0):5234 def polylog(gen x, long m, flag=0, long precision=0): 5077 5235 """ 5078 5236 x.polylog(m,flag=0): m-th polylogarithm of x. flag is optional, and 5079 5237 can be 0: default, 1: D_m -modified m-th polylog of x, 2: … … 5101 5259 sig_on() 5102 5260 return P.new_gen(polylog0(m, x.g, flag, pbw(precision))) 5103 5261 5104 def psi(gen x, precision=0):5262 def psi(gen x, long precision=0): 5105 5263 r""" 5106 5264 x.psi(): psi-function at x. 5107 5265 … … 5121 5279 sig_on() 5122 5280 return P.new_gen(gpsi(x.g, pbw(precision))) 5123 5281 5124 def sin(gen x, precision=0):5282 def sin(gen x, long precision=0): 5125 5283 """ 5126 5284 x.sin(): The sine of x. 5127 5285 … … 5141 5299 sig_on() 5142 5300 return P.new_gen(gsin(x.g, pbw(precision))) 5143 5301 5144 def sinh(gen x, precision=0):5302 def sinh(gen x, long precision=0): 5145 5303 """ 5146 5304 The hyperbolic sine function. 5147 5305 … … 5188 5346 return P.new_gen(gsqr(x.g)) 5189 5347 5190 5348 5191 def sqrt(gen x, precision=0):5349 def sqrt(gen x, long precision=0): 5192 5350 """ 5193 5351 x.sqrt(precision): The square root of x. 5194 5352 … … 5205 5363 sig_on() 5206 5364 return P.new_gen(gsqrt(x.g, pbw(precision))) 5207 5365 5208 def sqrtn(gen x, n, precision=0):5366 def sqrtn(gen x, n, long precision=0): 5209 5367 r""" 5210 5368 x.sqrtn(n): return the principal branch of the n-th root of x, 5211 5369 i.e., the one such that … … 5265 5423 ans = P.new_gen_noclear(gsqrtn(x.g, t0, &zetan, pbw(precision))) 5266 5424 return ans, P.new_gen(zetan) 5267 5425 5268 def tan(gen x, precision=0):5426 def tan(gen x, long precision=0): 5269 5427 """ 5270 5428 x.tan() - tangent of x 5271 5429 … … 5285 5443 sig_on() 5286 5444 return P.new_gen(gtan(x.g, pbw(precision))) 5287 5445 5288 def tanh(gen x, precision=0):5446 def tanh(gen x, long precision=0): 5289 5447 """ 5290 5448 x.tanh() - hyperbolic tangent of x 5291 5449 … … 5325 5483 sig_on() 5326 5484 return P.new_gen(teich(x.g)) 5327 5485 5328 def theta(gen q, z, precision=0):5486 def theta(gen q, z, long precision=0): 5329 5487 """ 5330 5488 q.theta(z): Jacobi sine theta-function. 5331 5489 … … 5344 5502 sig_on() 5345 5503 return P.new_gen(theta(q.g, t0, pbw(precision))) 5346 5504 5347 def thetanullk(gen q, long k, precision=0):5505 def thetanullk(gen q, long k, long precision=0): 5348 5506 """ 5349 5507 q.thetanullk(k): return the k-th derivative at z=0 of theta(q,z). 5350 5508 … … 5361 5519 sig_on() 5362 5520 return P.new_gen(thetanullk(q.g, k, pbw(precision))) 5363 5521 5364 def weber(gen x, flag=0, precision=0):5522 def weber(gen x, flag=0, long precision=0): 5365 5523 r""" 5366 5524 x.weber(flag=0): One of Weber's f functions of x. flag is optional, 5367 5525 and can be 0: default, function … … 5392 5550 sig_on() 5393 5551 return P.new_gen(weber0(x.g, flag, pbw(precision))) 5394 5552 5395 def zeta(gen s, precision=0):5553 def zeta(gen s, long precision=0): 5396 5554 """ 5397 5555 zeta(s): zeta function at s with s a complex or a p-adic number. 5398 5556 … … 5665 5823 # 5: Elliptic curve functions 5666 5824 ################################################## 5667 5825 5668 def ellinit(self, int flag=0, precision=0):5826 def ellinit(self, int flag=0, long precision=0): 5669 5827 """ 5670 5828 Return the Pari elliptic curve object with Weierstrass coefficients 5671 5829 given by self, a list with 5 elements. … … 6014 6172 return self.new_gen(g) 6015 6173 6016 6174 6017 def ellbil(self, z0, z1 ):6175 def ellbil(self, z0, z1, long precision=0): 6018 6176 """ 6019 6177 e.ellbil(z0, z1): return the value of the canonical bilinear form 6020 6178 on z0 and z1. … … 6036 6194 """ 6037 6195 t0GEN(z0); t1GEN(z1) 6038 6196 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))) 6041 6198 6042 6199 def ellchangecurve(self, ch): 6043 6200 """ … … 6070 6227 sig_on() 6071 6228 return self.new_gen(ellchangecurve(self.g, t0)) 6072 6229 6073 def elleta(self ):6230 def elleta(self, long precision=0): 6074 6231 """ 6075 6232 e.elleta(): return the vector [eta1,eta2] of quasi-periods 6076 6233 associated with the period lattice e.omega() of the elliptic curve … … 6089 6246 True 6090 6247 """ 6091 6248 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): 6096 6252 """ 6097 6253 e.ellheight(a, flag=2): return the global Neron-Tate height of the 6098 6254 point a on the elliptic curve e. … … 6139 6295 sig_on() 6140 6296 return self.new_gen(ellheight0(self.g, t0, flag, pbw(precision))) 6141 6297 6142 def ellheightmatrix(self, x ):6298 def ellheightmatrix(self, x, long precision=0): 6143 6299 """ 6144 6300 e.ellheightmatrix(x): return the height matrix for the vector x of 6145 6301 points on the elliptic curve e. … … 6166 6322 t0GEN(x) 6167 6323 sig_on() 6168 6324 # the argument prec has no effect 6169 return self.new_gen(mathell(self.g, t0, p rec))6325 return self.new_gen(mathell(self.g, t0, pbw(precision))) 6170 6326 6171 6327 def ellisoncurve(self, x): 6172 6328 """ … … 6351 6507 sig_on() 6352 6508 return self.new_gen(elllocalred(self.g, t0)) 6353 6509 6354 def elllseries(self, s, A=1 ):6510 def elllseries(self, s, A=1, long precision=0): 6355 6511 """ 6356 6512 e.elllseries(s, A=1): return the value of the `L`-series of 6357 6513 the elliptic curve e at the complex number s. … … 6391 6547 """ 6392 6548 t0GEN(s); t1GEN(A) 6393 6549 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))) 6396 6551 6397 6552 def ellminimalmodel(self): 6398 6553 """ … … 6463 6618 sig_on() 6464 6619 return self.new_gen(orderell(self.g, t0)) 6465 6620 6466 def ellordinate(self, x ):6621 def ellordinate(self, x, long precision=0): 6467 6622 """ 6468 6623 e.ellordinate(x): return the `y`-coordinates of the points 6469 6624 on the elliptic curve e having x as `x`-coordinate. … … 6492 6647 """ 6493 6648 t0GEN(x) 6494 6649 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))) 6497 6651 6498 6652 def ellpointtoz(self, P, long precision=0): 6499 6653 """ … … 6621 6775 sig_off() 6622 6776 return rootno 6623 6777 6624 def ellsigma(self, z, flag=0 ):6778 def ellsigma(self, z, flag=0, long precision=0): 6625 6779 """ 6626 6780 e.ellsigma(z, flag=0): return the value at the complex point z of 6627 6781 the Weierstrass `\sigma` function associated to the … … 6636 6790 """ 6637 6791 t0GEN(z) 6638 6792 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))) 6641 6794 6642 6795 def ellsub(self, z0, z1): 6643 6796 """ … … 6714 6867 sig_on() 6715 6868 return self.new_gen(elltors0(self.g, flag)) 6716 6869 6717 def ellzeta(self, z ):6870 def ellzeta(self, z, long precision=0): 6718 6871 """ 6719 6872 e.ellzeta(z): return the value at the complex point z of the 6720 6873 Weierstrass `\zeta` function associated with the elliptic … … 6746 6899 """ 6747 6900 t0GEN(z) 6748 6901 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): 6753 6905 """ 6754 6906 e.ellztopoint(z): return the point on the elliptic curve e 6755 6907 corresponding to the complex number z, under the usual complex … … 6780 6932 [0] 6781 6933 """ 6782 6934 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))) 6790 6937 6791 6938 def omega(self): 6792 6939 """ … … 6829 6976 """ 6830 6977 return self[12] 6831 6978 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))) 6839 6982 6840 6983 6841 6984 ########################################### … … 6863 7006 sig_off() 6864 7007 return n 6865 7008 6866 def bnfinit(self, long flag=0, tech=None ):7009 def bnfinit(self, long flag=0, tech=None, long precision=0): 6867 7010 if tech is None: 6868 7011 sig_on() 6869 return P.new_gen(bnfinit0(self.g, flag, <GEN>0, p rec))7012 return P.new_gen(bnfinit0(self.g, flag, <GEN>0, pbw(precision))) 6870 7013 else: 6871 7014 t0GEN(tech) 6872 7015 sig_on() 6873 return P.new_gen(bnfinit0(self.g, flag, t0, p rec))7016 return P.new_gen(bnfinit0(self.g, flag, t0, pbw(precision))) 6874 7017 6875 7018 def bnfisintnorm(self, x): 6876 7019 t0GEN(x) … … 7831 7974 sig_on() 7832 7975 return self.new_gen(reduceddiscsmith(self.g)) 7833 7976 7834 def polgalois(self ):7977 def polgalois(self, long precision=0): 7835 7978 """ 7836 7979 f.polgalois(): Galois group of the polynomial f 7837 7980 """ 7838 7981 sig_on() 7839 return self.new_gen(polgalois(self.g, p rec))7982 return self.new_gen(polgalois(self.g, pbw(precision))) 7840 7983 7841 7984 def nfgaloisconj(self, long flag=0, denom=None, long precision=0): 7842 7985 r""" … … 7941 8084 sig_on() 7942 8085 return self.new_gen(polresultant0(self.g, t0, self.get_var(var), flag)) 7943 8086 7944 def polroots(self, flag=0, precision=0):8087 def polroots(self, flag=0, long precision=0): 7945 8088 """ 7946 8089 polroots(x,flag=0): complex roots of the polynomial x. flag is 7947 8090 optional, and can be 0: default, uses Schonhage's method modified … … 8018 8161 sig_on() 8019 8162 return self.new_gen(recip(self.g)) 8020 8163 8021 def thueinit(self, flag=0 ):8022 sig_on() 8023 return self.new_gen(thueinit(self.g, flag, p rec))8164 def thueinit(self, flag=0, long precision=0): 8165 sig_on() 8166 return self.new_gen(thueinit(self.g, flag, pbw(precision))) 8024 8167 8025 8168 8026 8169 def rnfisnorminit(self, polrel, long flag=2): … … 8846 8989 sig_on() 8847 8990 return self.new_gen(listput(self.g, t0, n)) 8848 8991 8849 8850 8851 def elleisnum(self, long k, int flag=0): 8992 def elleisnum(self, long k, int flag=0, long precision=0): 8852 8993 """ 8853 8994 om.elleisnum(k, flag=0): om=[om1,om2] being a 2-component vector 8854 8995 giving a basis of a lattice L and k an even positive integer, … … 8887 9028 2.15314248576078 E50 8888 9029 """ 8889 9030 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): 8894 9034 """ 8895 9035 Return the value or the series expansion of the Weierstrass 8896 9036 `P`-function at `z` on the lattice `self` (or the lattice … … 8957 9097 """ 8958 9098 t0GEN(z) 8959 9099 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))) 8967 9101 8968 9102 def ellchangepoint(self, y): 8969 9103 """ … … 9061 9195 9062 9196 INPUT: 9063 9197 9064 9065 9198 - ``size`` - long, the number of bytes for the initial 9066 9199 PARI stack (see note below) 9067 9200 9068 9201 - ``maxprime`` - unsigned long, upper limit on a 9069 9202 precomputed prime number table (default: 500000) 9070 9203 9071 9072 9204 .. note:: 9073 9205 9074 In py_pari, the PARI stack is different than in gp or the9075 PARI C library. In Python, instead of the PARI stack9076 holding the results of all computations, it *only* holds9077 the results of an individual computation. Each time a new9078 Python /PARI object is computed, it it copied to its own9079 space in the Python heap, and the memory it occupied on the9080 PARI stack is freed. Thus it is not necessary to make the9081 stack very large. Also, unlike in PARI, if the stack does9082 overflow, in most cases the PARI stack is automatically9083 increased and the relevantstep 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. 9084 9216 9085 9217 This design obviously involves some performance penalties 9086 9218 over the way PARI works, but it scales much better and is … … 9096 9228 if bot: 9097 9229 return # pari already initialized. 9098 9230 9099 global num_primes, avma, top, bot, p rec9231 global num_primes, avma, top, bot, pari_default_prec 9100 9232 9101 9233 # The size here doesn't really matter, because we will allocate 9102 9234 # our own stack anyway. We ask PARI not to set up signal handlers. … … 9110 9242 pari_free(<void*>bot); bot = 0 9111 9243 init_stack(size) 9112 9244 9245 # Set real precision to 15 decimal digits 9113 9246 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)) 9120 9249 9121 9250 # Set printing functions 9122 9251 global pariOut … … 9183 9312 _x = t0heap[i] 9184 9313 return _x.g 9185 9314 9186 # TODO: Refactor code out of __call__ so it...9187 9188 s = str(x)9189 cdef GEN g9190 sig_on()9191 g = gp_read_str(s)9192 sig_off()9193 return g9194 9195 9196 9315 def set_real_precision(self, long n): 9197 9316 """ 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 """ 9212 9367 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 9217 9376 9218 9377 def get_real_precision(self): 9219 9378 """ 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 9230 9395 9231 9396 def set_series_precision(self, long n): 9232 9397 global precdl 9233 9398 precdl = n 9234 9399 9235 9400 def get_series_precision(self): 9401 global precdl 9236 9402 return precdl 9237 9403 9238 9404 … … 9665 9831 return self(x) 9666 9832 except (TypeError, AttributeError): 9667 9833 raise TypeError, "no canonical coercion of %s into PARI"%x 9668 if isinstance(x, gen):9669 return x9670 raise TypeError, "x must be a PARI object"9671 9834 9672 9835 cdef _an_element_c_impl(self): # override this in Cython 9673 9836 return self.PARI_ZERO 9674 9837 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 PARI9678 gen with (at most) precision *bits* of precision.9679 """9680 cdef unsigned long old_prec9681 old_prec = GP_DATA.fmt.sigd9682 precision = prec_bits_to_dec(precision)9683 if not precision:9684 precision = old_prec9685 self.set_real_precision(precision)9686 x = self(s)9687 self.set_real_precision(old_prec)9688 return x9689 9690 9691 9692 9838 cdef long get_var(self, v): 9693 9839 """ 9694 9840 Converts a Python string into a PARI variable reference number. Or … … 9890 10036 self.init_primes(max(2*num_primes,20*n)) 9891 10037 return self.nth_prime(n) 9892 10038 9893 def euler(self, precision=0):10039 def euler(self, long precision=0): 9894 10040 """ 9895 10041 Return Euler's constant to the requested real precision (in bits). 9896 10042 … … 9904 10050 sig_on() 9905 10051 return self.new_gen(mpeuler(pbw(precision))) 9906 10052 9907 def pi(self, precision=0):10053 def pi(self, long precision=0): 9908 10054 """ 9909 10055 Return the value of the constant pi to the requested real precision 9910 10056 (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 """ 2 Convert PARI GENs to/from Sage. 3 4 AUTHORS: 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 1 23 import sage.libs.pari.gen as gen 2 24 from sage.misc.sage_eval import sage_eval 3 25 4 26 from sage.rings.all import * 5 I = ComplexField().gen()6 27 7 28 def pari(x): 8 29 """ … … 30 51 sage: a = pari(1/2); a, a.type() 31 52 (1/2, 't_FRAC') 32 53 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:: 34 57 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) 38 60 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:: 42 71 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) 46 74 47 75 Conversion from matrices is supported, but not from vectors; use 48 76 lists instead:: … … 93 121 94 122 def python(z, locals=None): 95 123 """ 96 Return the closest python/Sage equivalent of the given pariobject.124 Return the closest Python/Sage equivalent of the given PARI object. 97 125 98 126 INPUT: 99 127 100 - `z` -- pariobject128 - ``z`` -- PARI object 101 129 102 - `locals` -- optional dictionary used in fallback cases that103 130 - ``locals`` -- optional dictionary used in fallback cases that 131 involve sage_eval 104 132 105 133 The component parts of a t_COMPLEX may be t_INT, t_REAL, t_INTMOD, 106 134 t_FRAC, t_PADIC. The components need not have the same type … … 108 136 t_REAL(). They are converted as follows: 109 137 110 138 t_INT: ZZ[i] 111 t_FRAC: QQ(i) 139 t_FRAC: QQ(i) 112 140 t_REAL: ComplexField(prec) for equivalent precision 113 141 t_INTMOD, t_PADIC: raise NotImplementedError 114 142 115 EXAMPLES: 143 EXAMPLES:: 144 116 145 sage: a = pari('(3+I)').python(); a 117 146 i + 3 118 147 sage: a.parent() … … 129 158 Rational Field 130 159 131 160 sage: a = pari('1.234').python(); a 132 1.234000000000000000000000000 # 32-bit 133 1.2340000000000000000000000000000000000 # 64-bit 161 1.23400000000000000 134 162 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 137 164 138 165 sage: a = pari('(3+I)/2').python(); a 139 166 1/2*i + 3/2 … … 142 169 143 170 Conversion of complex numbers: the next example is converting from 144 171 an element of the Symbolic Ring, which goes via the string 145 representation and hence the precision is architecture-dependent::172 representation:: 146 173 147 174 sage: I = SR(I) 148 175 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 151 177 sage: type(a) 152 178 <type 'sage.rings.complex_number.ComplexNumber'> 153 179 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 156 181 157 For architecture-independent complex numbers, start from a158 suitable ComplexField: 182 Starting instead from a suitable ComplexField:: 183 159 184 sage: z = pari(CC(1.0+2.0*I)); z 160 185 1.00000000000000 + 2.00000000000000*I 161 186 sage: a=z.python(); a … … 208 233 """ 209 234 t = z.type() 210 235 if t == "t_REAL": 211 return RealField( gen.prec_words_to_bits(z.precision()))(z)236 return RealField(z.precision_bits())(z) 212 237 elif t == "t_FRAC": 213 238 Q = RationalField() 214 239 return Q(z) … … 221 246 if tx in ["t_INTMOD", "t_PADIC"] or ty in ["t_INTMOD", "t_PADIC"]: 222 247 raise NotImplementedError, "No conversion to python available for t_COMPLEX with t_INTMOD or t_PADIC components" 223 248 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() 232 250 R = RealField(prec) 233 251 C = ComplexField(prec) 234 252 return C(R(z.real()), R(z.imag())) 235 253 if tx == "t_FRAC" or ty == "t_FRAC": 236 254 return QuadraticField(-1,'i')([python(c) for c in list(z)]) 237 255 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)]) 239 257 raise NotImplementedError, "No conversion to python available for t_COMPLEX with components %s"%(tx,ty) 240 258 elif t == "t_VEC": 241 259 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 9 9 sage: TestSuite(A).run() 10 10 """ 11 11 12 # ###############################################################################12 #***************************************************************************** 13 13 # Copyright (C) 2005, 2006 William Stein <wstein@gmail.com> 14 14 # 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) 16 # as published by the Free Software Foundation; either version 2 of 17 # the License, or (at your option) any later version. 18 18 # http://www.gnu.org/licenses/ 19 # ###############################################################################19 #***************************************************************************** 20 20 21 21 include "../ext/stdsage.pxi" 22 22 include "../ext/python.pxi" … … 59 59 60 60 def _pari_(self): 61 61 """ 62 Return the P arimatrix corresponding to self.62 Return the PARI matrix corresponding to self. 63 63 64 64 EXAMPLES:: 65 65 … … 77 77 This function preserves precision for entries of inexact type (e.g. 78 78 reals):: 79 79 80 sage: R = RealField( 4) # 4bits of precision80 sage: R = RealField(120) # 120 bits of precision 81 81 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] 84 84 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 89 88 """ 90 89 from sage.libs.pari.gen import pari 91 90 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 2853 2853 from sage.rings.integer_ring import is_IntegerRing 2854 2854 from sage.rings.rational_field import is_RationalField 2855 2855 2856 n = None2857 2858 2856 if is_IntegerModRing(R) or is_IntegerRing(R) or is_RationalField(R): 2859 2857 2860 2858 try: … … 2924 2922 return self._factor_pari_helper(G) 2925 2923 2926 2924 elif is_RealField(R): 2927 n = pari.set_real_precision(int(3.5*R.prec()) + 1)2928 2925 G = list(self._pari_with_name().factor()) 2929 2926 2930 2927 elif sage.rings.complex_double.is_ComplexDoubleField(R): … … 2973 2970 elif sage.rings.complex_field.is_ComplexField(R): 2974 2971 # This is a hack to make the polynomial have complex coefficients, since 2975 2972 # otherwise PARI will factor over RR. 2976 n = pari.set_real_precision(int(3.5*R.prec()) + 1)2977 2973 if self.leading_coefficient() != R.gen(): 2978 2974 G = list((pari(R.gen())*self._pari_with_name()).factor()) 2979 2975 else: … … 2985 2981 if G is None: 2986 2982 raise NotImplementedError 2987 2983 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): 2991 2987 """ 2992 2988 Fix up and normalize a factorization that came from PARI. 2993 2989 … … 3052 3048 F.append((R(unit), ZZ(1))) 3053 3049 unit = R.base_ring().one_element() 3054 3050 3055 if not n is None:3056 pari.set_real_precision(n) # restore precision3057 3051 return Factorization(F, unit) 3058 3052 3059 3053 @coerce_binop -
sage/rings/real_mpfi.pyx
diff --git a/sage/rings/real_mpfi.pyx b/sage/rings/real_mpfi.pyx
a b 2661 2661 2662 2662 # def _complex_number_(self): 2663 2663 # 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)2667 2664 2668 2665 def unique_floor(self): 2669 2666 """ … … 4264 4261 # 0.000000000000000 4265 4262 # sage: R(1).zeta() 4266 4263 # +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(); z4272 # 1.644934066848226436472415167 # 32-bit4273 # 1.6449340668482264364724151666460251892 # 64-bit4274 4275 # Note that the number of bits of precision in the constructor only4276 # affects the internal precision of the pari number, not the number4277 # of digits that gets displayed. To increase that you must4278 # use \code{pari.set_real_precision}.4279 4280 # sage: type(z)4281 # <type 'sage.libs.pari.gen.gen'>4282 # sage: R(z)4283 # 1.644934066848224284 4264 # """ 4285 4265 # cdef RealIntervalFieldElement x 4286 4266 # x = self._new() -
sage/rings/real_mpfr.pyx
diff --git a/sage/rings/real_mpfr.pyx b/sage/rings/real_mpfr.pyx
a b 1187 1187 sage: rt2 1188 1188 1.41421356237310 1189 1189 sage: rt2.python() 1190 1.414213562373095048801688724 # 32-bit 1191 1.4142135623730950488016887242096980786 # 64-bit 1190 1.41421356237309505 1192 1191 sage: rt2.python().prec() 1193 96 # 32-bit 1194 128 # 64-bit 1192 64 1195 1193 sage: pari(rt2.python()) == rt2 1196 1194 True 1197 1195 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 2993 2993 E_pari = E_work.pari_curve(prec=working_prec) 2994 2994 log_pari = E_pari.ellpointtoz(pt_pari, precision=working_prec) 2995 2995 2996 while prec_words_to_bits(log_pari.precision()) < precision:2996 while log_pari.precision_bits() < precision: 2997 2997 # result is not precise enough, re-compute with double 2998 # precision. if the base field is not QQ, this2998 # the precision. if the base field is not QQ, this 2999 2999 # requires modifying the precision of the embedding, 3000 3000 # the curve, and the point 3001 3001 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 632 632 :: 633 633 634 634 sage: E = EllipticCurve('37a1').pari_curve() 635 sage: E[14].python().prec ()635 sage: E[14].python().precision() 636 636 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] 640 639 641 640 This shows that the bug uncovered by trac #4715 is fixed:: 642 641