1
- // Copyright ©2016 The gonum Authors. All rights reserved.
1
+ // Derived from SciPy's special/cephes/zeta.c
2
+ // https://github.com/scipy/scipy/blob/master/scipy/special/cephes/zeta.c
3
+
2
4
// Use of this source code is governed by a BSD-style
3
5
// license that can be found in the LICENSE file.
4
-
5
- /*
6
- * Cephes Math Library Release 2.1: January, 1989
7
- * Copyright 1984, 1987, 1989 by Stephen L. Moshier
8
- * Direct inquiries to 30 Frost Street, Cambridge, MA 02140
9
- */
6
+ // Copyright ©1984, ©1987 by Stephen L. Moshier
7
+ // Portions Copyright ©2016 The gonum Authors. All rights reserved.
10
8
11
9
package cephes
12
10
13
11
import "math"
14
12
15
- /*
16
- * Adapted from scipy's cephes zeta.c
17
- */
18
-
19
- /*
20
- * Riemann zeta function of two arguments
21
- *
22
- *
23
- * DESCRIPTION:
24
- *
25
- * inf.
26
- * - -x
27
- * zeta(x,q) = > (k+q)
28
- * -
29
- * k=0
30
- *
31
- * where x > 1 and q is not a negative integer or zero.
32
- * The Euler-Maclaurin summation formula is used to obtain
33
- * the expansion
34
- *
35
- * n
36
- * - -x
37
- * zeta(x,q) = > (k+q)
38
- * -
39
- * k=1
40
- *
41
- * 1-x inf. B x(x+1)...(x+2j)
42
- * (n+q) 1 - 2j
43
- * + --------- - ------- + > --------------------
44
- * x-1 x - x+2j+1
45
- * 2(n+q) j=1 (2j)! (n+q)
46
- *
47
- * where the B2j are Bernoulli numbers. Note that (see zetac.c)
48
- * zeta(x,1) = zetac(x) + 1.
49
- *
50
- *
51
- * REFERENCE:
52
- *
53
- * Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals,
54
- * Series, and Products, p. 1073; Academic Press, 1980.
55
- *
56
- */
57
-
58
- /* Expansion coefficients
59
- * for Euler-Maclaurin summation formula
60
- * (2k)! / B2k
61
- * where B2k are Bernoulli numbers
62
- */
13
+ // zetaCoegs are the expansion coefficients for Euler-Maclaurin summation
14
+ // formula:
15
+ // \frac{(2k)!}{B_{2k}}
16
+ // where
17
+ // B_{2k}
18
+ // are Bernoulli numbers.
63
19
var zetaCoefs = []float64 {
64
20
12.0 ,
65
21
- 720.0 ,
66
22
30240.0 ,
67
23
- 1209600.0 ,
68
24
47900160.0 ,
69
- - 1.8924375803183791606e9 , /*1. 307674368e12/691 */
25
+ - 1.307674368e12 / 691 ,
70
26
7.47242496e10 ,
71
- - 2.950130727918164224e12 , /* 1.067062284288e16/3617 */
72
- 1.1646782814350067249e14 , /* 5.109094217170944e18/43867 */
73
- - 4.5979787224074726105e15 , /* 8.028576626982912e20/174611 */
74
- 1.8152105401943546773e17 , /*1. 5511210043330985984e23/854513 */
75
- - 7.1661652561756670113e18 , /* 1.6938241367317436694528e27/236364091 */
27
+ - 1.067062284288e16 / 3617 ,
28
+ 5.109094217170944e18 / 43867 ,
29
+ - 8.028576626982912e20 / 174611 ,
30
+ 1.5511210043330985984e23 / 854513 ,
31
+ - 1.6938241367317436694528e27 / 236364091 ,
76
32
}
77
33
78
- // Zeta calculates the Riemann zeta function of two arguments
34
+ // Zeta computes the Riemann zeta function of two arguments.
35
+ // Zeta(x,q) = \sum_{k=0}^{\infty} (k+q)^{-x}
36
+ // Note that Zeta returns +Inf if x is 1 and will panic if x is less than 1,
37
+ // q is either zero or a negative integer, or q is negative and x is not an
38
+ // integer.
39
+ //
40
+ // The Euler-Maclaurin summation formula is used to obtain the expansion:
41
+ // Zeta(x,q) = \sum_{k=1}^n (k+q)^{-x} + \frac{(n+q)^{1-x}}{x-1} - \frac{1}{2(n+q)^x} + \sum_{j=1}^{\infty} \frac{B_{2j}x(x+1)...(x+2j)}{(2j)! (n+q)^{x+2j+1}}
42
+ // where
43
+ // B_{2j}
44
+ // are Bernoulli numbers.
45
+ //
46
+ // Note that:
47
+ // zeta(x,1) = zetac(x) + 1
48
+ //
49
+ // REFERENCE: Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals, Series,
50
+ // and Products, p. 1073; Academic Press, 1980.
79
51
func Zeta (x , q float64 ) float64 {
80
52
if x == 1 {
81
53
return math .MaxFloat64
@@ -94,20 +66,16 @@ func Zeta(x, q float64) float64 {
94
66
}
95
67
}
96
68
97
- /* Asymptotic expansion
98
- * http://dlmf.nist.gov/25.11#E43
99
- */
69
+ // Asymptotic expansion: http://dlmf.nist.gov/25.11#E43
100
70
if q > 1e8 {
101
71
return (1 / (x - 1 ) + 1 / (2 * q )) * math .Pow (q , 1 - x )
102
72
}
103
73
104
74
// Euler-Maclaurin summation formula
105
75
106
- /* Permit negative q but continue sum until n+q > +9 .
107
- * This case should be handled by a reflection formula.
108
- * If q<0 and x is an integer, there is a relation to
109
- * the polyGamma function.
110
- */
76
+ // Permit negative q but continue sum until n+q > 9. This case should be
77
+ // handled by a reflection formula. If q<0 and x is an integer, there is a
78
+ // relation to the polyGamma function.
111
79
s := math .Pow (q , - x )
112
80
a := q
113
81
i := 0
0 commit comments