MathGroup Archive 2000

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

Search the Archive

Re: Function to transform lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22167] Re: [mg22126] Function to transform lists
  • From: Hartmut Wolf <hwolf at debis.com>
  • Date: Thu, 17 Feb 2000 01:23:55 -0500 (EST)
  • Organization: debis Systemhaus
  • References: <200002160734.CAA17918@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Enrique Cao schrieb:
> 
> Hi  All :
> 
>          I need  help  in  order to transform the list A  to the list B.
> 
>               How can I get a function to do that ?
> 
> A = {{{30, -2}},
>     {{45, -3}},
>     {{60, -4}},
>     {{45, -3}, {45, -5}},
>     {{30, -2}, {30, -6}},
>     {{45, -7}},
>     {{60, -8}},
>     {{60, -8}, {60, -9}},
>     {{60, -8}, {60, -9}, {60, -10}},
>     {{60, -8}, {60, -9}, {60, -10}, {60, -11}},
>     {{60, -8}, {60, -9}, {60, -10}, {60, -11}, {60, -12}},
>     {{45, -7}, {45, -13}}, {{45, -7}, {45, -13}, {45, -14}},
>     {{45, -7}, {45, -13}, {45, -14}, {45, -15}},
>     {{30, -2}, {30, -6}, {30, -16}},
>     {{45, -17}},
>     {{45, -17}, {45, -18}},
>     {{45, -17}, {45, -18}, {45, -19}},
>     {{45, -17}, {45, -18}, {45, -19}, {45, -20}},
>     {{45, -17}, {45, -18}, {45, -19}, {45, -20}, {45, -21}},
>  {{45, -17}, {45, -18}, {45, -19}, {45, -20}, {45, -21}, {45, -22}},
> {{45, -17}, {45, -18}, {45, -19}, {45, -20}, {45, -21},
> {45, -22},{45, -23}},
> {{30, -2}, {30, -6}, {30, -16}, {30, -24}},
> {{45, -25}},
>  {{60, -26}},
>  {{45, -25}, {45, -27}},
>  {{45, -25}, {45, -27}, {45, -28}}}
> 
> B = {{{60, -4}},
>     {{45, -3}, {45, -5}},
>     {{60, -8}, {60, -9}, {60, -10}, {60, -11}, {60, -12}},
>     {{45, -7}, {45, -13}, {45, -14}, {45, -15}},
>     {{45, -17}, {45, -18}, {45, -19}, {45, -20}, {45, -21},
> {45, -22},{45, -23}},
>     {{30, -2}, {30, -6}, {30, -16}, {30, -24}},
>     {{60, -26}},
>     {{45, -25}, {45, -27}, {45, -28}}}
> 
>                      Thank you very much
> 
>                      Enrique Cao
> 
> cao at mundo-r.com


Enrique,

with reference to Carl K. Woll <carlw at fermi.phys.washington.edu> who had
develloped that unforgetable 

OrderedUnion[li_]:=Block[{i},
    i[n_]:=(i[n]=Sequence[];n);
    i /@ li]

in respose to a posting in June last year, I defined...

In[3]:= structuredUnion[structure_, level_] := 
  Block[{i}, i[n_] := (i[n] = Sequence[]; n);
    Map[i, structure, level] //. {} -> Sequence[]]

...which applies his idea to certain levels of a structure. In addition
we need a means to reverse that structure (at a certain level, or down
to a certain level):

In[5]:= reverseStructure[structure_, level_Integer] := 
  Flatten[Map[Reverse, {structure}, level + If[level < 0, 0, 1]], 1]

In[6]:= reverseStructure[structure_, levelspec : {level_Integer}] := 
  Flatten[Map[Reverse, {structure}, levelspec + If[level < 0, 0, 1]], 1]


In[7]:= reverseStructure[{1, 2, 3, 4}, 0]
Out[7]= {4, 3, 2, 1}

In[8]:= reverseStructure[{{1, 2}, {3, 4}}, {0}]
Out[8]= {{3, 4}, {1, 2}}

In[9]:= reverseStructure[{{1, 2}, {3, 4}}, {1}]
Out[9]= {{2, 1}, {4, 3}}

In[10]:= reverseStructure[{{1, 2}, {3, 4}}, -2]
Out[10]= {{4, 3}, {2, 1}}


With these definitions we just need to do

In[11]:= tmp1 = reverseStructure[A, -3];         (* do not include level
{-2} *)
In[12]:= tmp2 = structuredUnion[tmp1, {-2}];     (* unify at level {-2}
*)
In[13]:= res = reverseStructure[tmp2, -3];
In[14]:= res === B
Out[14]= True


-- Hartmut


  • Prev by Date: Re: solving third order equation: bad result
  • Next by Date: Re: Linux: how compatible with add-ons ?
  • Previous by thread: Function to transform lists
  • Next by thread: Re: Function to transform lists