So, I wanted to do something that I assumed would be really simple. Given two lists, $A = \{a_1, a_2, ...\}, B = \{ ... \}$, I wanted to take a function $f(a, b)$ and generate a list with $\{ f(a_1, b_1), f(a_1, b_2), ... \}$, i.e. just the cartesian product.
Of course, it's really easy, in principal, to do this:
f[#1, #2] & @@@ Outer[List, {a1, a2, a3}, {b1, b2}]
The annoyance comes in when the elements are themselves lists. In this case, everything breaks. Ridiculous things start happening.
There is a "trivial" solution; just use dummy variables in the above statement, and replace it in with what I want later. But to me this seems fundamentally wrong somehow. Surely it should be possible to work with lists of arbitrary type; and not have to "protect" your mapping functions from lists of lists.
Any thoughts? Am I missing some fundamental strategy here?
-- Edit:
To elaborate more, I'm actually trying to make the following work:
dim = 2; (* Say *)
Table[
SomeFunc[dim, g, op, #] & /@
Subsets[Range[dim], {i}], {i, 1, dim}
]
op
previously was a single matrix; but now I need it to be a list of matricies. That is so say, I want to run "SomeFunc" for each matrix in that list, and also each of the results that come from the Subset
function (note, of course, that Subsets
returns lists.)
Outer[f, {a1, a2, a3}, {b1, b2}]
. You will also want to look into the documentation forOuter[]
; in particular, its support of level arguments. $\endgroup$f(x, y, a, b)
. So I don't thinkOuter
will work in that case. Well, maybe I could make it work ... but I think this question still is relevant. $\endgroup$Outer[SomeFunc[dim, g, #1, #2] &, opList, subList, 1]
, then? $\endgroup$