Re: Re:QuestionUsenet
- To: mathgroup at smc.vnet.net
- Subject: [mg77949] Re: [mg77917] Re:QuestionUsenet
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 20 Jun 2007 05:36:02 -0400 (EDT)
- References: <200706191059.GAA08415@smc.vnet.net>
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
- References:
- Re:QuestionUsenet
- From: JanPax <pax0@seznam.cz>
- Re:QuestionUsenet