@@ -72,8 +72,7 @@ from .convert cimport PyObject_AsGEN, gen_to_integer
72
72
from .pari_instance cimport (prec_bits_to_words, prec_words_to_bits,
73
73
default_bitprec, get_var)
74
74
from .stack cimport (new_gen, new_gen_noclear,
75
- clone_gen, clone_gen_noclear,
76
- clear_stack, reset_avma,
75
+ clone_gen, clear_stack, reset_avma,
77
76
remove_from_pari_stack, move_gens_to_heap)
78
77
from .closure cimport objtoclosure
79
78
@@ -1465,9 +1464,9 @@ cdef class Gen(Gen_base):
1465
1464
def __len__ (self ):
1466
1465
return glength(self .g)
1467
1466
1468
- def __richcmp__ (left , right , int op ):
1467
+ def __richcmp__ (self , right , int op ):
1469
1468
"""
1470
- Compare ``left `` and ``right`` using ``op``.
1469
+ Compare ``self `` and ``right`` using ``op``.
1471
1470
1472
1471
Examples:
1473
1472
@@ -1528,14 +1527,13 @@ cdef class Gen(Gen_base):
1528
1527
>>> pari('O(2)') == 0
1529
1528
True
1530
1529
"""
1531
- cdef Gen t0, t1
1530
+ cdef Gen t1
1532
1531
try :
1533
- t0 = objtogen(left)
1534
1532
t1 = objtogen(right)
1535
1533
except Exception :
1536
1534
return NotImplemented
1537
1535
cdef bint r
1538
- cdef GEN x = t0 .g
1536
+ cdef GEN x = self .g
1539
1537
cdef GEN y = t1.g
1540
1538
sig_on()
1541
1539
if op == Py_EQ:
@@ -1605,7 +1603,8 @@ cdef class Gen(Gen_base):
1605
1603
return r
1606
1604
1607
1605
def __copy__ (self ):
1608
- return clone_gen_noclear(self .g)
1606
+ sig_on()
1607
+ return clone_gen(self .g)
1609
1608
1610
1609
def __oct__ (self ):
1611
1610
"""
@@ -1821,6 +1820,7 @@ cdef class Gen(Gen_base):
1821
1820
>>> pari("[1,2,3]~").python_list()
1822
1821
[1, 2, 3]
1823
1822
"""
1823
+ # TODO: deprecate
1824
1824
cdef long n
1825
1825
cdef Gen t
1826
1826
@@ -2574,7 +2574,7 @@ cdef class Gen(Gen_base):
2574
2574
sig_on()
2575
2575
return new_gen(precision0(x.g, n))
2576
2576
2577
- def round (x , estimate = False ):
2577
+ def round (x , bint estimate = False ):
2578
2578
"""
2579
2579
round(x,estimate=False): If x is a real number, returns x rounded
2580
2580
to the nearest integer (rounding up). If the optional argument
@@ -2695,7 +2695,7 @@ cdef class Gen(Gen_base):
2695
2695
"""
2696
2696
return gsizebyte(x.g)
2697
2697
2698
- def truncate (x , estimate = False ):
2698
+ def truncate (x , bint estimate = False ):
2699
2699
"""
2700
2700
truncate(x,estimate=False): Return the truncation of x. If estimate
2701
2701
is True, also return the number of error bits.
@@ -3192,13 +3192,10 @@ cdef class Gen(Gen_base):
3192
3192
<... 'int'>
3193
3193
"""
3194
3194
sig_on()
3195
- cdef GEN g = ellan(self .g, n)
3196
- if python_ints:
3197
- v = [gtolong(gel(g, i+ 1 )) for i in range (glength(g))]
3198
- clear_stack()
3199
- return v
3200
- else :
3201
- return new_gen(g)
3195
+ cdef Gen g = new_gen(ellan(self .g, n))
3196
+ if not python_ints:
3197
+ return g
3198
+ return [gtolong(gel(g.g, i+ 1 )) for i in range (glength(g.g))]
3202
3199
3203
3200
def ellaplist (self , long n , python_ints = False ):
3204
3201
r """
@@ -3932,56 +3929,7 @@ cdef class Gen(Gen_base):
3932
3929
>>> nf(x='y')
3933
3930
[y^2 + 1, [0, 1], -4, 1, [Mat([1, 0.E-38 + 1.00000000000000*I]), [1, 1.00000000000000; 1, -1.00000000000000], [1, 1; 1, -1], [2, 0; 0, -2], [2, 0; 0, 2], [1, 0; 0, -1], [1, [0, -1; 1, 0]], [2]], [0.E-38 + 1.00000000000000*I], [1, y], [1, 0; 0, 1], [1, 0, 0, -1; 0, 1, 1, 0]]
3934
3931
"""
3935
- cdef long t = typ(self .g)
3936
- cdef Gen t0
3937
- cdef GEN result
3938
- cdef long arity
3939
- cdef long nargs = len (args)
3940
- cdef long nkwds = len (kwds)
3941
-
3942
- # Closure must be evaluated using *args
3943
- if t == t_CLOSURE:
3944
- if nkwds > 0 :
3945
- raise TypeError (" cannot evaluate a PARI closure using keyword arguments" )
3946
- if closure_is_variadic(self .g):
3947
- arity = closure_arity(self .g) - 1
3948
- args = list (args[:arity]) + [0 ]* (arity- nargs) + [args[arity:]]
3949
- t0 = objtogen(args)
3950
- sig_on()
3951
- result = closure_callgenvec(self .g, t0.g)
3952
- if result == gnil:
3953
- clear_stack()
3954
- return None
3955
- return new_gen(result)
3956
-
3957
- # Evaluate univariate polynomials, rational functions and
3958
- # series using *args
3959
- if nargs > 0 :
3960
- if nkwds > 0 :
3961
- raise TypeError (" mixing unnamed and keyword arguments not allowed when evaluating a PARI object" )
3962
- if not (t == t_POL or t == t_RFRAC or t == t_SER):
3963
- raise TypeError (" cannot evaluate PARI %s using unnamed arguments" % self .type())
3964
- if nargs != 1 :
3965
- raise TypeError (" evaluating PARI %s takes exactly 1 argument (%d given)"
3966
- % (self .type(), nargs))
3967
-
3968
- t0 = objtogen(args[0 ])
3969
- sig_on()
3970
- if t == t_POL or t == t_RFRAC:
3971
- return new_gen(poleval(self .g, t0.g))
3972
- else : # t == t_SER
3973
- return new_gen(gsubst(self .g, varn(self .g), t0.g))
3974
-
3975
- # Call substvec() using **kwds
3976
- vstr = iter (kwds.iterkeys()) # Variables as Python strings
3977
- t0 = objtogen(kwds.values()) # Replacements
3978
-
3979
- sig_on()
3980
- cdef GEN v = cgetg(nkwds+ 1 , t_VEC) # Variables as PARI polynomials
3981
- cdef long i
3982
- for i in range (nkwds):
3983
- set_gel(v, i+ 1 , pol_x(get_var(next(vstr))))
3984
- return new_gen(gsubstvec(self .g, v, t0.g))
3932
+ return self (* args, ** kwds)
3985
3933
3986
3934
def __call__ (self , *args , **kwds ):
3987
3935
"""
@@ -4026,7 +3974,57 @@ cdef class Gen(Gen_base):
4026
3974
...
4027
3975
TypeError: cannot evaluate PARI t_INT using unnamed arguments
4028
3976
"""
4029
- return self .eval(* args, ** kwds)
3977
+ cdef long t = typ(self .g)
3978
+ cdef Gen t0
3979
+ cdef GEN result
3980
+ cdef long arity
3981
+ cdef long nargs = len (args)
3982
+ cdef long nkwds = len (kwds)
3983
+
3984
+ # Closure must be evaluated using *args
3985
+ if t == t_CLOSURE:
3986
+ if nkwds:
3987
+ raise TypeError (" cannot evaluate a PARI closure using keyword arguments" )
3988
+ if closure_is_variadic(self .g):
3989
+ arity = closure_arity(self .g) - 1
3990
+ args = list (args[:arity]) + [0 ]* (arity- nargs) + [args[arity:]]
3991
+ t0 = objtogen(args)
3992
+ sig_on()
3993
+ result = closure_callgenvec(self .g, t0.g)
3994
+ if result is gnil:
3995
+ clear_stack()
3996
+ return None
3997
+ return new_gen(result)
3998
+
3999
+ # Evaluate univariate polynomials, rational functions and
4000
+ # series using *args
4001
+ if nargs:
4002
+ if nkwds:
4003
+ raise TypeError (" mixing unnamed and keyword arguments not allowed when evaluating a PARI object" )
4004
+ if not (t == t_POL or t == t_RFRAC or t == t_SER):
4005
+ raise TypeError (" cannot evaluate PARI %s using unnamed arguments" % self .type())
4006
+ if nargs != 1 :
4007
+ raise TypeError (" evaluating PARI %s takes exactly 1 argument (%d given)"
4008
+ % (self .type(), nargs))
4009
+
4010
+ t0 = objtogen(args[0 ])
4011
+ sig_on()
4012
+ if t == t_POL or t == t_RFRAC:
4013
+ return new_gen(poleval(self .g, t0.g))
4014
+ else : # t == t_SER
4015
+ return new_gen(gsubst(self .g, varn(self .g), t0.g))
4016
+
4017
+ # Call substvec() using **kwds
4018
+ cdef list V = [to_bytes(k) for k in kwds] # Variables as Python byte strings
4019
+ t0 = objtogen(kwds.values()) # Replacements
4020
+
4021
+ sig_on()
4022
+ cdef GEN v = cgetg(nkwds+ 1 , t_VEC) # Variables as PARI polynomials
4023
+ cdef long i
4024
+ for i in range (nkwds):
4025
+ varname = < bytes> V[i]
4026
+ set_gel(v, i+ 1 , pol_x(fetch_user_var(varname)))
4027
+ return new_gen(gsubstvec(self .g, v, t0.g))
4030
4028
4031
4029
def factorpadic (self , p , long r = 20 ):
4032
4030
"""
@@ -4275,7 +4273,7 @@ cdef class Gen(Gen_base):
4275
4273
def __abs__ (self ):
4276
4274
return self .abs()
4277
4275
4278
- def nextprime (self , bint add_one = 0 ):
4276
+ def nextprime (self , bint add_one = False ):
4279
4277
"""
4280
4278
nextprime(x): smallest pseudoprime greater than or equal to `x`.
4281
4279
If ``add_one`` is non-zero, return the smallest pseudoprime
@@ -4345,7 +4343,8 @@ cdef class Gen(Gen_base):
4345
4343
if typ(self .g) != t_POL and typ(self .g) != t_SER:
4346
4344
raise TypeError (" set_variable() only works for polynomials or power series" )
4347
4345
# Copy self and then change the variable in place
4348
- newg = clone_gen_noclear(self .g)
4346
+ sig_on()
4347
+ newg = clone_gen(self .g)
4349
4348
setvarn(newg.g, n)
4350
4349
return newg
4351
4350
0 commit comments