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 ...
You could start with something like
In[1]:= make[lst_,fun_,arity_] := Join[fun@@@Subsets[lst, {arity}], lst]
for a single function and a list 'lst' of variables.
In[2]:= make[{x,y}, f, 1]
Out[2]= {f[x],f[y],x,y}
In[3]:= Union@make[%, f, 1]
Out[3]= {x,y,f[x],f[y],f[f[x]],f[f[y]]}
In[4]:= make[{x,y}, g, 2]
Out[4]= {g[x,y],x,y}
In[5]:= Union@make[%, g, 2]
Out[5]= {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[6]:= 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[7]:= funList = {{Identity,1}, {f,1}, {g,2}}
In[8]:= makeMore[{x,y}, funList]
Out[8]= {x,y,f[x],f[y],g[x,y]}
In[9]:= Union@makeMore[%, funList]
Out[9]=
{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