I have two lists consisting of element pairs:
a1 = Table[{i, i^2}, {i, 10}] // N;
a2 = Table[{2 i, Sqrt[i]}, {i, 5}] // N;
I want to pull out from both lists those pairs which have the same first element. Using Intersection with the SameTest option:
In[84]:= d1 = Intersection[a1, a2, SameTest -> (First[#1] === First[#2] &)]
Out[84]= {{2., 4.}, {4., 16.}, {6., 36.}, {8., 64.}, {10., 100.}}
gives a different result as compared to
In[85]:= d2 = Intersection[a2, a1, SameTest -> (First[#1] === First[#2] &)]
Out[85]= {{2., 1.}, {4., 1.41421}, {6., 1.73205}, {8., 2.}, {10., 2.23607}}
d1 contains the element pairs of the list a1 for which there exists also a similar first element list a2. d2 contains the element pairs of a2 for which there exists also a similar first element in list a1. Thus Intersection is not commutative.
I would like to get as "Intersection" all element pairs a1 and of a2 for which there is a match in the first element. I can of course call Intersection as shown above two times and then join the results d1 and d2. But is there not a more efficient way so that I need to call Intersection only once? I ask because in my real case the lists are very large and the SameTest is more complicated than the identity test used in this example.
{{2., 4., 1.}, {4., 16. 1.41}, ..., {10., 100., 2.23}}
? $\endgroup$Abs[#1-#2]<epsilon
. $\endgroup${...}
? $\endgroup$