MathGroup Archive 1996

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

Search the Archive

Re: Programming: Replace Heads

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5198] Re: [mg5161] Programming: Replace Heads
  • From: Allan Hayes <hay at haystack>
  • Date: Thu, 14 Nov 1996 02:02:05 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Xah Lee <xah at best.com>
[mg5161] Programming: Replace Heads
writes

>Problem:
>Given an abritrary expression, how to replace all heads to List?
>
>my solution:
>Map[ {(Sequence@@#)}&, expr, {0,-2}]

A faster solution:

Apply[List, expr,{0,-2}]

Check:
Clear[a,b,c,d,e,f,g]
f = g[g[d[g[1, 1, 1], b[1, 1, 2], g[1, 1, 3]],
   c[g[1, 2, 1], c[1, 2, 2], e[1, 2, 3]]],
  g[c[d[2, 1, 1], a[2, 1, 2], a[2, 1, 3]],
   a[e[2, 2, 1], e[2, 2, 2], c[2, 2, 3]]],
  g[c[d[3, 1, 1], b[3, 1, 2], b[3, 1, 3]],
   d[b[3, 2, 1], b[3, 2, 2], a[3, 2, 3]]]]

Apply[List, f,{0,-2}]

{{{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}}, {{1, 2, 1}, {1, 2, 2}, {1, 2,  
3}}}, {{{2, 1, 1}, {2, 1, 2}, {2, 1, 3}}, {{2, 2, 1}, {2, 2, 2}, {2,  
2, 3}}}, {{{3, 1, 1}, {3, 1, 2}, {3, 1, 3}}, {{3, 2, 1}, {3, 2, 2},  
{3, 2, 3}}}}

Timings:
Do[Map[ {(Sequence@@#)}&, f, {0,-2}],{100}]//Timing
Do[Apply[List,f,{0,-2}],{100}]//Timing
Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk


  • Prev by Date: Transient equations
  • Next by Date: Re: Integration..Constants
  • Previous by thread: Programming: Replace Heads
  • Next by thread: Re: Programming: Replace Heads