Prev: InputField Rounding Problem
Next: does not work?
From: M.Roellig on 28 Apr 2010 01:59 > 2) is there better way to implement intersection check? > Hi. In[1]:= Intersection[{a, b}, {c, a}] Out[1]= {a} or if you like a True/False answer In[2]:= Intersection[{a, b}, {c, a}] =!= {} Out[2]= True Cheers, Markus
From: Dims on 28 Apr 2010 01:57 On 27 =D0=B0=D0=BF=D1=80, 16:28, Albert Retey <a...(a)gmx-topmail.de> wrote: > Am 27.04.2010 13:41, schrieb Dims: > Intersects[x_List, y_List] := Catch[ Yes, thank you, I also came to conclusion, that catch works. But I think it is a bug with return, since manual says, that we CAN use Return[] inside Do[].
From: Dims on 28 Apr 2010 07:00 On 28 =D0=B0=D0=BF=D1=80, 09:59, "M.Roellig" <markus.roel...(a)googlemail.com= > wrote: > In[2]:= Intersection[{a, b}, {c, a}] =!= {} But would Mathematica optimize evaluation of this expression, i.e., terminate searching for common elements if one already found?
From: dh on 29 Apr 2010 02:52 On 27.04.2010 13:41, Dims wrote: > I wrote a function to check if two lists contain equal members: > > Intersects[x_List, > y_List] := (Do[(Print["Outer step ", xi]; > Do[(Print[xi, yi]; > If[xi == yi, (Print["Returning True"]; Return[True])]), {yi, > y}]), {xi, x}]; False) > > But it returns False for interecting lists: > > In[47]:= Intersects[{a, b}, {c, a}] > During evaluation of In[47]:= Outer step a > During evaluation of In[47]:= ac > During evaluation of In[47]:= aa > During evaluation of In[47]:= Returning True > During evaluation of In[47]:= Outer step b > During evaluation of In[47]:= bc > During evaluation of In[47]:= ba > Out[47]= False > > Few questions: > > 1) why does not Return[] interrupt the loops? > 2) is there better way to implement intersection check? > > Thanks > Hi, Return does only return a single level . In your case only the inner Do loop is left. Use Throw and Catch to control how many levels to jump up. In general, try to avoid Return. Daniel -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh(a)metrohm.com> Internet:<http://www.metrohm.com>
From: M.Roellig on 29 Apr 2010 02:52
On 28 Apr., 13:00, Dims <dim...(a)hotmail.com> wrote: > On 28 =D0=B0=D0=BF=D1=80, 09:59, "M.Roellig" <markus.roel...(a)googlemail.com> > > wrote: > > In[2]:= Intersection[{a, b}, {c, a}] =!= {} > > But would Mathematica optimize evaluation of this expression, i.e., > terminate searching for common elements if one already found? No, it returns all common elements. But it is very fast. The Do approach scales terribly with the length of the list (N^2) and the position of the first intersecting element. Here is an example timing for two lists of length 10000 RandomSeed[123456789]; AbsoluteTiming(a)Table[Module[{listA, listB}, listA = Insert[RandomReal[{1, 1000}, 10000], 3333, RandomInteger[{1, 10000}]]; listB = Insert[RandomReal[{1001, 2000}, 10000], 3333, RandomInteger[{1, 10000}]]; intersects[listA, listB]], {10}] {537.7829457, {True, True, True, True, True, True, True, True, True, True}} The same with the built in command is almost instantaneous RandomSeed[123456789]; AbsoluteTiming(a)Table[Module[{listA, listB}, listA = Insert[RandomReal[{1, 1000}, 10000], 3333, RandomInteger[{1, 10000}]]; listB = Insert[RandomReal[{1001, 2000}, 10000], 3333, RandomInteger[{1, 10000}]]; Intersection[listA, listB] =!= {}], {10}] {0.2028004, {True, True, True, True, True, True, True, True, True, True}} Markus |