Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose f is a function of two variables: $f = f(x,y)\quad$ and $L$ is a list. Could someone help me in defining a list of the form: $D = \{f(x,y)\ |\ x \in L,\ y \in L\}$

I tried this:

D = List[ f[x,y], {x,y} ∈ KroneckerProduct[L,L]]

But I got syntax errors.

share|improve this question
1  
What syntax errors did you get? It could just be that you were trying to assign to D which is a defined symbol already in Mathematica. –  Verbeia Aug 10 '12 at 20:26
    
Also I formatted your code. You might want to read this page which shows how to do it yourself. –  Verbeia Aug 10 '12 at 20:27
    
Are you looking for functioning code or are you trying to typeset something? –  Sjoerd C. de Vries Aug 10 '12 at 20:29
    
Sorry about ambiguities. The syntax error was: D = Table[f[x, y], {x, y} \[Element] Tuples[L, 2]] Table::itform: Argument {x,y}\[Element]Tuples[L,2] at position 2 does not have the correct form for an iterator. >> where L = {{0, 0, 0, 1}, {0, 1, 1, 0}}. In my question, i didn't need x and y to be the same elements, so Tuples[L,2] is more appropriate. I am looking for a functioning code. Let me state it simply, similar to: Flatten[Table[f[x,y], {x, 0, 10}, {y, 0, 10}]] can't we have a similar definition where x and y belong to different lists, say L1 and L2 respectively ? –  Pavithran Iyer Aug 10 '12 at 21:21
2  
In future questions could you please post the code that you actually used? Apparently you used Table instead of List, Tuples instead of KroneckerProduct and x and y need not equal vs. need to be equal. –  Sjoerd C. de Vries Aug 10 '12 at 22:13

3 Answers 3

up vote 5 down vote accepted

Your original definition of D can be given with these lines:

In[1]:= L = {a, b, c, d};
In[2]:= Outer[f, L, L] // Flatten
Out[2]= {f[a, a], f[a, b], f[a, c], f[a, d], f[b, a], f[b, b], 
 f[b, c], f[b, d], f[c, a], f[c, b], f[c, c], f[c, d], f[d, a], 
 f[d, b], f[d, c], f[d, d]}]

If f is symmetric, then you can:

In[3]:= SetAttributes[f, Orderless]
In[4]:= Outer[f, L, L] // Flatten // Union
Out[4]= {f[a, a], f[a, b], f[a, c], f[a, d], f[b, b], f[b, c], 
 f[b, d], f[c, c], f[c, d], f[d, d]}
share|improve this answer

I think you intend to do something like this:

L = {a, b, c, d};
f /@ Transpose[{L, L}]

{f[{a, a}], f[{b, b}], f[{c, c}], f[{d, d}]}

With /@ a prefix shorthand for the Map function.

You might want to look up KroneckerProduct in the manual. It doesn't work as you seem to think.

KroneckerProduct[L, L]

{{a^2, a b, a c, a d}, {a b, b^2, b c, b d}, {a c, b c, c^2, c d}, {a d, b d, c d, d^2}}

share|improve this answer

If f is a function of two-variables, a direct way to define your list (we call it Dlt since D is a built-in Mathematica function) would be this :

L = {a, b, c, d, e};

Dlt = f @@@ Tuples[ L, 2]
{f[a, a], f[a, b], f[a, c], f[a, d], f[a, e],
 f[b, a], f[b, b], f[b, c], f[b, d], f[b, e],
 f[c, a], f[c, b], f[c, c], f[c, d], f[c, e],
 f[d, a], f[d, b], f[d, c], f[d, d], f[d, e],
 f[e, a], f[e, b], f[e, c], f[e, d], f[e, e]}

Symbol f @@@ expr is a shorthand for Apply[ f, expr, {1}]. Tuples[L, 2 ] generates all pairs of elements of list L ( e.g. {a,b} is different from {b,a}).

share|improve this answer
    
In case you want to define a list of values f over a cartesian product of two sets K and L I recommend this f @@@ Tuples[{K, L}]. You should also look at Scan. –  Artes Aug 10 '12 at 22:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.