Re: Plus sets in mathematica..?
- To: mathgroup at smc.vnet.net
- Subject: [mg75959] Re: Plus sets in mathematica..?
- From: dimitris <dimmechan at yahoo.com>
- Date: Mon, 14 May 2007 03:34:46 -0400 (EDT)
- References: <f26ocl$4tn$1@smc.vnet.net>
=CE=9F/=CE=97 changbo =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5:
> I want to make a function "f", its domain is every thinkable two sets, and
> the result is {a+b | a=E2=88=88A,b=E2=88=88B} for A,B.
>
> ex) A={1,2,3}, B={40,50,60,70},
> then I have f[A,B] as
> {41,42,43,51,52,53,61,62,63,71,72,73}.
>
> How can I construct function "f"? Let me know the source.. Thank you very
> much
I am sure they must be various ways. As regards timing issues I am not
the person
who will talk about.
Anyway, the easiest thing that comes in my mind is:
In[36]:=
A={1,2,3};
B={40,50,60,70};
Flatten@Outer[Plus,B,A]
Out[38]=
{41,42,43,51,52,53,61,62,63,71,72,73}
where
In[39]:=
Information["Outer", LongForm -> False]
"Outer[f, list1, list2, ... ] gives the generalized outer product of
the listi, forming all possible combinations of the \
lowest-level elements in each of them, and feeding them as arguments
to f. Outer[f, list1, list2, ... , n] treats as separate \
elements only sublists at level n in the listi. Outer[f, list1,
list2, ... , n1, n2, ... ] treats as separate elements only \
sublists at level ni in the corresponding listi."*Button[More=E2=80=A6,
ButtonData :> "Outer", Active -> True,
ButtonStyle -> "RefGuideLink"]
As an one-liner
In[4]:=
f[o_,e_,he_]:=Flatten@Outer[he,o,e]
Example
In[5]:=
f[A,B,Plus]
Out[5]=
{41,51,61,71,42,52,62,72,43,53,63,73}
In[9]:=
f[Table[Random[], {3}], CharacterRange["a", "e"], Times]
Out[9]=
{0.758958 a,0.758958 b,0.758958 c,0.758958 d,0.758958 e,0.441985 a,
0=2E441985 \
b,0.441985 c,0.441985 d,0.441985 e,0.344708 a,0.344708 b,0.344708 c,
0=2E344708 \
d,0.344708 e}
Dimitris