cartesian-product

Cross product in Scala

空扰寡人 提交于 2019-11-26 16:23:45
问题 I want to have a binary operator cross (cross-product/cartesian product) that operates with traversables in Scala: val x = Seq(1, 2) val y = List('hello', 'world', 'bye') val z = x cross y # i can chain as many traversables e.g. x cross y cross w etc assert z == ((1, 'hello'), (1, 'world'), (1, 'bye'), (2, 'hello'), (2, 'world'), (2, 'bye')) What is the best way to do this in Scala only (i.e. not using something like scalaz)? 回答1: You can do this pretty straightforwardly with an implicit

How to apply itertools.product to elements of a list of lists?

青春壹個敷衍的年華 提交于 2019-11-26 13:05:14
问题 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 = [((-1, 1),), ((-2, 2),), ((-3, 3),)] But what I want to get is cp1 = [(-1,-2,-3), (-1,-2,+3), (-1,+2,-3), (-1,+2,+3), ..., (+1,+2,-3), (+1,+2,+3)]. I have tried a few

Fighting cartesian product (x-join) when using NHibernate 3.0.0

谁都会走 提交于 2019-11-26 12:50:53
I'm bad at math but I kind get idea what cartesian product is. Here is my situation (simplified): public class Project{ public IList<Partner> Partners{get;set;} } public class Partner{ public IList<PartnerCosts> Costs{get;set;} public IList<Address> Addresses{get;set;} } public class PartnerCosts{ public Money Total{get;set;} } public class Money{ public decimal Amount{get;set;} public int CurrencyCode{get;set;} } public class Address{ public string Street{get;set;} } My aim is to effectively load entire Project. Problem of course is: If I try to eager load partners and their costs, query

Efficient Cartesian Product algorithm

折月煮酒 提交于 2019-11-26 12:28:15
问题 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 highly simplified version of what I do in my code. The two integers are lookup keys which are used to retrieve one/more objects and all the objects from the two lookups

How can I make Cartesian product with Java 8 streams?

大兔子大兔子 提交于 2019-11-26 12:26:31
问题 I have the following collection type: Map<String, Collection<String>> map; I would like to create unique combinations of each of map.size() from a single value in the collection for each Key. For example suppose the map looks like the following: A, {a1, a2, a3, ..., an} B, {b1, b2, b3, ..., bn} C, {c1, c2, c3, ..., cn} The result I would like to get would a List<Set<String>> result, looking similar to (ordering is not important, it just needs to be a \'complete\' result consisting of all

Cartesian product of 2 lists in Haskell

纵然是瞬间 提交于 2019-11-26 12:19:55
I wish to produce the Cartesian product of 2 lists in Haskell, but I cannot work out how to do it. The cartesian product gives all combinations of the list elements: xs = [1,2,3] ys = [4,5,6] cartProd :: [a] -> [b] -> [(a,b)] cartProd xs ys ==> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] This is not an actual homework question and is not related to any such question, but the way in which this problem is solved may help with one I am stuck on. This is very easy with list comprehensions. To get the cartesian product of the lists xs and ys , we just need to take the tuple (x,y) for

How can I create cartesian product of vector of vectors?

廉价感情. 提交于 2019-11-26 11:20:37
I've a vector of vectors say vector<vector<int> > items of different sizes like as follows 1,2,3 4,5 6,7,8 I want to create combinations in terms of Cartesian product of these vectors like 1,4,6 1,4,7 1,4,8 and so on till 3,5,8 How can I do that ? I've looked up several links and I've also listed them at the end of this post but I'm not able to interpret that as I'm not that familiar with the language. Could some body help me with this. #include <iostream> #include <iomanip> #include <vector> using namespace std; int main() { vector<vector<int> > items; int k = 0; for ( int i = 0; i < 5; i++ )

Mixing implicit and explicit JOINs

别来无恙 提交于 2019-11-26 09:55:59
问题 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 VARCHAR(255), managerEmployeeID INT ) Data INSERT INTO Employee (employeeID, name) VALUES (1, \'Gary\') INSERT INTO Employee (employeeID, name, managerEmployeeID

How to find all permutations (with repetition) in MATLAB?

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:30:11
问题 Suppose I have 4 letters and I want to arrange them in 3 places (repetition allowed), so I would have 4 3 =64 possible permutations. How can I compute and print them? 回答1: Simplifying Amro's answer, you could use this: %// Sample data x = 'ABCD'; %// Set of possible letters K = 3; %// Length of each permutation %// Create all possible permutations (with repetition) of letters stored in x C = cell(K, 1); %// Preallocate a cell array [C{:}] = ndgrid(x); %// Create K grids of values y = cellfun(

How can I compute a Cartesian product iteratively?

拈花ヽ惹草 提交于 2019-11-26 06:46:16
问题 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,2,3], [4,5], [6,7], [8,9,10], [11,12], [13] ] If I was asked to compute its Cartesian product, that is [ [1,4,6,8,11,13], [1,4,6,8,12,13], [1,4,6,9,11,13], [1