Ticket #15095: trac15095.patch
File trac15095.patch, 7.5 KB (added by , 8 years ago) 


sage/combinat/combinat.py
# HG changeset patch # User Eviatar Bach <eviatarbach@gmail.com> # Date 1377800909 25200 # Node ID f910e188eb898dd6614a4d85666358ac0e49e243 # Parent 7757908c07dc5fa01a310b97375336a3d4666cda Trac #15095: Symbolic hurwitz_zeta function diff git a/sage/combinat/combinat.py b/sage/combinat/combinat.py
a b 2131 2131 yield c 2132 2132 i+=1 2133 2133 2134 2135 2136 def hurwitz_zeta(s,x,N):2137 """2138 Returns the value of the `\zeta(s,x)` to `N`2139 decimals, where s and x are real.2140 2141 The Hurwitz zeta function is one of the many zeta functions. It2142 defined as2143 2144 .. math::2145 2146 \zeta(s,x) = \sum_{k=0}^\infty (k+x)^{s}.2147 2148 2149 When `x = 1`, this coincides with Riemann's zeta function.2150 The Dirichlet Lfunctions may be expressed as a linear combination2151 of Hurwitz zeta functions.2152 2153 Note that if you use floating point inputs, then the results may be2154 slightly off.2155 2156 EXAMPLES::2157 2158 sage: hurwitz_zeta(3,1/2,6)2159 8.414390000000002160 sage: hurwitz_zeta(11/10,1/2,6)2161 12.10410000000002162 sage: hurwitz_zeta(11/10,1/2,50)2163 12.103813495683755105709077412966680619033648618092164 2165 REFERENCES:2166 2167  http://en.wikipedia.org/wiki/Hurwitz_zeta_function2168 """2169 maxima.eval('load ("bffac")')2170 s = maxima.eval("bfhzeta (%s,%s,%s)"%(s,x,N))2171 2172 #Handle the case where there is a 'b' in the string2173 #'1.2000b0' means 1.2000 and2174 #'1.2000b1' means 12.0002175 i = s.rfind('b')2176 if i == 1:2177 return sage_eval(s)2178 else:2179 if s[i+1:] == '0':2180 return sage_eval(s[:i])2181 else:2182 return sage_eval(s[:i])*10**sage_eval(s[i+1:])2183 2184 return s ## returns an odd string2185 2186 2187 2134 ##################################################### 2188 2135 #### combinatorial sets/lists 2189 2136 
sage/functions/all.py
diff git a/sage/functions/all.py b/sage/functions/all.py
a b 24 24 from log import (exp, log, ln, polylog, dilog, lambert_w) 25 25 26 26 27 from transcendental import (zeta, zetaderiv, zeta_symmetric, dickman_rho) 27 from transcendental import (zeta, zetaderiv, zeta_symmetric, hurwitz_zeta, 28 dickman_rho) 28 29 29 30 from sage.functions.bessel import (bessel_I, bessel_J, bessel_K, bessel_Y, Bessel) 30 31 
sage/functions/transcendental.py
diff git a/sage/functions/transcendental.py b/sage/functions/transcendental.py
a b 25 25 from sage.gsl.integration import numerical_integral 26 26 from sage.structure.parent import Parent 27 27 from sage.structure.coerce import parent 28 from sage.structure.element import get_coercion_model 28 29 from sage.symbolic.expression import Expression 29 30 from sage.functions.log import exp 30 31 … … 33 34 ZZ, RR, RDF, CDF, prime_range) 34 35 35 36 from sage.symbolic.function import GinacFunction, BuiltinFunction, is_inexact 36 37 import sage.libs.mpmath.utils as mpmath_utils 38 from sage.misc.superseded import deprecation 39 from sage.rings.infinity import unsigned_infinity 40 from sage.functions.other import factorial, psi 41 from sage.combinat.combinat import bernoulli_polynomial 37 42 38 43 CC = complex_field.ComplexField() 39 44 I = CC.gen(0) … … 97 102 98 103 zeta = Function_zeta() 99 104 105 106 class Function_HurwitzZeta(BuiltinFunction): 107 def __init__(self): 108 r""" 109 TESTS:: 110 111 sage: latex(hurwitz_zeta(x, 2)) 112 \zeta\left(x, 2\right) 113 sage: hurwitz_zeta(x, 2)._sympy_() 114 zeta(x, 2) 115 """ 116 BuiltinFunction.__init__(self, 'hurwitz_zeta', nargs=2, 117 conversions=dict(mathematica='HurwitzZeta', 118 sympy='zeta'), 119 latex_name='\zeta') 120 121 def _eval_(self, s, x): 122 r""" 123 TESTS:: 124 125 sage: hurwitz_zeta(x, 1) 126 zeta(x) 127 sage: hurwitz_zeta(4, 3) 128 1/90*pi^4  17/16 129 sage: hurwitz_zeta(4, x) 130 1/5*x^5 + 1/2*x^4  1/3*x^3 + 1/30*x 131 sage: hurwitz_zeta(3, 0.5) 132 8.41439832211716 133 """ 134 co = get_coercion_model().canonical_coercion(s, x)[0] 135 if is_inexact(co) and not isinstance(co, Expression): 136 return self._evalf_(s, x, parent=parent(co)) 137 if x == 1: 138 return zeta(s) 139 if s in ZZ and s > 1: 140 return ((1) ** s) * psi(s  1, x) / factorial(s  1) 141 elif s in ZZ and s < 0: 142 return bernoulli_polynomial(x, s + 1) / (s + 1) 143 else: 144 return 145 146 def _evalf_(self, s, x, parent): 147 r""" 148 TESTS:: 149 150 sage: hurwitz_zeta(11/10, 1/2).n() 151 12.1038134956837 152 sage: hurwitz_zeta(11/10, 1/2).n(100) 153 12.103813495683755105709077413 154 sage: hurwitz_zeta(11/10, 1 + 1j).n() 155 9.85014164287853  1.06139499403981*I 156 """ 157 from mpmath import zeta 158 return mpmath_utils.call(zeta, s, x, parent=parent) 159 160 def _derivative_(self, s, x, diff_param): 161 r""" 162 TESTS:: 163 164 sage: y = var('y') 165 sage: diff(hurwitz_zeta(x, y), y) 166 x*hurwitz_zeta(x + 1, y) 167 """ 168 if diff_param == 1: 169 return s * hurwitz_zeta(s + 1, x) 170 else: 171 raise NotImplementedError('derivative with respect to first ' 172 'argument') 173 174 hurwitz_zeta_func = Function_HurwitzZeta() 175 176 177 def hurwitz_zeta(s, x, prec=None, **kwargs): 178 r""" 179 The Hurwitz zeta function `\zeta(s, x)`, where `s` and `x` are complex. 180 181 The Hurwitz zeta function is one of the many zeta functions. It 182 defined as 183 184 .. math:: 185 186 \zeta(s, x) = \sum_{k=0}^{\infty} (k + x)^{s}. 187 188 189 When `x = 1`, this coincides with Riemann's zeta function. 190 The Dirichlet Lfunctions may be expressed as a linear combination 191 of Hurwitz zeta functions. 192 193 EXAMPLES:: 194 195 sage: hurwitz_zeta(x, 1) 196 zeta(x) 197 sage: hurwitz_zeta(4, 3) 198 1/90*pi^4  17/16 199 sage: hurwitz_zeta(4, x) 200 1/5*x^5 + 1/2*x^4  1/3*x^3 + 1/30*x 201 sage: hurwitz_zeta(3, 1/2).n() 202 8.41439832211716 203 sage: hurwitz_zeta(11/10, 1/2).n() 204 12.1038134956837 205 sage: hurwitz_zeta(3, x).series(x, 60).subs(x=0.5).n() 206 8.41439832211716 207 sage: hurwitz_zeta(3, 0.5) 208 8.41439832211716 209 210 REFERENCES: 211 212  http://en.wikipedia.org/wiki/Hurwitz_zeta_function 213 """ 214 if prec: 215 deprecation(15095, 'the syntax hurwitz_zeta(s, x, prec) has been ' 216 'deprecated. Use hurwitz_zeta(s, x).n(digits=prec) ' 217 'instead.') 218 return hurwitz_zeta_func(s, x).n(digits=prec) 219 return hurwitz_zeta_func(s, x, **kwargs) 220 221 100 222 class Function_zetaderiv(GinacFunction): 101 223 def __init__(self): 102 224 r""" … … 112 234 n 113 235 sage: zetaderiv(n,x) 114 236 zetaderiv(n, x) 237 sage: zetaderiv(1, 4).n() 238 0.0689112658961254 239 sage: import mpmath; mpmath.diff(lambda x: mpmath.zeta(x), 4) 240 mpf('0.068911265896125382') 115 241 116 242 TESTS:: 117 243 … … 123 249 """ 124 250 GinacFunction.__init__(self, "zetaderiv", nargs=2) 125 251 252 def _evalf_(self, n, x, parent): 253 from mpmath import zeta 254 return mpmath_utils.call(zeta, x, 1, n, parent=parent) 255 126 256 zetaderiv = Function_zetaderiv() 127 257 128 258 def zeta_symmetric(s):