Re: How to speed up this calculation?
- To: mathgroup at smc.vnet.net
- Subject: [mg37242] Re: How to speed up this calculation?
- From: "Kevin J. McCann" <kmccann at umbc.edu>
- Date: Fri, 18 Oct 2002 05:17:03 -0400 (EDT)
- Organization: University of Maryland, Baltimore County
- References: <aokc15$agm$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here is a way with symbolic lists.
(* Here are some sample lists*)
lst1={A,B};
lst2={a,b,c};
(*Join into one list*)
list1=Join[lst1,lst2];
(*Do the outer product to get all possible ordered pairs*)
list2=Partition[Flatten[Outer[List,list1,list1]],2]
{{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},
{c^2}}
(*Union weeds out repeats*)
list4=Union[list3,list3]
{{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*)
list5=list4/.{x_^2}®{x,x}/.{x_*y_}®{x,y}
{{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*)
Sort[lst]//TableForm
--
Kevin J. McCann
Joint Center for Earth Systems Technology (JCET)
Department of Physics
UMBC
Baltimore MD 21250
"Cheng Liu" <cliu at lanl.gov> wrote in message
news:aokc15$agm$1 at smc.vnet.net...
> 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
> ====================================================
>
>