zeta.c
Go to the documentation of this file.
1 /* zeta.c
2  *
3  * Riemann zeta function of two arguments
4  *
5  *
6  *
7  * SYNOPSIS:
8  *
9  * double x, q, y, zeta();
10  *
11  * y = zeta( x, q );
12  *
13  *
14  *
15  * DESCRIPTION:
16  *
17  *
18  *
19  * inf.
20  * - -x
21  * zeta(x,q) = > (k+q)
22  * -
23  * k=0
24  *
25  * where x > 1 and q is not a negative integer or zero.
26  * The Euler-Maclaurin summation formula is used to obtain
27  * the expansion
28  *
29  * n
30  * - -x
31  * zeta(x,q) = > (k+q)
32  * -
33  * k=1
34  *
35  * 1-x inf. B x(x+1)...(x+2j)
36  * (n+q) 1 - 2j
37  * + --------- - ------- + > --------------------
38  * x-1 x - x+2j+1
39  * 2(n+q) j=1 (2j)! (n+q)
40  *
41  * where the B2j are Bernoulli numbers. Note that (see zetac.c)
42  * zeta(x,1) = zetac(x) + 1.
43  *
44  *
45  *
46  * ACCURACY:
47  *
48  *
49  *
50  * REFERENCE:
51  *
52  * Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals,
53  * Series, and Products, p. 1073; Academic Press, 1980.
54  *
55  */
56 
57 /*
58  * Cephes Math Library Release 2.0: April, 1987
59  * Copyright 1984, 1987 by Stephen L. Moshier
60  * Direct inquiries to 30 Frost Street, Cambridge, MA 02140
61  */
62 
63 #include "mconf.h"
64 extern double MACHEP;
65 
66 /* Expansion coefficients
67  * for Euler-Maclaurin summation formula
68  * (2k)! / B2k
69  * where B2k are Bernoulli numbers
70  */
71 static double A[] = {
72  12.0,
73  -720.0,
74  30240.0,
75  -1209600.0,
76  47900160.0,
77  -1.8924375803183791606e9, /*1.307674368e12/691 */
78  7.47242496e10,
79  -2.950130727918164224e12, /*1.067062284288e16/3617 */
80  1.1646782814350067249e14, /*5.109094217170944e18/43867 */
81  -4.5979787224074726105e15, /*8.028576626982912e20/174611 */
82  1.8152105401943546773e17, /*1.5511210043330985984e23/854513 */
83  -7.1661652561756670113e18 /*1.6938241367317436694528e27/236364091 */
84 };
85 
86 /* 30 Nov 86 -- error in third coefficient fixed */
87 
88 
89 double zeta(double x, double q)
90 {
91  int i;
92  double a, b, k, s, t, w;
93 
94  if (x == 1.0)
95  goto retinf;
96 
97  if (x < 1.0) {
98  domerr:
99  sf_error("zeta", SF_ERROR_DOMAIN, NULL);
100  return (NAN);
101  }
102 
103  if (q <= 0.0) {
104  if (q == floor(q)) {
105  sf_error("zeta", SF_ERROR_SINGULAR, NULL);
106  retinf:
107  return (INFINITY);
108  }
109  if (x != floor(x))
110  goto domerr; /* because q^-x not defined */
111  }
112 
113  /* Asymptotic expansion
114  * https://dlmf.nist.gov/25.11#E43
115  */
116  if (q > 1e8) {
117  return (1/(x - 1) + 1/(2*q)) * pow(q, 1 - x);
118  }
119 
120  /* Euler-Maclaurin summation formula */
121 
122  /* Permit negative q but continue sum until n+q > +9 .
123  * This case should be handled by a reflection formula.
124  * If q<0 and x is an integer, there is a relation to
125  * the polyGamma function.
126  */
127  s = pow(q, -x);
128  a = q;
129  i = 0;
130  b = 0.0;
131  while ((i < 9) || (a <= 9.0)) {
132  i += 1;
133  a += 1.0;
134  b = pow(a, -x);
135  s += b;
136  if (fabs(b / s) < MACHEP)
137  goto done;
138  }
139 
140  w = a;
141  s += b * w / (x - 1.0);
142  s -= 0.5 * b;
143  a = 1.0;
144  k = 0.0;
145  for (i = 0; i < 12; i++) {
146  a *= x + k;
147  b /= w;
148  t = a * b / A[i];
149  s = s + t;
150  t = fabs(t / s);
151  if (t < MACHEP)
152  goto done;
153  k += 1.0;
154  a *= x + k;
155  b /= w;
156  k += 1.0;
157  }
158 done:
159  return (s);
160 }
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
s
RealScalar s
Definition: level1_cplx_impl.h:126
b
Scalar * b
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
MACHEP
double MACHEP
Definition: const.c:54
boost::multiprecision::fabs
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:119
A
Definition: test_numpy_dtypes.cpp:298
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
Eigen::ArrayBase::pow
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_pow_op< typename Derived::Scalar, typename ExponentDerived::Scalar >, const Derived, const ExponentDerived > pow(const Eigen::ArrayBase< Derived > &x, const Eigen::ArrayBase< ExponentDerived > &exponents)
Definition: GlobalFunctions.h:143
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
mconf.h
zeta
double zeta(double x, double q)
Definition: zeta.c:89
sf_error
void sf_error(const char *func_name, sf_error_t code, const char *fmt,...)
Definition: sf_error.c:41
SF_ERROR_SINGULAR
@ SF_ERROR_SINGULAR
Definition: sf_error.h:10
NULL
#define NULL
Definition: ccolamd.c:609
align_3::t
Point2 t(10, 10)
SF_ERROR_DOMAIN
@ SF_ERROR_DOMAIN
Definition: sf_error.h:16
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
floor
const EIGEN_DEVICE_FUNC FloorReturnType floor() const
Definition: ArrayCwiseUnaryOps.h:481


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:08:34