       Re: QuestionUsenet

• To: mathgroup at smc.vnet.net
• Subject: [mg77943] Re: QuestionUsenet
• From: Szabolcs <szhorvat at gmail.com>
• Date: Wed, 20 Jun 2007 05:32:51 -0400 (EDT)
• Organization: University of Bergen
• References: <f58ebn\$93t\$1@smc.vnet.net>

```JanPax wrote:
>
>   Hi,
>   what is the easiest way to generate (in Mathematica) ALL terms given some
>   function symbols and their arities
>   and variables x,y, like e.g. f unary, g binary, up to depth N?
>   x,y, f(x),f(y),g(x,f(x)),g(x,f(y))....
>   Or even better, to generate *next* term at one step, so that all terms up
>   to depth N  will be reached once.
>
>   Thank you, JP

I am sure there are better solutions than this ...
but this one works too ...

In:= make[lst_,fun_,arity_] := Join[fun@@@Subsets[lst, {arity}], lst]

for a single function and a list 'lst' of variables.

In:= make[{x,y}, f, 1]
Out= {f[x],f[y],x,y}

In:= Union@make[%, f, 1]
Out= {x,y,f[x],f[y],f[f[x]],f[f[y]]}

In:= make[{x,y}, g, 2]
Out= {g[x,y],x,y}

In:= Union@make[%, g, 2]
Out= {x,y,g[x,y],g[g[x,y],x],g[g[x,y],y]}

Union[] is for getting rid of duplicates.  Use Tuples instead of Subsets
if ordering matters.

This can be extended to work with multiple functions:

In:= makeMore[lst_,funLst_] :=
Join@@Replace[
funLst,
{f_,ar_} :> f@@@Subsets[lst, {ar}],
{1} ]

Here 'funLst' is a list of {function, arity} pairs, for example:

In:= funList = {{Identity,1}, {f,1}, {g,2}}

In:= makeMore[{x,y}, funList]
Out= {x,y,f[x],f[y],g[x,y]}

In:= Union@makeMore[%, funList]
Out=
{x,y,f[x],f[y],f[f[x]],f[f[y]],f[g[x,y]],g[x,y],g[x,f[x]],g[x,f[y]],g[x,g[x,y]],g[y,f[x]],g[y,f[y]],g[y,g[x,y]],g[f[x],f[y]],g[f[x],g[x,y]],g[f[y],g[x,y]]}

Sorry for the stupid function names.

Szabolcs

```

• Prev by Date: Re: Re:QuestionUsenet
• Next by Date: Reproducible crash in version 6
• Previous by thread: Re: Integrate modified in version 6?
• Next by thread: Reproducible crash in version 6