cartesian-product

Iterative Cartesian Product in Java

南楼画角 提交于 2019-11-26 06:46:14
问题 I want to compute the cartesian product of an arbitrary number of nonempty sets in Java. I\'ve wrote that iterative code... public static <T> List<Set<T>> cartesianProduct(List<Set<T>> list) { List<Iterator<T>> iterators = new ArrayList<Iterator<T>>(list.size()); List<T> elements = new ArrayList<T>(list.size()); List<Set<T>> toRet = new ArrayList<Set<T>>(); for (int i = 0; i < list.size(); i++) { iterators.add(list.get(i).iterator()); elements.add(iterators.get(i).next()); } for (int j = 1; j

Cartesian product of 2 lists in Haskell

孤者浪人 提交于 2019-11-26 03:25:17
问题 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. 回答1: This is very easy with list

Finding cartesian product with PHP associative arrays

穿精又带淫゛_ 提交于 2019-11-26 03:13:43
问题 Say that I have an array like the following: Array ( [arm] => Array ( [0] => A [1] => B [2] => C ) [gender] => Array ( [0] => Female [1] => Male ) [location] => Array ( [0] => Vancouver [1] => Calgary ) ) How can I find the cartesian product while preserving the keys of the outer associative array and using them in the inner ones? The result of the algorithm should be this: Array ( [0] => Array ( [arm] => A [gender] => Female [location] => Vancouver ) [1] => Array ( [arm] => A [gender] =>

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

我与影子孤独终老i 提交于 2019-11-26 03:07:37
问题 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

How can I create cartesian product of vector of vectors?

◇◆丶佛笑我妖孽 提交于 2019-11-26 02:19:55
问题 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

Get the cartesian product of a series of lists?

你离开我真会死。 提交于 2019-11-26 01:17:29
问题 How can I get the Cartesian product (every possible combination of values) from a group of lists? Input: somelists = [ [1, 2, 3], [\'a\', \'b\'], [4, 5] ] Desired output: [(1, \'a\', 4), (1, \'a\', 5), (1, \'b\', 4), (1, \'b\', 5), (2, \'a\', 4), (2, \'a\', 5) ...] 回答1: In Python 2.6+ import itertools for element in itertools.product(*somelists): print(element) Documentation: Python 3 - itertools.product 回答2: import itertools >>> for i in itertools.product([1,2,3],['a','b'],[4,5]): ... print

Is there a good LINQ way to do a cartesian product?

狂风中的少年 提交于 2019-11-26 00:56:01
问题 I have a class structure like so: Person Dogs (dog 1, dog 2, etc) Puppies (puppy A, puppy B, etc) There is one person. He has 1..n dogs. Each dog has 1..n puppies. I want a list of all the possible combination of puppies, taking 1 puppy from each dog. Eg: dog 1 puppy A, dog 2 puppy A dog 1 puppy A, dog 2 puppy B dog 1 puppy B, dog 2 puppy A dog 1 puppy B, dog 2 puppy B If it was in sql tables, i\'d do something like the following to \'multiply\' the tables: select * from puppies a, puppies b

How to Eager Load Associations without duplication in NHibernate?

感情迁移 提交于 2019-11-26 00:08:33
问题 I\'d need to load a list of very large objects with so many children and children of children. what\'s the best approach to take? I\'m using Oracle 11g database and I\'ve written the below method but it results in cartesian product (duplicated results): public IList<ARNomination> GetByEventId(long eventId) { var session = this._sessionFactory.Session; var nominationQuery = session.Query<ARNomination>().Where(n => n.Event.Id == eventId); using (var trans = session.Transaction) { trans.Begin();

Generate all possible combinations of the elements of some vectors (Cartesian product)

六眼飞鱼酱① 提交于 2019-11-25 23:28:33
问题 I would like to generate all the possible combinations of the elements of a given number of vectors. For example, for [1 2] , [1 2] and [4 5] I want to generate the elements: [1 1 4; 1 1 5; 1 2 4; 1 2 5; 2 1 4; 2 1 5; 2 2 4; 2 2 5] The problem is that I don\'t know the number of vectors for which I need to calculate the combinations. There might be 3 as in this case, or there may be 10, and I need a generalization . Can you please help me to this in MATLAB? Is there already a predefined

Cartesian product of arbitrary sets in Java

China☆狼群 提交于 2019-11-25 23:16:57
问题 Do you know some neat Java libaries that allow you to make cartesian product of two (or more) sets? For example: I have three 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 number of sets might vary so I cannot do this in nested foreach loop. Under some conditions my application needs to make a product of Person-Gift pair,