cartesian-product

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

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 nee...

Cartesian product of arbitrary sets in Java

Do you know some neat Java libaries that allow you to make cartesian product of two (or more) sets? For example: I have tree sets. One with objects of class Person, second with objects of class Gift and third with objects of class GiftExtension. I want to generate one set containing all possible triples Person-Gift-GiftExtension. The...

MySQL Multitable Query Problem

I have a problem querying multiple tables in MySQL and am currently at my wits end. I have unique IDs in each table, and am using an INNER JOIN to combine them; I am quite new to SQL and this may be the wrong method, hence me posting here: Query: SELECT res.UserID, res.QuizID, res.QuizResult, u.UserID, u.UserLogin, q.QuizID, q.QuizNam...

Mixing implicit and explicit JOINs

I am having a problem with Hibernate generating invalid SQL. Specifically, mixing and matching implicit and explicit joins. This seems to be an open bug. However, I'm not sure why this is invalid SQL. I have come up with a small toy example that generates the same syntax exception. Schema CREATE TABLE Employee ( employeeID INT, name...

SQL cartesian join problem

I have three tables A: A.pID primary key, A.Name nvarchar(250) B: B.pID primary key, B.Name nvarchar(250) C: C.pID primary key, C.Name nvarchar(250) There is a m to n relation between A and B (table lA_B with primary key lA_B.pID and .pInstanceA Foreign key to table A and .pInstanceB Foreign key to table B) There is a m to n rela...

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

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...

Split String - the Cartesian way.

Given the following string: "foo bar-baz-zzz" I want to split it at the characters " " and "-", preserving their value, but get all combinations of inputs. i want to get a two-dimensional array containing {{"foo", "bar", "baz", "zzz"} ,{"foo bar", "baz", "zzz"} ,{"foo", "bar-baz", "zzz"} ,{"foo bar-baz", "zzz"} ,{"foo", "bar", "baz-z...

duplicates in a join

the following SQL is returning every BT.Bt_Name where L.date_back is Null. I only wish to select the BT.Bt_Names where L.Bc_id is duplicated SELECT BT.Bt_Name FROM Book_Title BT INNER JOIN ( Book_Copy BC INNER JOIN Loan L ON BC.Bc_id = L.Bc_id ) ON BT.Bt_id = BC.Bt_id WHERE L.Date_back Is NULL GROUP BY BT.Bt_name HAVING COUNT(L.Bc...

Efficient Cartesian Product algorithm

Can somebody please demonstrate for me a more efficient Cartesian product algorithm than the one I am using currently (assuming there is one). I've looked around SO and googled a bit but can't see anything obvious so I could be missing something. foreach (int i in is) { foreach (int j in js) { //Pair i and j } } This is a...

Generate all possible dna sequences from a few given sets

Hi, I have been trying to wrap my head around this for a while now but have not been able to come up with a good solution. Here goes: Given a number of sets: set1: A, T set2: C set3: A, C, G set4: T set5: G I want to generate all possible sequences from a list of sets. In this example the length of the sequence is 5, but it can be a...

List multiplication

Hi, Python newbie here. I have a list L = [a, b, c] and I want to generate a list of tuples : [(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...] I tried doing L * L but it didn't work. Can someone tell me how to get this in python. ...

How can I compute a Cartesian product iteratively?

This question asks how to compute the Cartesian product of a given number of vectors. Since the number of vectors is known in advance and rather small, the solution is easily obtained with nested for loops. Now suppose that you are given, in your language of choice, a vector of vectors (or list of lists, or set of sets, etc.): l = [ [1...

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

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 ...

Cartesian product in Scheme

I've been trying to do a function that returns the Cartesian Product of n sets,in Dr Scheme,the sets are given as a list of lists,I've been stuck at this all day,I would like a few guidelines as where to start. ----LATER EDIT ----- Here is the solution I came up with,I'm sure that it's not by far the most efficent or neat but I'm only ...

Concurrent cartesian product algorithm in Clojure

Is there a good algorithm to calculate the cartesian product of three seqs concurrently in Clojure? I'm working on a small hobby project in Clojure, mainly as a means to learn the language, and its concurrency features. In my project, I need to calculate the cartesian product of three seqs (and do something with the results). I found t...

Math Question: Is it possible to calculate the row position as you loop through a cartesian product of two arrays?

Is it possible to calculate the row position in the cartesian product of two arrays? For example if you have one array of two rows and another of three rows it's easy to calculate the size of the cartesian product (Array1.Rows.Count * Array2.Rows.Count = 6), but you can't iterate through each array and just use the product of the respec...

how to get cartesian products between database and local sequences in linq?

I saw this similar question here but can't figure out how to use Contains in Cartesian product desired result situation: http://stackoverflow.com/questions/1712105/linq-to-sql-exception-local-sequence-cannot-be-used-in-linq-to-sql-implementatio Let's say I have following: var a = new [] { 1, 4, 7 }; var b = new [] { 2, 5, 8 }; var te...

How to avoid Cartesian product in an INNER JOIN query?

I have 6 tables, let's call them a,b,c,d,e,f. Now I want to search all the colums (except the ID columns) of all tables for a certain word, let's say 'Joe'. What I did was, I made INNER JOINS over all the tables and then used LIKE to search the columns. INNER JOIN ... ON INNER JOIN ... ON.......etc. WHERE a.firstname ~* 'Joe' OR a.las...

C++ How to generate the set of cartesian product of n-dimensional tuples

I wish to generate some data that represents the co-ordinates of a cloud of points representing an n-cube of n dimensions. These points should be evenly distributed throughout the n-space and should be able to be generated with a user-defined spacing between them. This data will be stored in an array. ...

In python: how to apply itertools.product to elements of a list of lists

I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete... itertools.product seems to do the trick but I am stuck in a little detail. arrays = [(-1,+1), (-2,+2), (-3,+3)]; If I do cp = list(itertools.product(arrays)); I get cp = cp0 = ...