Ticket #9130: trac_9130_combined.2.patch
File trac_9130_combined.2.patch, 7.4 KB (added by , 8 years ago) 


doc/en/reference/functions.rst
# HG changeset patch # User Karen T. Kohl <ktkohl96@gmail.com> # Date 1326400728 21600 # Node ID aa729a73616e28c3bdf89950090597856a2e61c3 # Parent 92c93226b64f933e0af00bbcbd1a8a79c444f43f * * * Trac 9130: symbolic beta function * * * trac 9130: fix segfault in sage.symbolic.pynac.py_float When the second argument of py_float() is declared as object, Cython assumes that this is initialized and it can do reference counting, etc. Calls to py_float() from pynac cannot provide a valid python object for the second argument. In this case pynac just passes, a NULL pointer to py_float(), which lead to crashes. Changing the declaration from object to PyObject* and checking for NULL fixes the problem. * * * Trac 9130: beta function access with error for complex input * * * Trac 9130: beta function include in radom tests Trac 9130: beta functioninclude in random tests * * * Trac 9130: beta functionadditional comments and examples diff git a/doc/en/reference/functions.rst b/doc/en/reference/functions.rst
a b 9 9 sage/functions/transcendental 10 10 sage/functions/piecewise 11 11 sage/functions/orthogonal_polys 12 sage/functions/other 12 13 sage/functions/special 13 14 sage/functions/wigner 14 15 sage/functions/generalized 15 sage/functions/prime_pi 16 No newline at end of file 16 sage/functions/prime_pi 
sage/functions/all.py
diff git a/sage/functions/all.py b/sage/functions/all.py
a b 15 15 16 16 17 17 18 from other import ( ceil, floor, gamma, psi, factorial, 18 from other import ( ceil, floor, gamma, psi, factorial, beta, 19 19 abs_symbolic, erf, sqrt, 20 20 gamma_inc, incomplete_gamma, 21 21 real_part, real, 
sage/functions/other.py
diff git a/sage/functions/other.py b/sage/functions/other.py
a b 1181 1181 1182 1182 binomial = Function_binomial() 1183 1183 1184 class Function_beta(GinacFunction): 1185 def __init__(self): 1186 r""" 1187 Return the beta function. This is defined by 1188 1189 .. math:: 1190 1191 B(p,q) = \int_0^1 t^{p1}(1t)^{1q} dt 1192 1193 for complex or symbolic input `p` and `q`. 1194 Note that the order of inputs does not matter: `B(p,q)=B(q,p)`. 1195 1196 GiNaC is used to compute `B(p,q)`. However, complex inputs 1197 are not yet handled in general. When GiNaC raises an error on 1198 such inputs, we raise a NotImplementedError. 1199 1200 If either input is 1, GiNaC returns the reciprocal of the 1201 other. In other cases, GiNaC uses one of the following 1202 formulas: 1203 1204 .. math:: 1205 1206 B(p,q) = \Gamma(p)\Gamma(q)/\Gamma(p+q) 1207 1208 or 1209 1210 .. math:: 1211 1212 B(p,q) = (1)^q B(1pq, q). 1213 1214 1215 For numerical inputs, GiNaC uses the formula 1216 1217 .. math:: 1218 1219 B(p,q) = \exp[\log\Gamma(p)+\log\Gamma(q)\log\Gamma(p+q)] 1220 1221 1222 INPUT: 1223 1224  ``p``  number or symbolic expression 1225 1226  ``q``  number or symbolic expression 1227 1228 1229 OUTPUT: number or symbolic expression (if input is symbolic) 1230 1231 EXAMPLES:: 1232 1233 sage: beta(3,2) 1234 1/12 1235 sage: beta(3,1) 1236 1/3 1237 sage: beta(1/2,1/2) 1238 beta(1/2, 1/2) 1239 sage: beta(1,1) 1240 1 1241 sage: beta(1/2,1/2) 1242 0 1243 sage: beta(x/2,3) 1244 beta(1/2*x, 3) 1245 sage: beta(.5,.5) 1246 3.14159265358979 1247 sage: beta(1,2.0+I) 1248 0.400000000000000  0.200000000000000*I 1249 sage: beta(3,x+I) 1250 beta(x + I, 3) 1251 1252 Note that the order of arguments does not matter:: 1253 1254 sage: beta(1/2,3*x) 1255 beta(3*x, 1/2) 1256 1257 The following must be fixed to remain symbolic:: 1258 1259 sage: beta(2,1+5*I) 1260 0.0305039787798408  0.0198938992042440*I 1261 1262 """ 1263 GinacFunction.__init__(self, "beta", nargs=2, 1264 conversions=dict(maxima='beta', mathematica='Beta')) 1265 1266 beta = Function_beta() 1267 1184 1268 def _do_sqrt(x, prec=None, extend=True, all=False): 1185 1269 r""" 1186 1270 Used internally to compute the square root of x. 
sage/libs/ginac/decl.pxi
diff git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
a b 11 11 # we do *not* have to use sig_on() and sig_off(). We do use it a little 12 12 # in the actual pyx code to catch controlc for long running functions. 13 13 14 from cpython cimport PyObject 15 14 16 cdef extern from "ginac_wrap.h": 15 17 void ginac_pyinit_Integer(object) 16 18 void ginac_pyinit_Float(object) … … 431 433 object (*py_integer_from_long)(long int x) except + 432 434 object (*py_integer_from_python_obj)(object x) except + 433 435 434 object (*py_float)(object a, objectparent) except +436 object (*py_float)(object a, PyObject* parent) except + 435 437 object (*py_RDF_from_double)(double x) 436 438 437 439 
sage/symbolic/pynac.pyx
diff git a/sage/symbolic/pynac.pyx b/sage/symbolic/pynac.pyx
a b 1054 1054 """ 1055 1055 return py_is_cinteger(x) 1056 1056 1057 cdef public object py_float(object n, objectparent) except +:1057 cdef public object py_float(object n, PyObject* parent) except +: 1058 1058 """ 1059 1059 Evaluate pynac numeric objects numerically. 1060 1060 … … 1074 1074 sage: type(py_float(1/2, CC)) 1075 1075 <type 'sage.rings.complex_number.ComplexNumber'> 1076 1076 """ 1077 return parent(n) 1077 if parent is not NULL: 1078 return (<object>parent)(n) 1079 else: 1080 try: 1081 return RR(n) 1082 except TypeError: 1083 return CC(n) 1078 1084 1079 1085 def py_float_for_doctests(n, prec): 1080 1086 """ … … 1086 1092 sage: py_float_for_doctests(pi, RealField(80)) 1087 1093 3.1415926535897932384626 1088 1094 """ 1089 return py_float(n, prec)1095 return py_float(n, <PyObject*>prec) 1090 1096 1091 1097 # TODO: Optimize this 1092 1098 from sage.rings.real_double import RDF 
sage/symbolic/random_tests.py
diff git a/sage/symbolic/random_tests.py b/sage/symbolic/random_tests.py
a b 16 16 sage: [f for (one,f,arity) in _mk_full_functions()] 17 17 [Ei, abs, arccos, arccosh, arccot, arccoth, arccsc, arccsch, 18 18 arcsec, arcsech, arcsin, arcsinh, arctan, arctan2, arctanh, 19 b inomial, ceil, conjugate, cos, cosh, cot, coth, csc, csch,19 beta, binomial, ceil, conjugate, cos, cosh, cot, coth, csc, csch, 20 20 dickman_rho, dilog, dirac_delta, elliptic_e, elliptic_ec, 21 21 elliptic_eu, elliptic_f, elliptic_kc, elliptic_pi, erf, exp, 22 22 factorial, floor, heaviside, imag_part, integrate, … … 237 237 (euler_gamma  v3^(e) + (v2  factorial(e/v2))^(((2.85879036573  1.18163393202*I)*v2 + (2.85879036573  1.18163393202*I)*v3)*pi  0.247786879678 + 0.931826724898*I)*arccsc((0.891138386848  0.0936820840629*I)/v1)  (0.553423153995  0.5481180572*I)*v3 + 0.149683576515  0.155746451854*I)*v1 + arccsch(pi + e)*elliptic_f(khinchin*v2, 1.4656989704 + 0.863754357069*I) 238 238 sage: random_expr(5, verbose=True) 239 239 About to apply dirac_delta to [1] 240 About to apply arccsch to [0] 241 About to apply <builtin function add> to [0, arccsch(0)] 242 arccsch(0) 240 About to apply arcsec to [0] 241 About to apply <builtin function add> to [0, arcsec(0)] 242 arcsec(0) 243 243 244 """ 244 245 vars = [(1.0, sage.calculus.calculus.var('v%d' % (n+1))) for n in range(nvars)] 245 246 if ncoeffs is None: