2021-01-29 06:31:58 -05:00
|
|
|
|
#pragma once
|
|
|
|
|
//
|
2022-04-15 15:33:42 -04:00
|
|
|
|
// FILE: statHelpers.h
|
2021-01-29 06:31:58 -05:00
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-22 05:13:28 -05:00
|
|
|
|
// VERSION: 0.1.8
|
2021-01-29 06:31:58 -05:00
|
|
|
|
// PURPOSE: Arduino library with a number of statistic helper functions.
|
|
|
|
|
// DATE: 2020-07-01
|
|
|
|
|
// URL: https://github.com/RobTillaart/statHelpers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
|
|
2023-11-22 05:13:28 -05:00
|
|
|
|
#define STATHELPERS_LIB_VERSION (F("0.1.8"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// PERMUTATIONS
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t permutations(uint8_t n, uint8_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint64_t permutations64(uint8_t n, uint8_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// can be optimized similar to dfactorial
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double dpermutations(uint8_t n, uint8_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
/*
|
|
|
|
|
http://wordaligned.org/articles/next-permutation snippet
|
|
|
|
|
|
|
|
|
|
As an example consider finding the next permutation of:
|
|
|
|
|
|
|
|
|
|
8342666411
|
|
|
|
|
The longest monotonically decreasing tail is 666411, and the corresponding head is 8342.
|
|
|
|
|
|
|
|
|
|
8342 666411
|
2021-12-28 09:37:03 -05:00
|
|
|
|
666411 is, by definition, reverse-ordered, and cannot be increased by permuting its elements.
|
|
|
|
|
To find the next permutation, we must increase the head; a matter of finding the smallest tail
|
|
|
|
|
element larger than the head’s final 2.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
8342 666411
|
|
|
|
|
Walking back from the end of tail, the first element greater than 2 is 4.
|
|
|
|
|
|
|
|
|
|
8342 666411
|
|
|
|
|
Swap the 2 and the 4
|
|
|
|
|
|
|
|
|
|
8344 666211
|
2022-11-25 13:13:30 -05:00
|
|
|
|
Since head has increased, we now have a greater permutation. To reduce to the next permutation,
|
2021-12-28 09:37:03 -05:00
|
|
|
|
we reverse tail, putting it into increasing order.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
8344 112666
|
|
|
|
|
Join the head and tail back together. The permutation one greater than 8342666411 is 8344112666.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// http://www.nayuki.io/page/next-lexicographical-permutation-algorithm
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
|
|
|
|
|
// b = nextPermutation<char>(array, 100);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
template <typename T>
|
|
|
|
|
bool nextPermutation(T * array, uint16_t size)
|
|
|
|
|
{
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Find longest non-increasing suffix
|
2021-01-29 06:31:58 -05:00
|
|
|
|
int i = size - 1;
|
|
|
|
|
while (i > 0 && array[i - 1] >= array[i]) i--;
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Now i is the head index of the suffix
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Are we at the last permutation already?
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (i <= 0) return false;
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Let array[i - 1] be the pivot
|
|
|
|
|
// Find rightmost element that exceeds the pivot
|
2021-01-29 06:31:58 -05:00
|
|
|
|
int j = size - 1;
|
|
|
|
|
while (array[j] <= array[i - 1])
|
|
|
|
|
j--;
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Now the value array[j] will become the new pivot
|
|
|
|
|
// Assertion: j >= i
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Swap the pivot with j
|
2021-01-29 06:31:58 -05:00
|
|
|
|
T temp = array[i - 1];
|
|
|
|
|
array[i - 1] = array[j];
|
|
|
|
|
array[j] = temp;
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Reverse the suffix
|
2021-01-29 06:31:58 -05:00
|
|
|
|
j = size - 1;
|
|
|
|
|
while (i < j)
|
|
|
|
|
{
|
|
|
|
|
temp = array[i];
|
|
|
|
|
array[i] = array[j];
|
|
|
|
|
array[j] = temp;
|
|
|
|
|
i++;
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// FACTORIAL
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// exact ==> 12!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t factorial(uint8_t n);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// exact ==> 20!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint64_t factorial64(uint8_t n);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// float (4 byte) => 34!
|
|
|
|
|
// double (8 byte) => 170!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double dfactorialReference(uint8_t n);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// FASTER VERSION
|
|
|
|
|
// does part of the math with integers.
|
|
|
|
|
// tested on UNO and ESP32, roughly 3x faster
|
|
|
|
|
// numbers differ slightly in the order of IEEE754 precision => acceptable.
|
|
|
|
|
// 10e-7 for 4 bit float
|
|
|
|
|
// 10e-16 for 8 bit double
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double dfactorial(uint8_t n);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// stirling is an approximation function for factorial(n).
|
|
|
|
|
// it is slower but constant in time.
|
|
|
|
|
// float => 26!
|
|
|
|
|
// double => 143!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double stirling(uint8_t n);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// SEMIFACTORIAL
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// exact ==> 20!!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t semiFactorial(uint8_t n);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// exact ==> 33!!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint64_t semiFactorial64(uint8_t n);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// float (4 byte) => 56!!
|
|
|
|
|
// double (8 byte) => 300!!
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double dSemiFactorial(uint16_t n);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// COMBINATIONS
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// works for n = 0..30 for all k
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t combinations(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// works for n = 0..61 for all k
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint64_t combinations64(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// experimental - not exact but allows large values
|
|
|
|
|
// float (4 bits) works till n = 125 for all k
|
|
|
|
|
// double (8 bits) works till n = 1020 for all k
|
2022-04-15 15:33:42 -04:00
|
|
|
|
double dcombinations(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// recursive (mind your stack and time)
|
|
|
|
|
// works for n = 0..30 for all k
|
|
|
|
|
// educational purpose
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t rcombinations(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// recursive
|
|
|
|
|
// works for n = 0..61 for all k
|
|
|
|
|
// educational purpose
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint64_t rcombinations64(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// very slow double recursive way by means of Pascals triangle.
|
|
|
|
|
// works for n = 0..30 for all k (but takes a lot of time)
|
|
|
|
|
// educational purpose
|
2022-04-15 15:33:42 -04:00
|
|
|
|
uint32_t combPascal(uint16_t n, uint16_t k);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// EXPERIMENTAL
|
2021-08-06 11:19:47 -04:00
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// BIG SECTION
|
|
|
|
|
//
|
|
|
|
|
// keep track of exponent myself in 32 bit unsigned integer
|
|
|
|
|
// - can be extended to a 64 bit integer
|
|
|
|
|
// however it already takes hours to calculate with 32 bits
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
UNO
|
|
|
|
|
n n! millis() from earlier version
|
|
|
|
|
-----------------------------------------
|
|
|
|
|
1 1.00000e0 0
|
|
|
|
|
10 3.62880e6 1
|
|
|
|
|
100 9.33262e157 7
|
|
|
|
|
1000 4.02386e2567 105
|
|
|
|
|
2000 3.31627e5735 231
|
|
|
|
|
4000 1.82880e12673 504
|
|
|
|
|
8000 5.18416e27752 1086
|
|
|
|
|
10000 2.84625e35659 1389
|
|
|
|
|
16000 5.11880e60319 2330
|
|
|
|
|
32000 1.06550e130270 4978
|
|
|
|
|
100000 2.82428e456573 17201
|
|
|
|
|
1000000 8.26379e5565708 206421 (3.5 minutes)
|
|
|
|
|
10000000 an hour? too long...
|
|
|
|
|
|
|
|
|
|
ESP32 240MHz
|
|
|
|
|
n n! millis() from earlier version
|
|
|
|
|
-----------------------------------------
|
|
|
|
|
1 1.00000e0 0
|
|
|
|
|
10 3.62880e6 0
|
|
|
|
|
100 9.33262e157 0
|
2022-04-15 15:33:42 -04:00
|
|
|
|
1e3 4.02387e2567 8
|
|
|
|
|
1e4 2.84626e35659 110
|
|
|
|
|
1e5 2.82423e456573 1390
|
|
|
|
|
1e6 8.26393e5565708 16781
|
|
|
|
|
1e7 1.20242e65657059 196573 (3++ minutes)
|
|
|
|
|
1e8 1.61720e756570556 2253211
|
|
|
|
|
1e9 9.90463e4270738226 25410726 (7++ hrs to detect overflow! :(
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 0
|
|
|
|
|
10 0.6
|
|
|
|
|
100 1.57
|
|
|
|
|
1000 2.567
|
|
|
|
|
10000 3.5659
|
|
|
|
|
100000 4.56573
|
|
|
|
|
1000000 5.565708
|
|
|
|
|
10000000 6.5657059
|
|
|
|
|
100000000 7.56570556
|
|
|
|
|
|
|
|
|
|
// largest found - exponent is approaching max_uint32_t - 4294967296
|
|
|
|
|
518678058! 4.1873547e4294967283 // break condition was hit...
|
|
|
|
|
next one should fit too
|
|
|
|
|
518678059! 2.1718890e4294967292
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigFactorial(uint32_t n, double &mantissa, uint32_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// Should work full range for n = 518678059, k = { 0..n }
|
|
|
|
|
// and n > 518678059 => max k is definitely smaller ==> expect n+1 ==> k-15 at start
|
|
|
|
|
// performance: low. depends on k.
|
2021-08-06 11:19:47 -04:00
|
|
|
|
//
|
|
|
|
|
//
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// for relative small k and large n (multiple orders of magnitude) one can get an estimate
|
|
|
|
|
// P(n,k) ~ raw = log10(n - k/2) * k;
|
|
|
|
|
// exponent = int(raw);
|
|
|
|
|
// mantissa = pow(10, raw - int(raw));
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigPermutations(uint32_t n, uint32_t k, double &mantissa, uint32_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigCombinations(uint32_t n, uint32_t k, double &mantissa, uint32_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2022-11-25 13:13:30 -05:00
|
|
|
|
//
|
|
|
|
|
// EXPERIMENTAL 64 BIT
|
2021-08-06 11:19:47 -04:00
|
|
|
|
//
|
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigFactorial64(uint64_t n, double &mantissa, uint64_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigPermutations64(uint64_t n, uint64_t k, double &mantissa, uint64_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
2022-04-15 15:33:42 -04:00
|
|
|
|
void bigCombinations64(uint64_t n, uint64_t k, double &mantissa, uint64_t &exponent);
|
2021-08-06 11:19:47 -04:00
|
|
|
|
|
|
|
|
|
|
2022-11-25 13:13:30 -05:00
|
|
|
|
// -- END OF FILE --
|
2021-12-28 09:37:03 -05:00
|
|
|
|
|