[Date Index]
[Thread Index]
[Author Index]
Re: Expanding a nested structure (pattern matching?)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg24872] Re: [mg24835] Expanding a nested structure (pattern matching?)
*From*: "Allan Hayes" <hay at haystack.demon.co.uk>
*Date*: Sat, 19 Aug 2000 04:45:47 -0400 (EDT)
*References*: <8ndge7$ldi@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
David,
Here are two more ways following on your mehod.
I have abbreviated the name structureexpand to se
(*David Park*)
se[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]
Two variants.
se1[e_] := Flatten[e //. (f_ /; f =!= List)[a__] /; MemberQ[{a}, _List] :>
Flatten[Outer[f, ##]] & @@ (Replace[{a},
x_ /; Head[x] =!= List -> {x}, {1}]))]
se2[e_] := Flatten[e //. (f_ /; f =!= List)[a__] /; MemberQ[{f, a}, _List]
:>
Flatten[Outer[#[##2] &, ##]] & @@ (Replace[{f, a},
x_ /; Head[x] =!= List -> {x}, {1}]))]
The outputs of se, se1,se2 agree on your sample inputs
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}]]];
SameQ @@ Through[{se, se1, se2}[#]] & /@ {e1, e2, e3}
{True, True, True}
But se2 will also work over heads
se2[{g, h}[{1, 2}, 3]]
{g[1, 3], g[2, 3], h[1, 3], h[2, 3]}
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"David Park" <djmp at earthlink.net> wrote in message
news:8ndge7$ldi at smc.vnet.net...
> 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: [mg24872] [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
> >
> >
>
>
Prev by Date:
**bug in complex integral?**
Next by Date:
**Re: Expanding a nested structure (pattern matching?)[Correction]**
Previous by thread:
**RE: RE: Expanding a nested structure (pattern matching?)**
Next by thread:
**RE: RE: Expanding a nested structure (pattern matching?)**
| |