combinatorics

Generating a very large matrix of string combinations using combn() and bigmemory package

让人想犯罪 __ 提交于 2019-11-29 02:26:37
I have a vector x of 1,344 unique strings. I want to generate a matrix that gives me all possible groups of three values, regardless of order, and export that to a csv. I'm running R on EC2 on a m1.large instance w 64bit Ubuntu. When using combn(x, 3) I get an out of memory error: Error: cannot allocate vector of size 9.0 Gb The size of the resulting matrix is C1344,3 = 403,716,544 rows and three columns - which is the transpose of the result of combn() function. I thought of using the bigmemory package to create a file backed big.matrix so I can then assign the results of the combn() function

Array combinatorics in PHP

女生的网名这么多〃 提交于 2019-11-28 23:42:07
Consider following array: $a = [['x'], ['y', 'z', 'w'], ['m', 'n']]; How can generate following array from it: $output=[ [[x][y][m]], [[x][z][n]], [[x][w][m]], [[x][y][n]], [[x][z][m]], [[x][w][n]], ]; I am searching for a more efficient code than mine. (My current code is presented as an answer below) Here we go. Assuming: $array = [['x'], ['y', 'z', 'w'], ['m', 'n']]; EDIT: After some performance testing, I concluded the solution I posted before is about 300% slower than OP's code, surely due to nested function call stacking. So here is an improved version of OP's approach, which is around

Code for Variations with repetition (combinatorics)?

倖福魔咒の 提交于 2019-11-28 22:10:59
Does anyone have Java code for generating all VARIATIONS WITH REPETITION? There are plenty of permutation and combination examples available, and variations must be the easiest one... It feels stupid to waste time to reinvent the wheel (it must be plenty of code written for this). An example of VARIATIONS WITH REPETITION could be like this: (tupletSize=3, input= A, B) AAA, AAB, ABA, BAA, ABB, BAB, BBA, BBB Thanks! This works as is, and it's the easiest for you to study. public class Main { public static void main(String args[]) { brute("AB", 3, new StringBuffer()); } static void brute(String

Combinations and Permutations in F#

北战南征 提交于 2019-11-28 18:02:21
I've recently written the following combinations and permutations functions for an F# project, but I'm quite aware they're far from optimised. /// Rotates a list by one place forward. let rotate lst = List.tail lst @ [List.head lst] /// Gets all rotations of a list. let getRotations lst = let rec getAll lst i = if i = 0 then [] else lst :: (getAll (rotate lst) (i - 1)) getAll lst (List.length lst) /// Gets all permutations (without repetition) of specified length from a list. let rec getPerms n lst = match n, lst with | 0, _ -> seq [[]] | _, [] -> seq [] | k, _ -> lst |> getRotations |> Seq

Itertools to generate scrambled combinations

≯℡__Kan透↙ 提交于 2019-11-28 14:33:37
What I want to do is obtain all combinations and all unique permutations of each combination. The combinations with replacement function only gets me so far: from itertools import combinations_with_replacement as cwr foo = list(cwr('ACGT', n)) ## n is an integer My intuition on how to move forward is to do something like this: import numpy as np from itertools import permutations as perm bar = [] for x in foo: carp = list(perm(x)) for i in range(len(carp)): for j in range(i+1,len(carp)): if carp[i] == carp[j]: carp[j] = '' carp = carp[list(np.where(np.array(carp) != '')[0])] for y in carp: bar

Permutations of parameters (i.e. Cartesian product) into a multi-dimensional array

岁酱吖の 提交于 2019-11-28 14:10:13
I am interested in calculating a function on a bunch of permutations of parameter values. I want to keep it generic to N dimensions, but let me write it out in 3 dimensions to start with. Generating the permutations is easy enough with meshgrid , but I can't figure out how to reshape the resulting array back to the multidimensions? Here is a starting point: %These are the 3 variations of parameters, with some values. params1 = [100, 200, 300];%Picking these so it is easy to correlate to the function params2 = [10, 20]; params3 = [1, 2]; %This generates parameter_values as the cartesian

Get a list of combinations of lists' elements

Deadly 提交于 2019-11-28 12:38:04
Suppose I have 3 lists: ['q','w'], ['a','s'], ['z','x']. How to get a list of possible combinations out of these lists? So I get a list [['q','a','z'],['q','s','z']] and such. I made a method for two, but can't figure one for N lists: static <E> ArrayList combine(ArrayList<E> one,ArrayList<E> two) { ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); for(E e:one) { for(E e2:two) { ArrayList ps=new ArrayList(); ps.add(e); ps.add(e2); combs.add(ps); } } return combs; } I found out that this is done by Guava's Sets.cartesianProduct. You need N nested loops, which is what makes it hard.

Finding the index of a given permutation

做~自己de王妃 提交于 2019-11-28 12:21:15
I'm reading the numbers 0, 1, ..., (N - 1) one by one in some order. My goal is to find the lexicography index of this given permutation, using only O(1) space. This question was asked before, but all the algorithms I could find used O(N) space. I'm starting to think that it's not possible. But it would really help me a lot with reducing the number of allocations. Considering the following data: chars = [a, b, c, d] perm = [c, d, a, b] ids = get_indexes(perm, chars) = [2, 3, 0, 1] A possible solution for permutation with repetitions goes as follows: len = length(perm) (len = 4) num_chars =

Permutations of binary number by swapping two bits (not lexicographically)

混江龙づ霸主 提交于 2019-11-28 12:21:10
I'm looking for an algorithm which computes all permutations of a bitstring of given length ( n ) and amount of bits set ( k ). For example while n=4 and k=2 the algorithm shall output: 1100 1010 1001 0011 0101 0110 I'm aware of Gosper's Hack which generates the needed permutations in lexicographic order. But i need them to be generated in such a manner, that two consecutive permutations differ in only two (or at least a constant number of) bitpositions (like in the above example). Another bithack to do that would be awesome, but also a algorithmic description would help me alot. Walking bit

Permutations in VBA Excel

こ雲淡風輕ζ 提交于 2019-11-28 11:55:01
问题 I am trying to generate all the possible combinations of an array of characters. The input array has n characters, 5 <= n <= 7, and I would like to generate a second array A( C( n , 5 ) , 5 ) that contains all the C( n , 5 ) combinations. The order of the characters in the array isn't important. Here is an example: input array: { A, B, C, D, E, F } , so n = 6 output array should be: {A B C D E}, {A B C D F}, {A B C F E}, {A B F D E}, {A F C D E}, {F B C D E}, This is pretty simple for n=5 and