MathGroup Archive 1992

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

Search the Archive

Re: Flattening


I came in late on this and have not kept a record of the original 
question but does the following meet the specification?



finallist = (#//.{b___List} :> b)&/@startlist



It is OK on the examples that I have seen (I use Richard Gaylord's 
tests)

f[x_] := (#//.{b___List} :> b)&/@x
g[x_] := 
	FixedPoint[
      Replace[#,{A___,List[X___List],B___} :> {A,X,B}]&,
      x
  ]

startlist = {{a,a,a},{a,a},{{b,b},{b,b,b}},{a,a,a,a},{{b,b},{b}}}

f[startlist] === g[startlist]

True

and

f[{{{{{{a,b},{c,d}}}}}}] === g[{{{{{{a,b},{c,d}}}}}}]

True




And it is pretty fast:

longstartlist = Table[startlist, {20}];

f[longstartlist];//Timing

{0.4 Second, Null}

g[longstartlist];//Timing

{8.83333 Second, Null}

 

A final check 

f[longstartlist] === g[longstartlist]

True

From
Allan Hayes
Department of Mathematics
The University
Leicester LE1 7RH
U.K.
hay at leicester.ac.uk





  • Prev by Date: sparse matrices
  • Next by Date: More gaussian integration bugs
  • Previous by thread: Re: Flattening
  • Next by thread: Re: Ceiling & Floor