datasketches-cpp
HarmonicNumbers-internal.hpp
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _HARMONICNUMBERS_INTERNAL_HPP_
21 #define _HARMONICNUMBERS_INTERNAL_HPP_
22 
23 #include "HarmonicNumbers.hpp"
24 
25 #include <cmath>
26 
27 namespace datasketches {
28 
29 template<typename A>
30 double HarmonicNumbers<A>::getBitMapEstimate(const int bitVectorLength, const int numBitsSet) {
31  return (bitVectorLength * (harmonicNumber(bitVectorLength) - harmonicNumber(bitVectorLength - numBitsSet)));
32 }
33 
34 static const int NUM_EXACT_HARMONIC_NUMBERS = 25;
35 
36 static double tableOfExactHarmonicNumbers[] = {
37  0.0, // 0
38  1.0, // 1
39  1.5, // 2
40  11.0 / 6.0, // 3
41  25.0 / 12.0, // 4
42  137.0 / 60.0, // 5
43  49.0 / 20.0, // 6
44  363.0 / 140.0, // 7
45  761.0 / 280.0, // 8
46  7129.0 / 2520.0, // 9
47  7381.0 / 2520.0, // 10
48  83711.0 / 27720.0, // 11
49  86021.0 / 27720.0, // 12
50  1145993.0 / 360360.0, // 13
51  1171733.0 / 360360.0, // 14
52  1195757.0 / 360360.0, // 15
53  2436559.0 / 720720.0, // 16
54  42142223.0 / 12252240.0, // 17
55  14274301.0 / 4084080.0, // 18
56  275295799.0 / 77597520.0, // 19
57  55835135.0 / 15519504.0, // 20
58  18858053.0 / 5173168.0, // 21
59  19093197.0 / 5173168.0, // 22
60  444316699.0 / 118982864.0, // 23
61  1347822955.0 / 356948592.0 // 24
62  };
63 
64 static const double EULER_MASCHERONI_CONSTANT = 0.577215664901532860606512090082;
65 
66 template<typename A>
67 double HarmonicNumbers<A>::harmonicNumber(const uint64_t x_i) {
68  if (x_i < NUM_EXACT_HARMONIC_NUMBERS) {
69  return tableOfExactHarmonicNumbers[x_i];
70  } else {
71  double x = static_cast<double>(x_i);
72  double invSq = 1.0 / (x * x);
73  double sum = log(x) + EULER_MASCHERONI_CONSTANT + (1.0 / (2.0 * x));
74  /* note: the number of terms included from this series expansion is appropriate
75  for the size of the exact table (25) and the precision of doubles */
76  double pow = invSq; // now n^-2
77  sum -= pow * (1.0 / 12.0);
78  pow *= invSq; // now n^-4
79  sum += pow * (1.0 / 120.0);
80  pow *= invSq; /* now n^-6 */
81  sum -= pow * (1.0 / 252.0);
82  pow *= invSq; /* now n^-8 */
83  sum += pow * (1.0 / 240.0);
84  return sum;
85  }
86 }
87 
88 }
89 
90 #endif // _HARMONICNUMBERS_INTERNAL_HPP_
DataSketches namespace.
Definition: binomial_bounds.hpp:38