RE: Expanding a nested structure (pattern matching?)
- To: mathgroup at smc.vnet.net
- Subject: [mg24855] RE: [mg24835] Expanding a nested structure (pattern matching?)
- From: "David Park" <djmp at earthlink.net>
- Date: Wed, 16 Aug 2000 03:24:16 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Dear John,
A wonderful question actually! I am looking forward to the answers you will
get. Here is one approach that seemed to work. (You don't want to use
capital D as a symbol because it has a predefined meaning. Best to stay with
small letters.)
structureexpand[expr_] :=
FixedPoint[
Flatten[(# /.
a_[b_List, c_List] :>
Flatten[Outer[a, b, c]]) //. (a_ /; FreeQ[a, List])[b___,
c_List, d___] :> ((a[b, #, d] &) /@ c)] &, expr]
I tried it on three expressions.
e1 = a[b, c, d[{f, g}, {h, j}]]
e2 = a[b, c, d[e, f[{f1, f2}, {f3, f4}]]]
e3 = a[b, c[{c1, c2}, {c3, c4}], d[e, f[{f1, f2}, {f3, f4}]]]
structureexpand[e1]
{a[b, c, d[f, h]], a[b, c, d[f, j]], a[b, c, d[g, h]], a[b, c, d[g, j]]}
structureexpand[e2]
{a[b, c, d[e, f[f1, f3]]], a[b, c, d[e, f[f1, f4]]], a[b, c, d[e, f[f2,
f3]]],
a[b, c, d[e, f[f2, f4]]]}
structureexpand[e3]
{a[b, c[c1, c3], d[e, f[f1, f3]]], a[b, c[c1, c3], d[e, f[f1, f4]]],
a[b, c[c1, c3], d[e, f[f2, f3]]], a[b, c[c1, c3], d[e, f[f2, f4]]],
a[b, c[c1, c4], d[e, f[f1, f3]]], a[b, c[c1, c4], d[e, f[f1, f4]]],
a[b, c[c1, c4], d[e, f[f2, f3]]], a[b, c[c1, c4], d[e, f[f2, f4]]],
a[b, c[c2, c3], d[e, f[f1, f3]]], a[b, c[c2, c3], d[e, f[f1, f4]]],
a[b, c[c2, c3], d[e, f[f2, f3]]], a[b, c[c2, c3], d[e, f[f2, f4]]],
a[b, c[c2, c4], d[e, f[f1, f3]]], a[b, c[c2, c4], d[e, f[f1, f4]]],
a[b, c[c2, c4], d[e, f[f2, f3]]], a[b, c[c2, c4], d[e, f[f2, f4]]]}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
-----Original Message-----
> From: John A. Gunnels [mailto:gunnels at cs.utexas.edu]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, August 15, 2000 3:04 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg24855] [mg24835] Expanding a nested structure (pattern matching?)
>
>
> I hope that this is not a _really_ stupid question, but I have run
> into what appears to be a problem using rewrite rules to un-nest
> a structure.
>
> The crux of the problem (I have tried to make it as simple as possible
> without losing the essence of my difficulty) has to do with duplicating
> the structure surrounding the terms that I wish to rewrite.
>
> Any nested list is intended to represent a choice point and my rewrite
> rules are aimed at enumerating all of the possible sequences of choices.
>
> An example:
> Input:
> A[B, C, D[ {F, G}, {H, J}]]
> should become
> A[B, C, D[F, H]],
> A[B, C, D[F, J]],
> A[B, C, D[G, J]],
> A[B, C, D[G, J]]
>
> Obviously, the order isn't important, but the nesting isn't restricted
> to level 2 nor am I guaranteed that all heads or elements are unique.
> I realize that I may have to simply write the code that iterates through
> the different Depth[]s, but this seems like it might have a very clean
> answer that simply hasn't occurred to me.
>
> Thanks,
> John A. Gunnels
> gunnels at cs.utexas.edu
>
>