datasketches-cpp
Loading...
Searching...
No Matches
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#include "fdlibm_log.hpp"
25
26#include <cmath>
27
28namespace datasketches {
29
30template<typename A>
31double HarmonicNumbers<A>::getBitMapEstimate(const int bitVectorLength, const int numBitsSet) {
32 return (bitVectorLength * (harmonicNumber(bitVectorLength) - harmonicNumber(bitVectorLength - numBitsSet)));
33}
34
35static const int NUM_EXACT_HARMONIC_NUMBERS = 25;
36
37static double tableOfExactHarmonicNumbers[] = {
38 0.0, // 0
39 1.0, // 1
40 1.5, // 2
41 11.0 / 6.0, // 3
42 25.0 / 12.0, // 4
43 137.0 / 60.0, // 5
44 49.0 / 20.0, // 6
45 363.0 / 140.0, // 7
46 761.0 / 280.0, // 8
47 7129.0 / 2520.0, // 9
48 7381.0 / 2520.0, // 10
49 83711.0 / 27720.0, // 11
50 86021.0 / 27720.0, // 12
51 1145993.0 / 360360.0, // 13
52 1171733.0 / 360360.0, // 14
53 1195757.0 / 360360.0, // 15
54 2436559.0 / 720720.0, // 16
55 42142223.0 / 12252240.0, // 17
56 14274301.0 / 4084080.0, // 18
57 275295799.0 / 77597520.0, // 19
58 55835135.0 / 15519504.0, // 20
59 18858053.0 / 5173168.0, // 21
60 19093197.0 / 5173168.0, // 22
61 444316699.0 / 118982864.0, // 23
62 1347822955.0 / 356948592.0 // 24
63 };
64
65static const double EULER_MASCHERONI_CONSTANT = 0.577215664901532860606512090082;
66
67template<typename A>
68double HarmonicNumbers<A>::harmonicNumber(const uint64_t x_i) {
69 if (x_i < NUM_EXACT_HARMONIC_NUMBERS) {
70 return tableOfExactHarmonicNumbers[x_i];
71 } else {
72 double x = static_cast<double>(x_i);
73 double invSq = 1.0 / (x * x);
74 double sum = fdlibm::log(x) + EULER_MASCHERONI_CONSTANT + (1.0 / (2.0 * x));
75 /* note: the number of terms included from this series expansion is appropriate
76 for the size of the exact table (25) and the precision of doubles */
77 double pow = invSq; // now n^-2
78 sum -= pow * (1.0 / 12.0);
79 pow *= invSq; // now n^-4
80 sum += pow * (1.0 / 120.0);
81 pow *= invSq; /* now n^-6 */
82 sum -= pow * (1.0 / 252.0);
83 pow *= invSq; /* now n^-8 */
84 sum += pow * (1.0 / 240.0);
85 return sum;
86 }
87}
88
89}
90
91#endif // _HARMONICNUMBERS_INTERNAL_HPP_
DataSketches namespace.
Definition binomial_bounds.hpp:38