### partitionsSample with non-standard setup Source: https://jwood000.github.io/RcppAlgos/articles/CombinatorialSampling.html Demonstrates partitionsSample with a non-standard input vector and a target sum, showing its flexibility. ```R partitionsSample(17 + (1:10) * 3, 10, TRUE, target = 320, n = 3, seed = 111) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #> [1,] 23 23 26 26 29 29 38 38 41 47 #> [2,] 26 26 26 29 29 29 32 41 41 41 #> [3,] 20 23 23 26 26 35 38 41 44 44 ``` -------------------------------- ### Example Usage Source: https://jwood000.github.io/RcppAlgos/reference/expandGridIterator.html Demonstrates how to use the expandGridIter function and its methods with sample data. ```APIDOC ## Examples ```R a = expandGridIter(factor(state.abb), euro, islands) a@nextIter() #> Var1 Var2 Var3 #> 1 AL 13.7603 11506 a@nextNIter(3) #> Var1 Var2 Var3 #> 1 AL 13.7603 5500 #> 2 AL 13.7603 16988 #> 3 AL 13.7603 2968 a@front() #> Var1 Var2 Var3 #> 1 AL 13.7603 11506 all_remaining = a@nextRemaining() dim(all_remaining) #> [1] 26399 3 a@summary() #> $description #> [1] "Cartesian Product of the source (see the sourceVector method for more info)" #> #> $currentIndex #> [1] 26401 #> #> $totalResults #> [1] 26400 #> #> $totalRemaining #> [1] -1 #> a@back() #> Var1 Var2 Var3 #> 1 WY 200.482 82 a[[5]] #> Var1 Var2 Var3 #> 1 AL 13.7603 16 a@summary() #> $description #> [1] "Cartesian Product of the source (see the sourceVector method for more info)" #> #> $currentIndex #> [1] 5 #> #> $totalResults #> [1] 26400 #> #> $totalRemaining #> [1] 26395 #> a[[c(1, 17, 3)]] #> Var1 Var2 Var3 #> 1 AL 13.7603 11506 #> 2 AL 13.7603 13 #> 3 AL 13.7603 16988 a@summary() #> $description #> [1] "Cartesian Product of the source (see the sourceVector method for more info)" #> #> $currentIndex #> [1] 5 #> #> $totalResults #> [1] 26400 #> #> $totalRemaining #> [1] 26395 #> ``` ``` -------------------------------- ### Example of Brute-force Group Partitioning Source: https://jwood000.github.io/RcppAlgos/articles/OtherCombinatorics.html This example demonstrates the output of the `funBruteGrp` function, showing how a vector is partitioned into 3 groups of size 4. The output illustrates permutations within the last group. ```R funBruteGrp(myUp = 6) ``` -------------------------------- ### Subset Sum with Product Constraint Source: https://jwood000.github.io/RcppAlgos/articles/SubsetSum.html This example demonstrates using comboGeneral with a product constraint. A helper function `getAllThenFilter` is defined to first get all combinations and then filter them, which is then compared against the optimized `comboGeneral` for performance. ```R getAllThenFilter <- function(n, m, lim) { t <- comboGeneral(n, m, constraintFun = "prod") t[t[, m + 1] == lim, -(m+1)] } ``` ```R library(microbenchmark) microbenchmark(optimized = comboGeneral(25, 10, constraintFun = "prod", comparisonFun = "==", limitConstraints = 1037836800), brute = getAllThenFilter(25, 10, 1037836800), times = 20, unit = "relative", check = "equal") ``` -------------------------------- ### Partition Iterators and Navigation Source: https://jwood000.github.io/RcppAlgos/reference/RcppAlgos-package.html Shows how to initialize and navigate partition iterators. This includes starting iteration from a specific index and advancing the iterator. ```R p_iter = partitionsIter(5000, 100, target = 6000) p_iter[[1e9]] ## start iterating from index = 1e9 p_iter@nextIter() p_iter@nextNIter(1e3) ``` -------------------------------- ### Benchmark Compositions with Repetition Source: https://jwood000.github.io/RcppAlgos/articles/HighPerformanceBenchmarks.html Compares RcppAlgos with 'arrangements' and 'partitions' for generating compositions with repetition. This is a small case example to illustrate the setup. ```R t1 <- compositionsGeneral(0:15, repetition = TRUE) t2 <- arrangements::compositions(15) t3 <- partitions::compositions(15) # Each package has different output formats... we only examine dimensions ``` -------------------------------- ### Iterating Over Partitions Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Demonstrates how to iterate over partitions of a number using partitionsIter. Shows how to get the next iteration, current iteration, and summary statistics. Random access is supported, but previous iteration is not. ```R p = partitionsIter(16, 4) p@nextIter() #> [1] 1 2 3 10 p@nextIter() #> [1] 1 2 4 9 iter = p@currIter() i = 1 while (!is.null(iter)) { cat(i, " ", iter, "\n") iter = p@nextIter() i = i + 1 } #> 1 1 2 4 9 #> 2 1 2 5 8 #> 3 1 2 6 7 #> 4 1 3 4 8 #> 5 1 3 5 7 #> 6 1 4 5 6 #> 7 2 3 4 7 #> 8 2 3 5 6 #> No more results. ``` ```R partitionsGeneral(16, 4, lower = 2) #> [,1] [,2] [,3] [,4] #> [1,] 1 2 4 9 #> [2,] 1 2 5 8 #> [3,] 1 2 6 7 #> [4,] 1 3 4 8 #> [5,] 1 3 5 7 #> [6,] 1 4 5 6 #> [7,] 2 3 4 7 #> [8,] 2 3 5 6 ``` ```R p@summary() #> $description #> [1] "Partitions of 16 into 4 parts" #> #> $currentIndex #> [1] 10 #> #> $totalResults #> [1] 9 #> #> $totalRemaining #> [1] -1 ``` ```R p[[7]] #> [1] 1 4 5 6 ``` ```R p@prevIter() #> Error: no slot of name "prevIter" for this object of class "Partitions" ``` -------------------------------- ### Forward Iteration and Summary Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Demonstrates how to get the next combinations and retrieve summary information about the iterator's state. ```APIDOC ## Get the remaining combinations with nextRemaining a$nextRemaining() #> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 1 4 5 #> [3,] 2 3 4 #> [4,] 2 3 5 #> [5,] 2 4 5 #> [6,] 3 4 5 a$summary() #> $description #> [1] "Combinations of 5 choose 3" #> #> $currentIndex #> [1] 11 #> #> $totalResults #> [1] 10 #> #> $totalRemaining #> [1] -1 ``` -------------------------------- ### Example Usage of stdThreadMax Source: https://jwood000.github.io/RcppAlgos/reference/stdThreadMax.html Demonstrates the typical output of stdThreadMax(). The returned value is a hint and may vary across systems. ```R stdThreadMax() #> [1] 10 ``` -------------------------------- ### Partition Vector into Groups with comboGroups Source: https://jwood000.github.io/RcppAlgos/index.html Efficiently partitions a vector into specified group sizes using RcppAlgos::comboGroups. This example finds all possible partitions of players into groups of size 2 and 3. ```R players <- c("Ross", "Bobby", "Max", "Casper", "Jake") comboGroups(players, grpSizes = c(2, 3)) ``` -------------------------------- ### Permutations with Repetition (Fibonacci Example) Source: https://jwood000.github.io/RcppAlgos/articles/GeneralCombinatorics.html Generates permutations with repetition using a sequence of Fibonacci numbers. Shows that the class of the output matches the input. ```R fibonacci <- c(1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L) permsFib <- permuteGeneral(fibonacci, 5, TRUE) ht(permsFib) ``` ```R #> head --> #> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 1 1 1 1 #> [2,] 1 1 1 1 2 #> [3,] 1 1 1 1 3 #> [4,] 1 1 1 1 5 #> [5,] 1 1 1 1 8 #> -------- #> tail --> #> [,1] [,2] [,3] [,4] [,5] #> [32764,] 34 34 34 34 5 #> [32765,] 34 34 34 34 8 #> [32766,] 34 34 34 34 13 #> [32767,] 34 34 34 34 21 #> [32768,] 34 34 34 34 34 ## N.B. class is preserved class(fibonacci) #> [1] "integer" class(permsFib[1, ]) #> [1] "integer" ``` -------------------------------- ### Iterating Over Compositions Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Demonstrates how to iterate over compositions of a number using compositionsIter. Shows how to get the next iteration, current iteration, and summary statistics. Random access is supported, but previous iteration is not. ```R p = compositionsIter(6, 3, TRUE) p@nextIter() #> [1] 1 1 4 p@nextIter() #> [1] 1 2 3 iter = p@currIter() i = 1 while (!is.null(iter)) { cat(i, " ", iter, "\n") iter = p@nextIter() i = i + 1 } #> 1 1 2 3 #> 2 1 3 2 #> 3 1 4 1 #> 4 2 1 3 #> 5 2 2 2 #> 6 2 3 1 #> 7 3 1 2 #> 8 3 2 1 #> 9 4 1 1 #> No more results. ``` ```R compositionsGeneral(6, 3, TRUE, lower = 2) #> [,1] [,2] [,3] #> [1,] 1 2 3 #> [2,] 1 3 2 #> [3,] 1 4 1 #> [4,] 2 1 3 #> [5,] 2 2 2 #> [6,] 2 3 1 #> [7,] 3 1 2 #> [8,] 3 2 1 #> [9,] 4 1 1 ``` ```R p@summary() #> $description #> [1] "Compositions with repetition of 6 into 3 parts" #> #> $currentIndex #> [1] 11 #> #> $totalResults #> [1] 10 #> #> $totalRemaining #> [1] -1 ``` ```R p[[7]] #> [1] 2 3 1 ``` ```R p@prevIter() #> Error: no slot of name "prevIter" for this object of class "Partitions" ``` -------------------------------- ### Iterator Summary and Back Access Source: https://jwood000.github.io/RcppAlgos/reference/comboGroupsIterator.html Shows how to get a summary of the iterator's state using summary() and access the last element of the current partition using back(). ```R a@summary() #> $description #> [1] "Partition of v of length 12 into 3 uniform groups" #> #> $currentIndex #> [1] 5776 #> #> $totalResults #> [1] 5775 #> #> $totalRemaining #> [1] -1 #> a@back() #> Grp1 Grp1 Grp1 Grp1 Grp2 Grp2 Grp2 Grp2 Grp3 Grp3 Grp3 Grp3 #> 1 10 11 12 2 7 8 9 3 4 5 6 ``` -------------------------------- ### Basic Usage of comboGeneral with Constraint Function Source: https://jwood000.github.io/RcppAlgos/articles/CombPermConstraints.html Demonstrates using `comboGeneral` with the `constraintFun` parameter set to 'sum'. An additional column is appended to the output, showing the sum of each combination. This example also shows parallel processing and compares performance against base R's `combn`. ```R library(RcppAlgos) options(width = 90) packageVersion("RcppAlgos") #> [1] '2.10.0' cat(paste(capture.output(sessionInfo())[1:3], collapse = "\n")) #> R version 4.5.2 (2025-10-31) #> Platform: aarch64-apple-darwin20 #> Running under: macOS Sequoia 15.7.4 ## base R using combn and FUN combnSum = combn(20, 10, sum) algosSum = comboGeneral(20, 10, constraintFun = "sum") ## Notice the additional column (i.e. the 11th column) head(algosSum) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] #> [1,] 1 2 3 4 5 6 7 8 9 10 55 #> [2,] 1 2 3 4 5 6 7 8 9 11 56 #> [3,] 1 2 3 4 5 6 7 8 9 12 57 #> [4,] 1 2 3 4 5 6 7 8 9 13 58 #> [5,] 1 2 3 4 5 6 7 8 9 14 59 #> [6,] 1 2 3 4 5 6 7 8 9 15 60 identical(as.integer(combnSum), algosSum[,11]) #> [1] TRUE ## Using parallel paralSum = comboGeneral(20, 10, constraintFun = "sum", Parallel = TRUE) identical(paralSum, algosSum) #> [1] TRUE library(microbenchmark) microbenchmark(serial = comboGeneral(20, 10, constraintFun = "sum"), parallel = comboGeneral(20, 10, constraintFun = "sum", Parallel = TRUE), combnSum = combn(20, 10, sum), unit = "relative") #> Warning in microbenchmark(serial = comboGeneral(20, 10, constraintFun = "sum"), : less #> accurate nanosecond times to avoid potential integer overflows #> Unit: relative #> expr min lq mean median uq max neval #> serial 3.622962 3.609644 3.249072 3.497137 3.418335 2.047686 100 #> parallel 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 100 #> combnSum 162.354728 154.733496 135.879799 147.380454 139.765227 81.747219 100 ``` -------------------------------- ### Partition 22 using table method with multiplicities Source: https://jwood000.github.io/RcppAlgos/articles/IntegerPartitions.html Achieves the same result as the previous example by utilizing the `table` method for specifying multiplicities. This offers an alternative way to define part constraints. ```R partitionsGeneral(table(c(rep(0L, 4), 1:8)), 6, target = 22) ``` -------------------------------- ### Reset Iterator and Get Total Results Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Resets the iterator to the beginning and retrieves the total number of possible combinations. This is a common starting point for iterating. ```R a$startOver() ## How many total combinations do we have? a$summary()$totalResults #> [1] 10 ``` -------------------------------- ### Initialize partitionsIter with table method Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Initializes a partitions iterator using a table as input for the source vector. ```R partitionsIter( v, m = NULL, target = NULL, nThreads = NULL, tolerance = NULL, ... ) ``` -------------------------------- ### Install RcppAlgos Package Source: https://jwood000.github.io/RcppAlgos/index.html Install the RcppAlgos package from CRAN or the development version from GitHub. ```r install.packages("RcppAlgos") ## install the development version devtools::install_github("jwood000/RcppAlgos") ``` -------------------------------- ### Get Max Concurrent Threads Source: https://jwood000.github.io/RcppAlgos/reference/stdThreadMax.html Call stdThreadMax() to get the number of concurrent threads supported by the system. If the value cannot be determined, it defaults to 1L. ```R stdThreadMax() ``` -------------------------------- ### Generate Combinations Starting from a Specific Index Source: https://jwood000.github.io/RcppAlgos/reference/comboGroups.html Generate combinations using comboGroups, but start the output from a specific index. This is useful for paginating or retrieving a subset of combinations. ```R total = comboGroupsCount(11, grpSizes = c(3, 3, 5)) ``` ```R comboGroups(11, grpSizes = c(3, 3, 5), lower = total - 20) ``` -------------------------------- ### Generate Partitions with a Step of 24 Source: https://jwood000.github.io/RcppAlgos/articles/OtherCombinatorics.html Illustrates generating partitions by stepping through a sequence with an interval of 24, demonstrating potential duplication issues. This approach is less efficient than dedicated algorithms. ```R do.call(rbind, lapply(seq(1, 169, 24), function(x) { funBruteGrp(myLow = x, myUp = x) })) #> Grp1 Grp2 Grp3 #> 1 "( 1 2 3 4 )" "( 5 6 7 8 )" "( 9 10 11 12 )" #> 25 "( 1 2 3 4 )" "( 5 6 7 9 )" "( 8 10 11 12 )" #> 49 "( 1 2 3 4 )" "( 5 6 7 10 )" "( 8 9 11 12 )" #> 73 "( 1 2 3 4 )" "( 5 6 7 11 )" "( 8 9 10 12 )" #> 97 "( 1 2 3 4 )" "( 5 6 7 12 )" "( 8 9 10 11 )" #> 121 "( 1 2 3 4 )" "( 5 6 8 7 )" "( 9 10 11 12 )" ### <-- This is the same as the 1st #> 145 "( 1 2 3 4 )" "( 5 6 8 9 )" "( 7 10 11 12 )" ### partition. The only difference is #> 169 "( 1 2 3 4 )" "( 5 6 8 10 )" "( 7 9 11 12 )" ### that the 2nd Grp has been permuted ``` -------------------------------- ### RcppAlgos partitionsSample and compositionsSample Usage Source: https://jwood000.github.io/RcppAlgos/reference/partitionsSample.html Demonstrates the basic usage of the `partitionsSample` and `compositionsSample` functions, including their default S3 methods. ```R partitionsSample(v, m = NULL, ...) compositionsSample(v, m = NULL, ...) # Default S3 method partitionsSample( v, m = NULL, repetition = FALSE, freqs = NULL, target = NULL, n = NULL, sampleVec = NULL, seed = NULL, nThreads = NULL, namedSample = FALSE, ... ) # Default S3 method compositionsSample( v, m = NULL, repetition = FALSE, freqs = NULL, target = NULL, weak = FALSE, n = NULL, sampleVec = NULL, seed = NULL, nThreads = NULL, namedSample = FALSE, ... ) # S3 method for class 'table' partitionsSample( v, m = NULL, target = NULL, n = NULL, sampleVec = NULL, seed = NULL, nThreads = NULL, namedSample = FALSE, ... ) # S3 method for class 'table' compositionsSample( v, m = NULL, target = NULL, weak = FALSE, n = NULL, sampleVec = NULL, seed = NULL, nThreads = NULL, namedSample = FALSE, ... ) ``` -------------------------------- ### Iterate Through Combinations Source: https://jwood000.github.io/RcppAlgos/reference/combinatoricsIterator.html Demonstrates how to retrieve individual combinations and multiple combinations sequentially from an iterator object. Use `@nextIter()` to get the next single combination and `@nextNIter(n)` to get the next `n` combinations. ```R b@nextIter() #> [1] 1056.087 1038.314 1036.531 1035.189 1029.416 1026.804 1025.575 ``` ```R b@nextNIter(3) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] #> [1,] 1056.087 1038.314 1036.531 1035.189 1029.416 1026.804 1024.763 #> [2,] 1056.087 1038.314 1036.531 1035.189 1029.416 1026.804 1021.563 #> [3,] 1056.087 1038.314 1036.531 1035.189 1029.416 1026.804 1019.610 ``` -------------------------------- ### Get current iteration of iterator Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Returns the current iteration number of the iterator. ```R currIter ``` -------------------------------- ### Initialize partitionsIter Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Initializes an iterator for partitions. Supports various arguments for customization, including repetition, frequencies, and target number. ```R partitionsIter(v, m = NULL, repetition = FALSE, freqs = NULL, target = NULL, nThreads = NULL, tolerance = NULL, ...) ``` -------------------------------- ### General Partitions with Custom Frequencies and Targets Source: https://jwood000.github.io/RcppAlgos/articles/SubsetSum.html Illustrates using partitionsGeneral with a randomly sampled vector, custom frequencies, and a specific target sum. It also shows how to limit the number of results returned for performance. ```r set.seed(42) mySamp <- sample(-100:100, 50) sort(mySamp) #> [1] -98 -97 -96 -95 -81 -77 -74 -65 -60 -59 -58 -54 -52 -43 -36 -33 -30 -27 -12 -9 -2 #> [22] -1 3 8 9 10 13 21 27 30 33 35 42 45 49 52 53 57 61 63 64 70 #> [43] 73 76 83 84 88 91 96 100 system.time(exotic <- partitionsGeneral(mySamp, 8, freqs = rep(1:5, 10), target = 496)) #> user system elapsed #> 0.098 0.001 0.099 dim(exotic) #> [1] 102241 8 ## Over 1 billion total combinations prettyNum(comboCount(mySamp, 8, freqs = rep(1:5, 10)), big.mark = ",") #> [1] "1,343,133,680" ## Only getting a few (a thousand in this case) is much faster system.time(partitionsGeneral(mySamp, 8, freqs = rep(1:5, 10), target = 496, upper = 1e3)) #> user system elapsed #> 0.002 0.000 0.001 ``` -------------------------------- ### Basic usage of partitionsSample with seed Source: https://jwood000.github.io/RcppAlgos/articles/CombinatorialSampling.html Demonstrates the basic usage of partitionsSample to draw random samples of partitions. The 'seed' parameter ensures reproducible results. ```R partitionsSample(100, 8, TRUE, n = 3, seed = 42) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #> [1,] 1 1 3 3 4 20 23 45 #> [2,] 1 1 2 7 14 14 29 32 #> [3,] 2 10 11 11 16 16 16 18 ``` -------------------------------- ### General Combinations with Constraints Source: https://jwood000.github.io/RcppAlgos/reference/RcppAlgos-package.html Example of generating combinations with specific constraints, utilizing parallel processing for performance. ```R comboGeneral(150, 5, constraintFun = "sum", Parallel = TRUE) ``` -------------------------------- ### Compare Serial vs. Multithreaded Partitions Generation Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Demonstrates how to use partitionsIterator with and without multithreading. Multithreading is only effective for large numbers of elements. Use microbenchmark to compare performance. ```R serial <- partitionsIter(1000, 10) multi <- partitionsIter(1000, 10, nThreads = 4) fetch1e6 <- multi@nextNIter(1e6) ## much faster than serial@nextNIter(1e6) fetch1e3 <- multi@nextNIter(1e3) ## only one thread used... same as serial@nextNIter(1e3)library(microbenchmark) microbenchmark(multi@nextNIter(1e6), serial@nextNIter(1e6)) microbenchmark(multi@nextNIter(1e3), serial@nextNIter(1e3)) ``` -------------------------------- ### partitionsIter() and compositionsIter() Source: https://jwood000.github.io/RcppAlgos/reference/index.html These functions provide iterators for generating partitions and compositions, enabling efficient processing of large numbers. ```APIDOC ## partitionsIter() ### Description Provides an iterator for generating partitions. ### Method `partitionsIter(n)` ### Parameters - **n** (integer) - The integer to partition. ### Returns An iterator object for partitions. ## compositionsIter() ### Description Provides an iterator for generating compositions. ### Method `compositionsIter(n)` ### Parameters - **n** (integer) - The integer to compose. ### Returns An iterator object for compositions. ``` -------------------------------- ### Get Previous Combinations with prevNIter Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Retrieves a specified number of previous combinations. This is useful for iterating backward through the results. ```R a$prevNIter(4) #> [,1] [,2] [,3] #> [1,] 3 4 5 #> [2,] 2 4 5 #> [3,] 2 3 5 #> [4,] 2 3 4 ``` -------------------------------- ### Iterate through partitions with repetition Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Initialize an iterator for partitions with repetition and demonstrate fetching single partitions, multiple partitions, the front partition, remaining partitions, and summary information. ```R a = partitionsIter(0:10, repetition = TRUE) a@nextIter() a@nextNIter(3) a@front() a@nextRemaining() a@summary() a@back() a[[5]] a[[c(1, 17, 3)]] a@summary() ``` -------------------------------- ### Use Pre-computed Factorizations Source: https://jwood000.github.io/RcppAlgos/reference/divisorsSieve.html Demonstrates how to use the pre-computed factorizations generated by divisorsSieve for further analysis. Accessing the factorizations by index is efficient. ```R ## Use generated complete factorization for further ## analysis by accessing the index of allFacs for (s in mySamp) { myFac <- allFacs[[s]] ## Continue algorithm } ``` -------------------------------- ### Get Remaining Combinations with nextRemaining Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Retrieves the remaining combinations from the current iterator position. Assumes the iterator has been initialized. ```R a$nextRemaining() #> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 1 4 5 #> [3,] 2 3 4 #> [4,] 2 3 5 #> [5,] 2 4 5 #> [6,] 3 4 5 ``` -------------------------------- ### Get summary information of iterator Source: https://jwood000.github.io/RcppAlgos/reference/partitionsIterator.html Returns a list containing summary information about the iterator's state and configuration. ```R summary ``` -------------------------------- ### Permutation Sampling with Frequencies Source: https://jwood000.github.io/RcppAlgos/reference/RcppAlgos-package.html Example of sampling permutations from a set while considering element frequencies. A seed is used for reproducibility. ```R permuteSample(rnorm(100), 10, freqs = rep(1:4, 25), n = 15, seed = 123) ``` -------------------------------- ### Benchmark: Compositions (General, Repetition) Source: https://jwood000.github.io/RcppAlgos/articles/HighPerformanceBenchmarks.html Compares RcppAlgos (serial) with 'arrangements' and 'partitions' for generating all compositions of 15 with repetition. Excludes 'partitions' for larger cases due to efficiency. ```R stopifnot(identical(dim(t1), dim(t2)), identical(dim(t1), dim(t(t3)))), all(rowSums(t1) == 15), all(rowSums(t2) == 15), all(colSums(t3) == 15)) dim(t1) #> [1] 16384 15 rm(t1, t2, t3) invisible(gc()) microbenchmark(cbRcppAlgosSer = compositionsGeneral(0:15, repetition = TRUE), cbArrangements = arrangements::compositions(15), cbPartitions = partitions::compositions(15), times = 20, unit = "relative") #> Unit: relative #> expr min lq mean median uq max neval #> cbRcppAlgosSer 1.107 1.101 1.098 1.096 1.094 1.053 20 #> cbArrangements 1.000 1.000 1.000 1.000 1.000 1.000 20 #> cbPartitions 110.419 117.834 145.577 145.641 165.113 191.249 20 ``` -------------------------------- ### partitionsSample with namedSample and sampleVec Source: https://jwood000.github.io/RcppAlgos/articles/CombinatorialSampling.html Shows how to use partitionsSample with 'namedSample = TRUE' to obtain lexicographical indices and with 'sampleVec' to retrieve specific partitions. ```R partitionsSample(100, 8, TRUE, n = 3, seed = 42, namedSample = TRUE) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #> 61413 1 1 3 3 4 20 23 45 #> 54425 1 1 2 7 14 14 29 32 #> 623844 2 10 11 11 16 16 16 18 partitionsSample(100, 8, TRUE, sampleVec = c(61413, 54425, 623844)) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #> [1,] 1 1 3 3 4 20 23 45 #> [2,] 1 1 2 7 14 14 29 32 #> [3,] 2 10 11 11 16 16 16 18 ``` -------------------------------- ### Generate Partitions in Chunks Source: https://jwood000.github.io/RcppAlgos/reference/partitionsGeneral.html Demonstrates using the `lower` and `upper` arguments to generate partitions within a specific range, useful for parallel processing. ```R partitionsGeneral(15, 3, lower = 6) partitionsGeneral(15, 3, upper = 5) ``` -------------------------------- ### Get Current Combination Source: https://jwood000.github.io/RcppAlgos/reference/combinatoricsIterator.html Fetches the combination currently pointed to by the iterator. Use `@currIter()` to view the most recently generated or selected combination. ```R b@currIter() #> [1] 1056.087 1038.314 1036.531 1035.189 1029.416 1026.804 1019.610 ``` -------------------------------- ### Generate Compositions with Distinct Parts Instantly using Threads Source: https://jwood000.github.io/RcppAlgos/articles/IntegerCompositions.html Shows how using multiple threads (nThreads = 4) can generate compositions with distinct parts almost instantly, significantly reducing the elapsed time. ```R system.time(compositionsGeneral(55, 8, nThreads = 4)) #> user system elapsed #> 0.645 0.017 0.189 ``` -------------------------------- ### Return Named List of Factorizations Source: https://jwood000.github.io/RcppAlgos/reference/divisorsRcpp.html Shows how to get a named list of factorizations when processing a vector of numbers by setting `namedList = TRUE`. ```R ## Return named list myFacsWithNames <- divisorsRcpp(myVec, namedList = TRUE) ``` -------------------------------- ### Generate All Integer Partitions of N Source: https://jwood000.github.io/RcppAlgos/articles/IntegerPartitions.html Use `partitionsGeneral` with `repetition = TRUE` and `m = NULL` to find all possible ways to sum to N using positive integers, allowing parts to repeat. The `v` argument should be a sequence from 0 to N. ```R library(RcppAlgos) options(width = 90) packageVersion("RcppAlgos") #> [1] '2.10.0' cat(paste(capture.output(sessionInfo())[1:3], collapse = "\n")) #> R version 4.5.2 (2025-10-31) #> Platform: aarch64-apple-darwin20 #> Running under: macOS Sequoia 15.7.4 partitionsGeneral(0:5, repetition = TRUE) #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0 0 0 0 5 #> [2,] 0 0 0 1 4 #> [3,] 0 0 0 2 3 #> [4,] 0 0 1 1 3 #> [5,] 0 0 1 2 2 #> [6,] 0 1 1 1 2 #> [7,] 1 1 1 1 1 ## Note that we could also use comboGeneral: ## comboGeneral(0:5, repetition = TRUE, ## constraintFun = "sum", ## comparisonFun = "==", limitConstraints = 5) ## ## The same goes for any of the examples below ``` -------------------------------- ### Benchmark RcppAlgos Version 2.10.0 Iterators Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Measures the performance of the test_nextIter function for version 2.10.0 using microbenchmark. Ensure RcppAlgos is installed. ```r curr_version <- as.integer(gsub("\.", "", packageVersion("RcppAlgos"))) curr_version #> [1] 2100 microbenchmark(curr_v = test_nextIter(15, 8, v = curr_version)) #> Unit: milliseconds #> expr min lq mean median uq max neval #> curr_v 1.883376 1.960046 2.010785 1.99299 2.011481 2.722359 100 ``` -------------------------------- ### Get Complete Factorization of a Single Number Source: https://jwood000.github.io/RcppAlgos/reference/divisorsRcpp.html Demonstrates how to obtain the complete factorization for a single integer using divisorsRcpp. The output is an unnamed vector. ```R ## Get the complete factorization of a single number divisorsRcpp(10^8) #> [1] 1 2 4 5 8 10 16 #> [8] 20 25 32 40 50 64 80 #> [15] 100 125 128 160 200 250 256 #> [22] 320 400 500 625 640 800 1000 #> [29] 1250 1280 1600 2000 2500 3125 3200 #> [36] 4000 5000 6250 6400 8000 10000 12500 #> [43] 15625 16000 20000 25000 31250 32000 40000 #> [50] 50000 62500 78125 80000 100000 125000 156250 #> [57] 160000 200000 250000 312500 390625 400000 500000 #> [64] 625000 781250 800000 1000000 1250000 1562500 2000000 #> [71] 2500000 3125000 4000000 5000000 6250000 10000000 12500000 #> [78] 20000000 25000000 50000000 100000000 ``` -------------------------------- ### Sample Partitions with Restrictions Source: https://jwood000.github.io/RcppAlgos/articles/CombinatorialSampling.html Demonstrates sampling partitions of a number with frequency constraints. The `target` parameter can be used to restrict the sum of elements in the partition. ```R partitionsSample(0:50, 6, freqs = c(50, rep(1, 50)), n = 3, seed = 222, target = 100) ``` -------------------------------- ### Accessing First and Remaining Elements Source: https://jwood000.github.io/RcppAlgos/reference/comboGroupsIterator.html Demonstrates retrieving the first element of the current partition using front() and all remaining elements using nextRemaining(). ```R a@front() #> Grp1 Grp1 Grp1 Grp1 Grp2 Grp2 Grp2 Grp2 Grp3 Grp3 Grp3 Grp3 #> 1 2 3 4 5 6 7 8 9 10 11 12 all_remaining = a@nextRemaining() dim(all_remaining) #> [1] 5774 12 ``` -------------------------------- ### Constrained Combinations with comboGeneral Source: https://jwood000.github.io/RcppAlgos/index.html Generate combinations that satisfy specific constraints, such as a product range. This example finds combinations where the product is between 3600 and 4000. ```r ## Get combinations such that the product is between ## 3600 and 4000 (including 3600 but not 4000) comboGeneral(5, 7, TRUE, constraintFun = "prod", comparisonFun = c(">=","<"), limitConstraints = c(3600, 4000), keepResults = TRUE) #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #> [1,] 1 2 3 5 5 5 5 3750 #> [2,] 1 3 3 4 4 5 5 3600 #> [3,] 1 3 4 4 4 4 5 3840 #> [4,] 2 2 3 3 4 5 5 3600 #> [5,] 2 2 3 4 4 4 5 3840 #> [6,] 3 3 3 3 3 3 5 3645 #> [7,] 3 3 3 3 3 4 4 3888 ``` -------------------------------- ### Verify RcppAlgos Version 2.10.0 Iterator Results Source: https://jwood000.github.io/RcppAlgos/articles/CombinatoricsIterators.html Compares the output of test_nextIter with comboGeneral for version 2.10.0 to ensure correctness. Ensure RcppAlgos is installed. ```r identical(test_nextIter(15, 8, get_val = TRUE, v = curr_version), comboGeneral(15, 8)) #> [1] TRUE ``` -------------------------------- ### Using Multithreading for Factorization Source: https://jwood000.github.io/RcppAlgos/reference/divisorsRcpp.html Demonstrates how to utilize multiple threads for faster computation of factorizations by specifying the `nThreads` argument. Timing is included. ```R ## Using nThreads system.time(divisorsRcpp(myVec, nThreads = 2)) #> user system elapsed #> 0.001 0.000 0.001 ```