MathGroup Archive 2007

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

Search the Archive

Re: Re:QuestionUsenet


On 19 Jun 2007, at 19:59, 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
>
>

Assuming that you mean what I think you mean, here is one way (up to  
level 2 - it's clear how to continue beyond that).

SetAttributes[F, {Flat, OneIdentity, Orderless}]

FF0[l_List] := ReplaceList[l, {___, x_ /; Length[{x}] == 1, ___} :> G 
[x]]
FF1[l_List] := ReplaceList[(F[##1] & ) @@ l, F[x_, y_] :> F[x, G[y]]]
FF2[l_List] := ReplaceList[(F[##1] & ) @@ l, F[x_, y_] :> F[G[x], G[y]]]

p = Tuples[{a, b}, 2];

Reverse[Union[Flatten[({FF0[#1], FF1[##1], FF2[##1]} & ) /@ p]]] /.  
{F -> g, G -> f}

{f[b], f[a], g[f[b], f[b]], g[f[a], f[b]], g[f[a], f[a]], g[b, f[b]],  
g[b, f[a]],
   g[a, f[b]], g[a, f[a]]}

q=Tuples[{a,b,c},2];

Reverse[Union[Flatten[({FF0[#1], FF1[##1], FF2[##1]} & ) /@ q]]] /.
   {F -> g, G -> f}

{f[c], f[b], f[a], g[f[c], f[c]], g[f[b], f[c]], g[f[b], f[b]], g[f 
[a], f[c]],
   g[f[a], f[b]], g[f[a], f[a]], g[c, f[c]], g[c, f[b]], g[c, f[a]], g 
[b, f[c]],
   g[b, f[b]], g[b, f[a]], g[a, f[c]], g[a, f[b]], g[a, f[a]]}

etc.


Andrzej Kozlowski


  • Prev by Date: ComplexExpand in Mathematica 5.2 and 6
  • Next by Date: Audio
  • Previous by thread: Re:QuestionUsenet
  • Next by thread: Re: Re:QuestionUsenet