cartesian-product

All combinations for hash of arrays [duplicate]

心不动则不痛 提交于 2019-12-01 09:47:01
问题 This question already has answers here : Turning a Hash of Arrays into an Array of Hashes in Ruby (7 answers) Closed 6 years ago . Summary Given a Hash where some of the values are arrays, how can I get an array of hashes for all possible combinations? Test Case options = { a:[1,2], b:[3,4], c:5 } p options.self_product #=> [{:a=>1, :b=>3, :c=>5}, #=> {:a=>1, :b=>4, :c=>5}, #=> {:a=>2, :b=>3, :c=>5}, #=> {:a=>2, :b=>4, :c=>5}] When the value for a particular key is not an array, it should

Dimensionality agnostic (generic) cartesian product

主宰稳场 提交于 2019-12-01 06:28:23
I'm looking to generate the cartesian product of a relatively large number of arrays to span a high-dimensional grid. Because of the high dimensionality, it won't be possible to store the result of the cartesian product computation in memory; rather it will be written to hard disk. Because of this constraint, I need access to the intermediate results as they are generated. What I've been doing so far is this: for x in xrange(0, 10): for y in xrange(0, 10): for z in xrange(0, 10): writeToHdd(x,y,z) which, apart from being very nasty, is not scalable (i.e. it would require me writing as many

Dimensionality agnostic (generic) cartesian product

耗尽温柔 提交于 2019-12-01 03:53:10
问题 I'm looking to generate the cartesian product of a relatively large number of arrays to span a high-dimensional grid. Because of the high dimensionality, it won't be possible to store the result of the cartesian product computation in memory; rather it will be written to hard disk. Because of this constraint, I need access to the intermediate results as they are generated. What I've been doing so far is this: for x in xrange(0, 10): for y in xrange(0, 10): for z in xrange(0, 10): writeToHdd(x

Cartesian Product of multiple arrays in C

▼魔方 西西 提交于 2019-11-30 22:01:35
I am able to achieve cartesian product of static number of arrays in C.But I would like to build a code dynamically taking the number of input arrays.Can someone shed some light how to do this "only using arrays".If it's not possible with arrays please suggest me other solution.Thank you.Here is my code below for cartesian product of 3 arrays. #include<stdio.h> int main(void) { int array1[100]; int array2[100]; int array3[100]; int cartesian_array[100][100]; int m,n,o; int i,j; int p=0,q=0,r=0; int x,y,z; int cartesian_arr_row_len; int cartesian_arr_col_len; printf("Enter the size of first

Cartesian product in clojure

こ雲淡風輕ζ 提交于 2019-11-30 17:53:59
I'm trying to implement a method that will take a list of lists and return a the cartesian product of these lists. Here's what I have so far: (defn cart ([] '()) ([l1] (map list l1)) ([l1 l2] (map (fn f[x] (map (fn g [y] (list x y)) l2)) l1) ) ) (defn cartesian-product [& lists] (reduce cart lists) ) ;test cases (println (cartesian-product '(a b) '(c d))) ; ((a c) (a d) (b c) (b d)) (println (cartesian-product ())) ;() (println (cartesian-product '(0 1))) ; ((0) (1)) (println (cartesian-product '(0 1) '(0 1))) ; ((0 0) (0 1) (1 0) (1 1)) (println (apply cartesian-product (take 4 (repeat (range

Cartesian Product of multiple arrays in C

一个人想着一个人 提交于 2019-11-30 17:28:06
问题 I am able to achieve cartesian product of static number of arrays in C.But I would like to build a code dynamically taking the number of input arrays.Can someone shed some light how to do this "only using arrays".If it's not possible with arrays please suggest me other solution.Thank you.Here is my code below for cartesian product of 3 arrays. #include<stdio.h> int main(void) { int array1[100]; int array2[100]; int array3[100]; int cartesian_array[100][100]; int m,n,o; int i,j; int p=0,q=0,r

What's a good, non-recursive algorithm to calculate a Cartesian product?

天涯浪子 提交于 2019-11-30 16:07:26
Note This is not a REBOL-specific question. You can answer it in any language. Background The REBOL language supports the creation of domain-specific languages known as "dialects" in REBOL parlance . I've created such a dialect for list comprehensions, which aren't natively supported in REBOL. A good cartesian product algorithm is needed for list comprehensions. The Problem I've used meta-programming to solve this, by dynamically creating and then executing a sequence of nested foreach statements. It works beautifully. However, because it's dynamic, the code is not very readable. REBOL doesn't

Why does application of `sequence` on List of Lists lead to computation of its Cartesian Product?

笑着哭i 提交于 2019-11-30 12:32:53
问题 My question is about the sequence function in Prelude , the signature of which is as follows: sequence :: Monad m => [m a] -> m [a] I understand how this function works for List of Maybe s. For example, applying sequence on [Just 3, Just 9] gives Just [3, 9] . I noticed that applying sequence on List of List s gives its Cartesian Product. Can someone please help me understand how/why this happens? 回答1: This works because using lists as monads in Haskell makes them model indeterminism.

Cartesian Product in c++

我的未来我决定 提交于 2019-11-30 10:56:33
I have been searching for weeks on how to come up with piece of code which I could applied the cartesian product. Let's say I have two arrays : int M[2]= {1,2}; int J[3] = {0,1,2}; So the code will takes those two arrays in apply the rule M X J therefore we will have the pairs (1,0)(1,1)(1,2)(2,0)(2,1)(2,2) and I want the new result to be saved into a new array where each index in the array contains a pair , for example c[0] = (1,0). Help please :( Here is an simple example of implementing Cartesian product using vector. Vectors are much better choice as we do not need to worry about its size

Why does application of `sequence` on List of Lists lead to computation of its Cartesian Product?

試著忘記壹切 提交于 2019-11-30 02:55:06
My question is about the sequence function in Prelude , the signature of which is as follows: sequence :: Monad m => [m a] -> m [a] I understand how this function works for List of Maybe s. For example, applying sequence on [Just 3, Just 9] gives Just [3, 9] . I noticed that applying sequence on List of List s gives its Cartesian Product. Can someone please help me understand how/why this happens? Peter Wortmann This works because using lists as monads in Haskell makes them model indeterminism. Consider: sequence [[1,2],[3,4]] By definition this is the same as: do x <- [1,2] y <- [3,4] return