MathGroup Archive 2000

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

Search the Archive

Re: RE: Expanding a nested structure (pattern matching?)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24905] Re: [mg24855] RE: [mg24835] Expanding a nested structure (pattern matching?)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sun, 20 Aug 2000 01:34:54 -0400 (EDT)
  • References: <8nlh4v$944@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hartmut, David, John
David has sent me Hartmut's fine solution:

ClearAll["`*"]

dsb2[h_List] := Flatten[h]
dsb2[h_ /; MemberQ[h, {___}]] := Distribute[h, List]
dsb2[a_] := a
strexp2[e_] := dsb2//@e (*strexp2 is my notation*)

This is clearly the way to tackle such problems.

We can even reduce the code slightly to

dsb3[a_List] := Flatten[a]
dsb3[a_] := Distribute[a, List]
strexp3[e_] := dsb3//@ e

since, for example, we have

Distribute[3, List]

        3

The following version works also through heads

dsb4[a_List] := Flatten[a]
dsb4[r : _List[___]] := Flatten[dsb4 /@ Through[r]]
dsb4[r_] := Distribute[r, List]
strexp4[e_] := dsb4//@e

strexp4[{g, h}[{c, d}, e]]

        {g[c, e], g[d, e], h[c, e], h[d, e]}

The three functions agree on the following examples.

examples = {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}]]],
      e4 = f[{a, b}, {{c1, c2}, {d1, d2}}],
      e5 = a[b, c, d[e, f[{f1, f2}, {f3, f4, f5}, {f6, f7}]]],
      e6 =
        a[b, c[{c1, c2}, {c3}], d[e, f[{f1, f2}, {f3, f4, f5}, {f6, f7}]]]};

SameQ @@ Through[{strexp4, strexp3}[#]] & /@ examples

        {True, True, True, True, True, True}

A spot timing shows a slight increase is speed

tst = h @@ Table[Random[], {7}, {6}];

Timing[strexp2[tst];]

        {10.65 Second, Null}

Timing[strexp3[tst];]

        {8.24 Second, Null}

Timing[strexp4[tst];]

        {8.08 Second, Null}

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




--
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:8nlh4v$944 at smc.vnet.net...
> [From Hartmut Wolf]
> > Also redefining...
> >
> > dsb2[h_List] := Flatten[h]
> > dsb2[h_ /; MemberQ[h, {___}]] := Distribute[h, List]
> > dsb2[a_] := a
> >
> > dsb2 file://@ e4
> > {f[a, c1], f[a, c2], f[a, d1], f[a, d2], f[b, c1], f[b, c2], f[b, d1],
> >   f[b, d2]}
> >
>
> Dear Hartmut,
>
> That seems like a nice solution. And I was blithely unaware of MapAll!
>
> Here is a somewhat more complete list of examples. John communicated with
me
> and gave a case with variable numbers of and lengths of lists.
>
> 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}]]]
> e4 = f[{a, b}, {{c1, c2}, {d1, d2}}]
> e5 = a[b, c, d[e, f[{f1, f2}, {f3, f4, f5}, {f6, f7}]]]
> e6 = a[b, c[{c1, c2}, {c3}], d[e, f[{f1, f2}, {f3, f4, f5}, {f6, f7}]]]
>
> On the last case, your method was more than 10 times faster than my
> procedure.
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>




  • Prev by Date: Re: Surface Graphics versus Graphics3D features
  • Next by Date: Email
  • Previous by thread: RE: RE: Expanding a nested structure (pattern matching?)
  • Next by thread: Confusing Behavior of LogPlot vs. Plot