       Re: Lists of all values of a two-variable function

• To: mathgroup at smc.vnet.net
• Subject: [mg64751] Re: [mg64735] Lists of all values of a two-variable function
• From: "Igor C. Antonio" <igora at wolf-ram.com>
• Date: Thu, 2 Mar 2006 06:47:39 -0500 (EST)
• Organization: Wolfram Research, Inc.
• References: <200603011104.GAA23021@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```José Carlos Santos wrote:
> Hi all,
>
> I would like to do this: given a function _f_ in two variables and two
> lists l1 and l2, to get the list of all values f[a,b] with _a_ in l1 and
> _b_ in l2, with all duplicated values removed. How do I do that?
>
> The closest thing that I am able to do is:
>
> Table[f[l1[[i]],l2[[j]]],{i,1,Length[l1]},{j,1,Length[l2]}]
>
> but, of course:
>
> 1) what I get is a list of lists;
>
> 2) I eventually get duplicated values.
>
> Best regards,
>
> Jose Carlos Santos

l1 = {1,1,2}; l2 = {1,2,3};

This code is more simple than the the second (further below), but the duplicates
are removed only f has been evaluated:

In:=
Union[Flatten[Outer[f[##]&,l1,l2],1]]

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

The alternative is to calculate all possible arguments of f, remove the
duplicates, then map f over the results:

In:=
f[Sequence@@#]& /@ Union[Flatten[Outer[List,l1,l2],1]]

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

Very similar code, but if your f has a long running-time, the second choice is
better.

--
Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.

```

• Prev by Date: Re: Nonlinear Fit of Complex functions
• Next by Date: Re: Derivation of Sums of Functions
• Previous by thread: Re: Lists of all values of a two-variable function
• Next by thread: Re: Lists of all values of a two-variable function