MathGroup Archive 2002

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

Search the Archive

RE: SsssComplement?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37873] RE: [mg37831] SsssComplement?
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Sat, 16 Nov 2002 01:15:37 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Mozalev Vasil [mailto:mvp at snklik.ryazan.ru]
To: mathgroup at smc.vnet.net
>Sent: Thursday, November 14, 2002 12:12 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg37873] [mg37831] SsssComplement?
>
>
>Hi. Excuse me for poor-quality English language.
>My question about "Complement" and "UnsortedComplement".
>Function " UnsortedComplement [list, listi] " working more slowly then
>function " Complement
>[list, listi] " because deletes elements "listi" behind many 
>passes, whereas
>" Complement [list,
>listi] " it makes for one pass.
>What kind of procedure I can use to write " SssssComplement 
>[list, list1] "
>for maintenance of
>removal elements "list1" from "list" without sorting, but for one pass?
>The specified lists already are sorted before their processing 
>by function "
>SssssComplement
>[list, list1] ".
>It is necessary for acceleration of accounts.
>Example for check of correctness of work of required function:
>SssssComplement [{1,3,5,7,10}, {3, 7,10}] should give
>{1,5}.
>But
>SssssComplement [{1,3,5,7,10}, {7,3,10}] should give
>{1,3,5}, as for one pass the number 3 will not leave.
>Thank you.
>Vasil.

Vasil,

in honour of Carl Woll's unforgetable OrderedUnion:
http://forums.wolfram.com/mathgroup/archive/1999/Jul/msg00085.html


OrderedComplement[l_List,ll___List]:=
  Block[{i,Sequence},
    i[n_]:=n;
    Scan[(i[#]=Sequence[])&, Union[ll]];
    i/@l]


In[2]:= OrderedComplement[{5,7,1,3,10},{10,7,3}]
Out[2]= {5,1}

In[3]:= OrderedComplement[{5,7,1,3,10},{10},{7,3}]
Out[3]= {5,1}

In[4]:= 0rderedComplement[{5,7,1,3,10}]
Out[4]= {5,7,1,3,10}

("Ordered" here means, the existing order is not destroyed, rather than
re-ordered.)


Here also a more straightforward alternative solution:

In[12]:= UnsortedComplement[l_List, ll___List] := 
  l[[ Sort[Flatten[Position[l, #] & /@ Complement[l, ll]]] ]]

In[13]:= UnsortedComplement[{5, 7, 1, 3, 10}, {10, 7, 3}]
Out[13]= {5, 1}

In[14]:= UnsortedComplement[{5, 7, 1, 3, 10}, {10}, {7, 3}]
Out[14]= {5, 1}

In[15]:= UnsortedComplement[{5, 7, 1, 3, 10}]
Out[15]= {5, 7, 1, 3, 10}

This may be advantageous when the complement is small (but the lists were
large). I didn't test however.

--
Hartmut Wolf



  • Prev by Date: Re: FullSimplify and FunctionExpand
  • Next by Date: RE: SsssComplement?
  • Previous by thread: Re: SsssComplement?
  • Next by thread: RE: SsssComplement?