MathGroup Archive 2002

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

Search the Archive

Re: How to speed up this calculation?

  • To: mathgroup at
  • Subject: [mg37242] Re: How to speed up this calculation?
  • From: "Kevin J. McCann" <kmccann at>
  • Date: Fri, 18 Oct 2002 05:17:03 -0400 (EDT)
  • Organization: University of Maryland, Baltimore County
  • References: <aokc15$agm$>
  • Sender: owner-wri-mathgroup at

Here is a way with symbolic lists.

(* Here are some sample lists*)



(*Join into one list*)


(*Do the outer product to get all possible ordered pairs*)


{{A, A}, {A, B}, {A, a},

{A, b}, {A, c}, {B, A},

{B, B}, {B, a}, {B, b},

{B, c}, {a, A}, {a, B},

{a, a}, {a, b}, {a, c},

{b, A}, {b, B}, {b, a},

{b, b}, {b, c}, {c, A},

{c, B}, {c, a}, {c, b},

{c, c}}

(*Turn the pairs into products*)

list3=list2/.{x_,y_}®{x y}

{{A^2}, {A*B}, {a*A}, {A*b},

{A*c}, {A*B}, {B^2}, {a*B},

{b*B}, {B*c}, {a*A}, {a*B},

{a^2}, {a*b}, {a*c}, {A*b},

{b*B}, {a*b}, {b^2}, {b*c},

{A*c}, {B*c}, {a*c}, {b*c},


(*Union weeds out repeats*)


{{a^2}, {a*A}, {A^2}, {a*b},

{A*b}, {b^2}, {a*B}, {A*B},

{b*B}, {B^2}, {a*c}, {A*c},

{b*c}, {B*c}, {c^2}}

(*Now turn the products back into pairs*)

(*This is the step the requires symbols*)


{{a, a}, {a, A}, {A, A},

{a, b}, {A, b}, {b, b},

{a, B}, {A, B}, {b, B},

{B, B}, {a, c}, {A, c},

{b, c}, {B, c}, {c, c}}

(*List it out*)

(*If you have numbers, you can now use a Rule to replace*)


Kevin J. McCann
Joint Center for Earth Systems Technology (JCET)
Department of Physics
Baltimore MD 21250

"Cheng Liu" <cliu at> wrote in message
news:aokc15$agm$1 at
> 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
> 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
> ====================================================

  • Prev by Date: RE: List processing
  • Next by Date: Re: RE: Re: re: Accuracy and Precision
  • Previous by thread: Re: How to speed up this calculation?
  • Next by thread: Re: How to speed up this calculation?