# Distinguishing between elements of a given list

In a previous question I asked how to distinguish between the roots of a system of equations. However I did not obtain any working solution, so let's make things simpler now.

Assuming we have the following list

data = {{-0.613042, 3.72089*10^-20}, {0.3, 0.0631203},
{0.3, -0.0631203}, {0.302369, 8.6956*10^-17},
{0.969193, 4.35961*10^-19}}


As we can see, it contains five elements corresponding to five points on the $(x,y)$ plane. Three of them lie on the $x$ axis, while the other two have non zero value of $y$.

I want to create a second list, data2, containing the following information

data2 = {xL1, xL2, xL3, xL4, yL4}


where

• xL2 is the root on the $x$ axis with the highest value of $x$,
• xL3 is the root on the $x$ axis with the smallest value of $x$,
• xL1 is the root on the $x$ axis which is between xL2 and xL3,
• xL4 is the root on the (x,y) plane with non zero value of $y$,
• yL4 is the corresponding value of xL4.

For this example we have xL1 = 0.302369, xL2 = 0.969193, xL3 = -0.613042, and (xL4, yL4) = (0.3, 0.0631203).

Note 1: The determination of the elements of data2 should be done automatically for every given list (data).

Note 2: In some cases the initial list could contain only three elements which are the three points on the $x$ axis. For example we could have

data = {{-2.00803, 2.31869*10^-23}, {0.738218, -1.29609*10^-15},
{1.75066, -1.61207*10^-18}}


In this case, data2 should contain only data2 = {xL1, xL2, xL3} without printing Null in the 4th and 5th position.

Any ideas on how to achieve this?

• Are xL1 etc just the x-value of the root, or the x-y tuple? – Quantum_Oli Oct 17 '16 at 9:30
• @Quantum_Oli It's just the x-value. For this example we have xL1 = 0.302369, xL2 = 0.969193, xL3 = -0.613042, and (xL4, yL4) = (0.3, 0.0631203). – Vaggelis_Z Oct 17 '16 at 9:37
• I think your definition does not specify that yL4 should be positive. – Quantum_Oli Oct 17 '16 at 9:44
• @Quantum_Oli Both roots with non-zero values of $y$ have the same absolute value of $y$, so it does not really matter whether you take the positive or the negative one. We could always take the absolute value. – Vaggelis_Z Oct 17 '16 at 9:46

We use SortBy with two sorting criteria to first produce those roots on the x-axis, then the final two (if present):

SecondList[data_] := Module[{data2},
data2 = SortBy[Chop@data, {Abs[Last[#]] &, First}];
Join[data2[[{2, 3, 1}, 1]], If[Length[data2] > 3, {data[[4,1]], Abs[data2[[4,2]]]}, {}]]
]

data = {{-0.613042, 3.72089*10^-20}, {0.3, 0.0631203},
{0.3, -0.0631203}, {0.302369, 8.6956*10^-17},
{0.969193, 4.35961*10^-19}}

SecondList[data]


{0.302369, 0.969193, -0.613042, 0.3, 0.0631203}

data = {{-2.00803, 2.31869*10^-23}, {0.738218, -1.29609*10^-15},
{1.75066, -1.61207*10^-18}}

SecondList[data]


{0.738218, 1.75066, -2.00803}

• It works, however according to the current definition in the module it prints (xL1, xL3, xL2, xL4, yL4). It should be SecondList[data_] := Module[{data2}, data2 = SortBy[Chop@data, {Abs[Last[#]] &, First}]; Join[data2[[{2, 3, 1}, 1]], If[Length[data2] > 3, Abs[data2[[4]]], {}]] ] – Vaggelis_Z Oct 17 '16 at 10:00
• Well spotted, easily fixed. – Quantum_Oli Oct 17 '16 at 10:02
• I noticed that in the case of 5 roots the Abs applies on both the x and y coordinates. This is bad. It should apply only on the y coordinate. – Vaggelis_Z Oct 17 '16 at 10:04
• Again, easily fixed. Sloppy this morning..! – Quantum_Oli Oct 17 '16 at 10:15

I am not sure what you want to say xL4 and yL4 as whole data is on xy plane. For the others

data = {{-0.613042, 3.72089*10^-20}, {0.3, 0.0631203}, {0.3, -0.0631203},
{0.302369, 8.6956*10^-17}, {0.969193, 4.35961*10^-19}};

xL2[list__] := Sort[Select[list, Abs[#[[2]]] < 10^-13 &], #1[[1]] > #2[[1]] &][[1,1]]
xL3[list__] := Sort[Select[list, Abs[#[[2]]] < 10^-13 &], #1[[1]] < #2[[1]] &][[1,1]]
xL1[list__] := Select[list,Abs[#[[2]]] < 10^-13 && xL2[list] > #[[1]] > xL3[list] &][[1, 1]]

xL2[data]
xL3[data]
xL1[data]


0.969193

-0.613042

0.302369

I think you get the idea.