MathGroup Archive 2007

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

Search the Archive

Re: QuestionUsenet

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

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_] :=
             {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]

Sorry for the stupid function names.


  • 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