| Title: | Fast Stirling Numbers of the Second Kind |
|---|---|
| Description: | Provides efficient tools for calculating Stirling numbers of the second kind and their logarithms. Includes an exact arbitrary-precision implementation using 'gmp' that avoids numerical cancellation, a fast C++ backend with internal caching for log-scale calculations, and Temme's asymptotic approximation for very large inputs. |
| Authors: | Jon Blood [aut, cre] |
| Maintainer: | Jon Blood <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.1 |
| Built: | 2026-06-07 06:13:29 UTC |
| Source: | https://github.com/jblood94/logstirling2 |
Downloads the pre-computed long-double state blocks from GitHub and
saves them to the user's local data directory. Once downloaded,
logStirling2 will automatically detect and use these states
for accelerated calculations.
get_state_data(force = FALSE)get_state_data(force = FALSE)
force |
Logical; if |
Invisible TRUE on success.
Calculates the natural logarithm of Stirling numbers of the second kind,
, which represent the number of ways to partition a set of
elements into non-empty subsets.
logStirling2(n, k = NULL, as.matrix = TRUE, ones = TRUE) logStirling2Temme(n, k = NULL, as.matrix = TRUE, ones = TRUE, twoterms = TRUE)logStirling2(n, k = NULL, as.matrix = TRUE, ones = TRUE) logStirling2Temme(n, k = NULL, as.matrix = TRUE, ones = TRUE, twoterms = TRUE)
n |
Integer vector of set sizes. Coerced to natural numbers (floor). |
k |
Integer vector of subset sizes. Coerced to natural numbers (floor).
If |
as.matrix |
Logical; if |
ones |
Logical; if |
twoterms |
Logical; if |
The function dispatches to one of three C++ routines (Row_C,
All_C, or Mult_C) depending on the sparsity of the input
vector n.
For systems supporting 16-byte long double precision, if , the function automatically searches for pre-computed state blocks.
If found in the user's data directory (tools::R_user_dir), these
blocks are used to dramatically accelerate calculations. If missing, the
full table is computed on-the-fly. If unsupported (e.g., Apple
Silicon/ARM64), the full table is computed using standard double precision.
logStirling2Temme provides a high-speed asymptotic approximation
based on Temme's method, which is functionally identical in interface but
trades exactness for performance at very large .
A numeric matrix or vector containing . For , values are returned as NA_real_.
Temme, N. M. (1993). Asymptotic estimates of Stirling numbers. Studies in Applied Mathematics, 89(3), 233-243.
# 1. Matrix output for specified n and k logStirling2(n = 5:8, k = 2:5, as.matrix = TRUE) # 2. Vector output with 'ones' filtered # This returns only the "non-trivial" values (1 < k < n) logStirling2(n = 8:10, k = NULL, as.matrix = FALSE, ones = FALSE) # 3. Full row with large n s <- logStirling2(n = 1e3, as.matrix = FALSE) length(s) s[10:13] # 4. Temme's asymptotic approximation — fast even for very large n s <- logStirling2Temme(n = 1e5, as.matrix = FALSE) s[1000:1003]# 1. Matrix output for specified n and k logStirling2(n = 5:8, k = 2:5, as.matrix = TRUE) # 2. Vector output with 'ones' filtered # This returns only the "non-trivial" values (1 < k < n) logStirling2(n = 8:10, k = NULL, as.matrix = FALSE, ones = FALSE) # 3. Full row with large n s <- logStirling2(n = 1e3, as.matrix = FALSE) length(s) s[10:13] # 4. Temme's asymptotic approximation — fast even for very large n s <- logStirling2Temme(n = 1e5, as.matrix = FALSE) s[1000:1003]
Calculates the exact value of using bigz integers.
stirling2direct(n, k)stirling2direct(n, k)
n |
Positive integer set size. |
k |
Integer subset size in |
Implements the explicit formula for positive arguments:
This is a "direct" calculation similar to gmp::Stirling2(method =
"direct"), but without cancellation errors for "large" n.
A bigz object.
logStirling2 for log-scale calculations accepting
vectors for n and k.
# Basic usage stirling2direct(5, 3) # Comparison with the log version mapply(\(k) log(stirling2direct(200, k)), 10:20) logStirling2(200, 10:20)# Basic usage stirling2direct(5, 3) # Comparison with the log version mapply(\(k) log(stirling2direct(200, k)), 10:20) logStirling2(200, 10:20)