MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Plus sets in mathematica..?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75952] Re: Plus sets in mathematica..?
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Mon, 14 May 2007 03:31:11 -0400 (EDT)
  • Organization: University of Bergen
  • References: <f26ocl$4tn$1@smc.vnet.net>

changbo wrote:
> I want to make a function "f", its domain is every thinkable two sets, and the result is  {a+b | aâ??A,bâ??B} 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
> 


Look up Outer in the documentation. Mathematica doesn't have sets, but 
Lists, where the order of elements does matter. If you need to treat 
lists as sets (i.e. no repeating elements), look up Union.

In[3]:=
A={1,2,3}
B={40,50,60,70}

Out[3]=
{1,2,3}

Out[4]=
{40,50,60,70}

In[5]:=
Outer[Plus,A,B]

Out[5]=
{{41,51,61,71},{42,52,62,72},{43,53,63,73}}

In[6]:=
Flatten@%

Out[6]=
{41,51,61,71,42,52,62,72,43,53,63,73}

In[7]:=
Union@%

Out[7]=
{41,42,43,51,52,53,61,62,63,71,72,73}

Define the function f as
f[a_, b_] := Flatten@Outer[Plus, a, b]

Szabolcs


  • Prev by Date: Re: Plus sets in mathematica..?
  • Next by Date: Re: Plus sets in mathematica..?
  • Previous by thread: Re: Plus sets in mathematica..?
  • Next by thread: Re: Plus sets in mathematica..?