cartesian-product

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

假如想象 提交于 2019-11-29 23:45:50
问题 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

Iterate over cartesian product of vectors

岁酱吖の 提交于 2019-11-29 20:26:24
问题 I have the following nested loop: for (x in xs) { for (y in ys) { # Do something with x and y } } Which I’d like to flatten so I thought of building a Cartesian product of the two vectors xs and ys and iterating over the result. In Python, this would be trivial: for xy in product(xs, ys): # x, y = xy[0], xy[1] But in R, the simplest equivalent I’ve found looks daunting: xys <- expand.grid(xs, ys) for (i in 1 : nrow(xys)) { xy <- as.vector(xys[i, ]) # x <- xy[1], y <- xy[2] } Surely there must

In Perl, how can I get the Cartesian product of multiple sets?

心已入冬 提交于 2019-11-29 17:25:55
问题 I want to do permutation in Perl. For example I have three arrays: ["big", "tiny", "small"] and then I have ["red", "yellow", "green"] and also ["apple", "pear", "banana"] . How do I get: ["big", "red", "apple"] ["big", "red", "pear"] ..etc.. ["small", "green", "banana"] I understand this is called permutation. But I am not sure how to do it. Also I don't know how many arrays I can have. There may be three or four, so I don't want to do nested loop. 回答1: That's actually not permutation but

In Perl, how can I iterate over the Cartesian product of multiple sets?

本秂侑毒 提交于 2019-11-29 14:53:07
Given x number of arrays, each with a possibly different number of elements, how can I iterate through all combinations where I select one item from each array? Example: [ ] [ ] [ ] foo cat 1 bar dog 2 baz 3 4 Returns [foo] [cat] [ 1 ] [foo] [cat] [ 2 ] ... [baz] [dog] [ 4 ] I'm doing this in Perl, btw. My Set::CrossProduct module does exactly what you want. Note that you aren't really looking for permutations, which is the ordering of the elements in a set. You're looking for the cross product, which is the combinations of elements from different sets. My module gives you an iterator, so you

Create a type list combination of types in C++

折月煮酒 提交于 2019-11-29 09:29:47
Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or C. For a N=2 case, this would be std::tuple<A,A> std::tuple<A,B> std::tuple<A,C> std::tuple<B,A> std::tuple<B,B> std::tuple<B,C> std::tuple<C,A> std::tuple<C,B> std::tuple<C,C> The idea is to create a tuple which holds a container for all those types, so I can later store any of those types inside the container list. template <typename ...Combinations>

MemoryError while creating cartesian product in Numpy

前提是你 提交于 2019-11-29 04:21:06
I have 3 numpy arrays and need to form the cartesian product between them. Dimensions of the arrays are not fixed, so they can take different values, one example could be A=(10000, 50), B=(40, 50), C=(10000,50). Then, I perform some processing (like a+b-c) Below is the function that I am using for the product. def cartesian_2d(arrays, out=None): arrays = [np.asarray(x) for x in arrays] dtype = arrays[0].dtype n = np.prod([x.shape[0] for x in arrays]) if out is None: out = np.empty([n, len(arrays), arrays[0].shape[1]], dtype=dtype) m = n // arrays[0].shape[0] out[:, 0] = np.repeat(arrays[0], m,

Calculate n-ary Cartesian Product

三世轮回 提交于 2019-11-29 03:55:10
Given two lists, I can produce a list of all permutations the Cartesian Product of these two lists: permute :: [a] -> [a] -> [[a]] permute xs ys = [ [x, y] | x <- xs, y <- ys ] Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ] How do I extend permute so that instead of taking two lists, it takes a list (length n) of lists and returns a list of lists (length n) permute :: [[a]] -> [[a]] Example> permute [ [1,2], [3,4], [5,6] ] == [ [1,3,5], [1,3,6], [1,4,5], [1,4,6] ] --etc I couldn't find anything relevant on Hoogle.. the only function matching the signature was transpose , which

Cartesian Product + N x M Dynamic Array

荒凉一梦 提交于 2019-11-29 02:32:11
I have looked hours for a solution without any success. Hopefully someone can help me out. I have a dynamic array of N items on M origin zip codes. For instance: Item 1: 11001, 54010, 60621 Item 2: 11001, 60621 Item 3: 60621 I want to create a new array that will look like this: Route 1: 11001, 11001, 60621 Route 2: 11001, 60621, 60621 Route 3: 54010, 11001, 60621 etc - until Route 6. Suggestions? ---------------------- Is there any way to accomplish this WITHOUT using Linq? VB.net and Linq do not go together :) Gabe It sounds like you want this function from Eric Lippert's blog post written

Cartesian product in MATLAB

僤鯓⒐⒋嵵緔 提交于 2019-11-29 01:25:25
Here is the simplified version of the problem I have. Suppose I have a vector p=[1 5 10] and another one q=[.75 .85 .95]. And I want to come up with the following matrix: res=[1, .75;1, .85; 1, .95; 5, .75; 5, .85; 5, .95; 10, .75; 10, .85; 10, .95]. This is also known as the Cartesian Product. How can I do that? Many thanks Here's one way: [X,Y] = meshgrid(p,q); result = [X(:) Y(:)]; The output is: result = 1.0000 0.7500 1.0000 0.8500 1.0000 0.9500 5.0000 0.7500 5.0000 0.8500 5.0000 0.9500 10.0000 0.7500 10.0000 0.8500 10.0000 0.9500 A similar approach as the one described by @nibot can be

Is there a multi-dimensional version of arange/linspace in numpy?

流过昼夜 提交于 2019-11-28 18:33:41
I would like a list of 2d NumPy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, ..., 3.5, 4, 4.5, 5} and the same for y. I could do x = np.arange(-5, 5.1, 0.5) y = np.arange(-5, 5.1, 0.5) and then iterate through all possible pairs, but I'm sure there's a nicer way... I would like something back that looks like: [[-5, -5], [-5, -4.5], [-5, -4], ... [5, 5]] but the order does not matter. farenorth You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step: import numpy as np X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5] For linspace-like