RE: How to speed up this calculation?

*To*: mathgroup at smc.vnet.net*Subject*: [mg37235] RE: [mg37200] How to speed up this calculation?*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>*Date*: Fri, 18 Oct 2002 05:16:49 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

>-----Original Message----- >From: Cheng Liu [mailto:cliu at lanl.gov] To: mathgroup at smc.vnet.net >Sent: Wednesday, October 16, 2002 8:26 PM >To: mathgroup at smc.vnet.net >Subject: [mg37235] [mg37200] How to speed up this calculation? > > >Dear group, > > I have the following question regarding a lengthy calculation >using Mathematica: > >For given w points in x direction and h points in y direction, I can >construct all the points using > > h=10; w=8; > points=Flatten[Transpose[Outer[List,Range[w],Range[h]]],1] > >Next, I need to find all the possible pairs of point including points >themselves, i.e., pair AA. I can use > > pairs=Outer[List,points,points,1] > >Then, I have to clear those pairs that repeat themselves, >i.e., pair AB and >pair BA. Also, when w and h are of the order of 1000s, the >computation >takes a very long time. Is there a better way of doing the >second part of >the computation? Thanks in advance. > >Sincerely > >Cheng > > >==================================================== >Cheng Liu, Ph.D. >MST-8, Structure/Property Relations >Materials Science and Technology Division >Los Alamos National Laboratory >Los Alamos, New Mexico 87545 > >Phone: 505-665-6892 (office), 505-667-9950 (lab) >Fax: 505-667-8021 >email: cliu at lanl.gov >==================================================== > > Cheng, you didn't tell about a specific order of your resulting pairs, so you not need Transpose in your first line: points = Flatten[Outer[List, Range[w], Range[h]], 1] Now build the pairs (for sake of clarity , let me call the points points = {p1, p2, p3, p4, p5}, of course you don't do that): Flatten[ With[{l = Length[points]}, Array[If[#1>#2, Unevaluated[Sequence[]], points[[{#1, #2}]]]&, {l, l}]], 1] {{p1, p1}, {p1, p2}, {p1, p3}, {p1, p4}, {p1, p5}, {p2, p2}, {p2, p3}, {p2, p4}, {p2, p5}, {p3, p3}, {p3, p4}, {p3, p5}, {p4, p4}, {p4, p5}, {p5, p5}} Alternatively you might do pairs = Outer[List, points, points, 1] Flatten[MapIndexed[Drop[#1, First[#2] - 1] &, pairs], 1] {{p1, p1}, {p1, p2}, {p1, p3}, {p1, p4}, {p1, p5}, {p2, p2}, {p2, p3}, {p2, p4}, {p2, p5}, {p3, p3}, {p3, p4}, {p3, p5}, {p4, p4}, {p4, p5}, {p5, p5}} ...try out which is faster. -- Hartmut Wolf