Cross product of two lists

cartesian product of list of lists java
cartesian product algorithm
scala cartesian product
c++ cartesian product
cartesian product python without itertools
kotlin cartesian product
cartesian product java
cross product list of lists

Messing around with 'extension functions' for the List module. (I spent quite a while developing 'mapfold' - which threads an accumulator like fold, but uses it as a parameter to create new values like map - then discovered that that is what List.scan_left does)

For generation of test data, I needed to do a cross product of two lists, This is what I came up with:

///Perform cross product of two lists, return tuple
let crossproduct l1 l2 =
let product lst v2 = List.map (fun v1 -> (v1, v2)) lst
List.map_concat (product l1) l2

Is this any good, or is there already some better way to do this?

Same question for this one:

///Perform cross product of three lists, return tuple
let crossproduct3 l1 l2 l3 =
let tuplelist = crossproduct l1 l2 //not sure this is the best way...
let product3 lst2 v3 = List.map (fun (v1, v2) -> (v1, v2, v3)) lst2
List.map_concat (product3 tuplelist) l3

another option is to use F# "sequence expressions" and write something like this:

let crossproduct l1 l2 =
seq { for el1 in l1 do
for el2 in l2 do
yield el1, el2 };;

(actually, it is almost the same thing as what you wrote, because 'for .. in .. do' in sequence expression can be viewed as map_concat). This works with (lazy) sequences, but if you want to work with lists, you'd just wrap the code inside [ ... ] rather than inside seq { ... }.

Get the cartesian product of a series of lists?, In Python 2.6+ import itertools for element in itertools.product(*somelists): print(​element). Documentation: Python 3 - itertools.product. The other type, called the cross product, is a vector product since it yields another vector rather than a scalar. As with the dot product, the cross product of two vectors contains valuable information about the two vectors themselves.

Just came across a rather elegant solution to this using computation expressions:

type Product () =
member this.Bind (l,f) = List.collect f l
member this.Return n = [n]

let enumeratedPizzas =
Product() {
let! x = ["New York";"Chicago"]
let! y = ["Pepperoni";"Sausage"]
let! z = ["Cheese";"Double Cheese"]
return x,y,z
}

By Techneilogy, copied from fssnip.net, follow the link to see commented code.

Cartesian product of two or more lists, Also demonstrate, using your function/method, that the product of an empty list with any other list is empty. {1, 2} × {} = {}: {} × {1, 2} = {}. For extra  The cross product could point in the completely opposite direction and still be at right angles to the two other vectors, so we have the: "Right Hand Rule" With your right-hand, point your index finger along vector a , and point your middle finger along vector b : the cross product goes in the direction of your thumb.

Your crossproduct function looks good (you've probably noticed the missing "in" keywords). I like this version of crossproduct3 better, but that's just me :

let crossproduct3 l1 l2 l3 =
List.map_concat
(fun z ->
(List.map_concat (fun y -> List.map (fun x -> (x, y, z)) l3) l2)) l1;;

Your function has an equivalent algorithmic complexity.

Finally, when using crossproduct on an explicitly empty list, you may hit on the value restriction (roughly, a restriction that makes sure the compiler only infers polymorphic types for a syntactic value), that is particularly strict in F#. The solution is to annotate calls that use an empty list, in the following way (if you want the second list to be composed of integers):

(crossproduct [3; 4] [] : (int * int) list)

Cross product between two lists of vectors, How about this furious[a_, b_] := Module[{a1, a2, a3, b1, b2, b3, c}, {a1, a2, a3} = Transpose[a, {2, 3, 4, 1}]; {b1, b2, b3} = Transpose[b, {2, 3, 4,  You're looking for itertools.product if you're on (at least) Python 2.6. Note that for pre-2.6 use, you can simply copy-and-paste the pure Python implementation from the linked documentation. product() is not restricted to two parameters thus usable for an arbitrary number of lists (listed themselves) as well.

I was using Benjol answer for a while until I found that you can pretty much do the same thing with Linq. Benjol's solution is probably still the most elegant but if anyone wants a solution that you can use with C# also then here you go:

query {
for i in [1, 2] do
for j in [3, 4] do
select (i, j)
}

Which is almost exactly the same as Tomas Petricek's solution except the solution doesn't need to be nested for loops so it is slightly simpler.

Cartesian product of two lists, The Cartesian product of the input lists. That is, a list of each possible ordered pair of a character from the first list and a character from the second list. Each pair is a list or string or similar of two characters, or of two length-one strings. The J primitive catalogue {forms the Cartesian Product of two or more boxed lists. The result is a multi-dimensional array (which can be reshaped to a simple list of lists if desired). The result is a multi-dimensional array (which can be reshaped to a simple list of lists if desired).

As of F# 4.0, you can use List.allPairs to give the Cartesian product of two lists. If you have n lists, see How do I compute the cartesian product of n sequences in F#?

Cross product, In mathematics, the cross product or vector product is a binary operation on two vectors in three-dimensional space ( R 3 ) {\displaystyle \left(\mathbb {R}  Cross Product – Let we have given two vector A = a1 * i + a2 * j + a3 * k and B = b1 * i + b2 * j + b3 * k. Then cross product is calculated as cross product = (a2 * b3 – a3 * b2) * i + (a3 * b1 – a1 * b3) * j + (a1 * b2 – a2 * b1) * k, where [(a2 * b3 – a3 * b2), (a3 * b1 – a1 * b3), (a1 * b2 – a2 * b1)] are the coefficient of unit vector along i, j and k directions.

Python, Python | Construct Cartesian Product Tuple list. Sometimes, while working with data, we need to create data as all possible pairs of containers. This type of  A cross join is used when you wish to create combination of every row from two tables. All row combinations are included in the result; this is commonly called cross product join. A common use for a cross join is to create obtain all combinations of items, such as colors and sizes.

numpy.cross, Return the cross product of two (arrays of) vectors. The cross product of a and b in R^3 is a vector perpendicular to both a and b. If a and b are arrays of vectors,  Free Vector cross product calculator - Find vector cross product step-by-step This website uses cookies to ensure you get the best experience. By using this website, you agree to our Cookie Policy.

Why doesn't list*list produce a cartesian product? : Python, I'm wondering wy the Python devs haven't defined that multiplying two lists should return their cartesian product. It, would be pretty useful - e.g. nested looping  I'm trying to create a function that does the cross product between two lists of the same dimensions, element by element. Each entry of the list is a 3D vector. Something like this: a = {{a1, a2,