| Home | Libraries | People | FAQ | More

### Riemann Zeta Function

##### Synopsis

    #include <boost/math/special_functions/zeta.hpp>

    namespace boost{ namespace math{

    template <class T>
    calculated-result-type zeta(T z);

    template <class T, class Policy>
    calculated-result-type zeta(T z, const Policy&);

    }} // namespaces

The return type of these functions is computed using the result type calculation rules: the return type is `double` if T is an integer type, and T otherwise.

The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.

##### Description

    template <class T>
    calculated-result-type zeta(T z);

    template <class T, class Policy>
    calculated-result-type zeta(T z, const Policy&);

Returns the zeta function of z:

##### Accuracy

The following table shows the peak errors (in units of epsilon) found on various platforms with various floating point types, along with comparisons to the GSL-1.9 and Cephes libraries. Unless otherwise specified any floating point type that is narrower than the one shown will have effectively zero error.

Table 6.73. Error rates for zeta

| Microsoft Visual C++ version 12.0 Win32 double | GNU C++ version 5.1.0 linux double | GNU C++ version 5.1.0 linux long double | Sun compiler version 0x5130 Sun Solaris long double
--- | --- | --- | --- | ---
Zeta: Random values greater than 1 | Max = 0.836ε (Mean = 0.093ε) | Max = 0ε (Mean = 0ε) (GSL 1.16: Max = 8.69ε (Mean = 1.03ε)) (Cephes: Max = 4.49e+33ε (Mean = 6.85e+32ε) And other failures.) | Max = 0.846ε (Mean = 0.0833ε) (<tr1/cmath>: Max = 5.45ε (Mean = 1ε)) | Max = 0.846ε (Mean = 0.0743ε)
Zeta: Random values less than 1 | Max = 7.03ε (Mean = 2.98ε) | Max = 0ε (Mean = 0ε) (GSL 1.16: Max = 137ε (Mean = 13.8ε)) (Cephes: Max = +INFε (Mean = +INFε) And other failures.) | Max = 7.03ε (Mean = 2.71ε) (<tr1/cmath>: Max = 538ε (Mean = 59.3ε)) | Max = 70.1ε (Mean = 17.1ε)
Zeta: Values close to and greater than 1 | Max = 0.994ε (Mean = 0.421ε) | Max = 0ε (Mean = 0ε) (GSL 1.16: Max = 7.73ε (Mean = 4.07ε)) (Cephes: Max = 6.77e+15ε (Mean = 1.52e+15ε) And other failures.) | Max = 0.995ε (Mean = 0.5ε) (<tr1/cmath>: Max = 1.9e+06ε (Mean = 5.11e+05ε)) | Max = 0.995ε (Mean = 0.5ε)
Zeta: Values close to and less than 1 | Max = 0.991ε (Mean = 0.375ε) | Max = 0ε (Mean = 0ε) (GSL 1.16: Max = 0.991ε (Mean = 0.28ε)) (Cephes: Max = 8.66e+15ε (Mean = 1.9e+15ε) And other failures.) | Max = 0.998ε (Mean = 0.508ε) (<tr1/cmath>: Max = 8.53e+06ε (Mean = 1.87e+06ε)) | Max = 0.998ε (Mean = 0.568ε)
Zeta: Integer arguments | Max = 6.5ε (Mean = 2.17ε) | Max = 0ε (Mean = 0ε) (GSL 1.16: Max = 3.75ε (Mean = 1.1ε)) (Cephes: Max = +INFε (Mean = +INFε) And other failures.) | Max = 9ε (Mean = 3.06ε) (<tr1/cmath>: Max = 70.3ε (Mean = 17.4ε)) | Max = 21ε (Mean = 7.13ε)

##### Testing

The tests for these functions come in two parts: basic sanity checks use spot values calculated using Mathworld's online evaluator, while accuracy checks use high-precision test values calculated at 1000-bit precision with NTL::RR and this implementation. Note that the generic and type-specific versions of these functions use differing implementations internally, so this gives us reasonably independent test data. Using our test data to test other "known good" implementations also provides an additional sanity check.

##### Implementation

All versions of these functions first use the usual reflection formulas to make their arguments positive:

The generic versions of these functions are implemented using the series:

When the significand (mantissa) size is recognised (currently for 53, 64 and 113-bit reals, plus single-precision 24-bit handled via promotion to double) then a series of rational approximations devised by JM are used.

For 0 < z < 1 the approximating form is:

For a rational approximation R(1-z) and a constant C.

For 1 < z < 4 the approximating form is:

For a rational approximation R(n-z) and a constant C and integer n.

For z > 4 the approximating form is:

ζ(z) = 1 + eR(z - n)

For a rational approximation R(z-n) and integer n, note that the accuracy required for R(z-n) is not full machine precision, but an absolute error of: ε/R(0). This saves us quite a few digits when dealing with large z, especially when ε is small.

Finally, there are some special cases for integer arguments, there are closed forms for negative or even integers:

and for positive odd integers we simply cache pre-computed values as these are of great benefit to some infinite series calculations.