Problems in dealing with sublists

I have a list made by several sublists of two different kinds: the first kind of sublist has just one element, the second kind has three elements. I also have two functions: I would like to apply the first function to the elements of those sublists that have only one component, and the other function to the elements of those sublists with three components.

To give you some context:

I am interested in studying the points of intersection between a function and a constant function, for different values of the constant function itself (that I called $$l$$).

To see the function, run the following code:

cdf1[x_] = CDF[NormalDistribution[1, 1], x];
pdf1[x_] = PDF[NormalDistribution[1, 1], x];
cdfm1[x_] = cdf1[x]^20;
pdfm1[x_] = cdfm1'[x];
cdf[x_] = CDF[NormalDistribution[0, 1], x];
pdf[x_] = PDF[NormalDistribution[0, 1], x];
cdfm[x_] = cdf[x]^20;
pdfm[x_] = cdfm'[x];
mix1[x_, h_] = h  pdf1[x] + (1 - h) pdfm1[x];
mix[x_, h_] = h pdf[x] + (1 - h) pdfm [x];
ratio[x_, h_] = mix1[x, h]/mix[x, h];
Manipulate[Plot[{ratio[x, 0.4], l}, {x, -6, 7}, PlotRange -> 1.5], {l, 0, 1.5}]


As you can see, for very low or very high values of $$l$$ there is only one point of intersection, for all the other values of $$l$$ there are three points of intersection.

I obtained all the points of intersection for different values of $$l$$ with this code:

sol = Solve[ratio[x, 0.4] == l && (-10 < x < 10), x]
tab = Table[x /. sol, {l, 0.1, 1.5, 0.1}]

{{-1.80259}, {-1.10944}, {-0.703973, 1.52579, 1.7873}, {-0.416291,
1.32432, 1.9424}, {-0.193145, 1.21091, 2.02258}, {-0.0107771,
1.12506, 2.08055}, {0.143772, 1.0518, 2.12697}, {0.279357, 0.983264,
2.16616}, {0.404907, 0.912193, 2.20033}, {0.538168, 0.822835,
2.23079}, {2.2584}, {2.28373}, {2.30719}, {2.3291}, {2.34968}}


So we have a list made of sublists of two kinds: either {a} or {b,c,d}, with $$b. I would like to obtain a list that displays the value 1-cdf[a] if the sublist is made by $$a$$ only, and that displays the value cdf[c]-cdf[b]+1-cdf[d] if the sublist is made of three element. So, for example, in the case of the list I provided, I would like to get another list like this:

{1-cdf[-1.80259], 1-cdf[-1.10944], cdf[1.52579]-cdf[-0.703973]+1-cdf[1.7873]...}


with values instead of the expression, of course. Thank you very much for any help you can provide!

You can achieve what you're after creating a function and overloading it for 1 and 3 arguments:

func[a_]:=1-cdf[a]
func[b_,c_,d_]:=cdf[c]-cdf[b]+1-cdf[d]


Now, Applying func to your list gives the result:

func @@@ tab
(* {0.964273, 0.866379, 0.732689, 0.594747, 0.485171, 0.392755, 0.313105, 0.242396,
0.175831, 0.102777, 0.0119603, 0.0111937, 0.0105221, 0.00992698, 0.00939481} *)


You can also use Replace to achieve the same without an additional function:

Replace[
tab,
{
{a_} :> 1 - cdf[a],
{b_, c_, d_} :> cdf[c] - cdf[b] + 1 - cdf[d]
},
1
]

• Thank you very much! – Api Dec 28 '18 at 17:53

A basic approach to your problem

 fun[list_] :=
Table[Which[Length[list[[i]]] == 1, First[1 - cdf[list[[i]]]],
Length[list[[i]]] == 3,
cdf[list[[i, 2]]] - cdf[list[[i, 1]]] + 1 - cdf[list[[i, 3]]]], {i,
1, Length[tab]}]

fun[tab]
(*{0.964274, 0.86638, 0.732689, 0.594747, 0.485171, 0.392755, 0.313106,
0.242396, 0.175831, 0.102777, 0.0119604, 0.0111937, 0.0105221,0.00992688, 0.00939478}*)