Function Repository Resource:

ConvexDefect

Source Notebook

Compute the elements missing from a subset that prevent it from being order convex

Contributed by: E. Chan-López, A. Martín-Ruiz and Carlos Antonio Marín Mendoza

ResourceFunction["ConvexDefect"][subset,parentSet]

returns the elements of parentSet lying in Interval[MinMax[subset]] that are not contained in subset, i.e. the "gaps" preventing order-convexity.

Details and Options

ResourceFunction["ConvexDefect"][subset,parentSet] computes .
Returns {} if and only if subset is order-convex in parentSet.
Returns elements in the convex closure not in subset.
Measures deviation from order-convexity.
Uses the ordering specified by the option "ComparisonFunction" to determine convexity.
The returned list is sorted, as produced by Complement.
Supports numeric elements, strings, symbols and custom orderings via "ComparisonFunction".
Satisfies .
The union is disjoint.
The term "defect" emphasizes that this quantity measures the deviation from order-convexity, rather than merely representing a set-theoretic complement.

Examples

Basic Examples (5) 

Identify the exact gaps in a non-convex subset:

In[1]:=
ResourceFunction["ConvexDefect"][{1, 3, 7, 9}, Range[10]]
Out[1]=

A convex set has no defect:

In[2]:=
ResourceFunction["ConvexDefect"][{3, 4, 5}, Range[10]]
Out[2]=

The defect size quantifies "how far" from convex:

In[3]:=
Length[ResourceFunction["ConvexDefect"][{1, 10}, Range[10]]]
Out[3]=

Non-contiguous parent set:

In[4]:=
ResourceFunction["ConvexDefect"][{10, 30}, {5, 10, 20, 30, 50}]
Out[4]=

The empty set has no defect:

In[5]:=
ResourceFunction["ConvexDefect"][{}, Range[100]]
Out[5]=

Scope (4) 

Works with rational elements:

In[6]:=
ResourceFunction["ConvexDefect"][{1/4, 3/4}, {0, 1/4, 1/2, 3/4, 1}]
Out[6]=

Works with strings under canonical (lexicographic) order:

In[7]:=
ResourceFunction[
 "ConvexDefect"][{"b", "e"}, {"a", "b", "c", "d", "e", "f"}]
Out[7]=

Works with symbolic heads that have a canonical ordering:

In[8]:=
ResourceFunction["ConvexDefect"][{a, d}, {a, b, c, d, e}]
Out[8]=

A singleton always has an empty defect:

In[9]:=
ResourceFunction["ConvexDefect"][{5}, Range[10]]
Out[9]=

Options (2) 

ConvexDefect supports a "ComparisonFunction" option to specify a custom ordering. Under such an ordering, the defect reflects the gaps with respect to the induced order:

In[10]:=
ResourceFunction["ConvexDefect"][{"ab", "abcde"},
 	{"a", "ab", "xyz", "test", "abcde", "longer"},
 	"ComparisonFunction" -> Function[LessEqual[StringLength[#], StringLength @ #2]]
 ]
Out[10]=

Under the default LessEqual, the same call gives a different defect because the canonical order is lexicographic, not length-based:

In[11]:=
ResourceFunction["ConvexDefect"][{"ab", "abcde"},
 	{"a", "ab", "xyz", "test", "abcde", "longer"}
 ]
Out[11]=

Applications (3) 

Identify missing sensor readings in an active measurement range:

In[12]:=
ResourceFunction["ConvexDefect"][{20, 25, 30, 40, 45}, Range[0, 100, 5]]
Out[12]=

Find which days are missing from a schedule within its active range:

In[13]:=
With[
 	{
  		allDays = Range[1, 31],
  		scheduled = {5, 6, 7, 8, 10, 11, 12, 13, 14, 15}
  	},
 	ResourceFunction["ConvexDefect"][scheduled, allDays]
 ]
Out[13]=

Detect missing entries in a sorted database key range:

In[14]:=
ResourceFunction["ConvexDefect"][{100, 200, 400, 500}, Range[100, 600, 100]]
Out[14]=

Properties and Relations (5) 

OrderConvexQ returns True if and only if the defect is empty:

In[15]:=
With[{s = {2, 3, 5}, p = Range @ 10},
 	SameQ[ResourceFunction["OrderConvexQ"][s, p],
  		SameQ[ResourceFunction["ConvexDefect"][s, p], {}]
  	]
 ]
Out[15]=

The defect size equals the number of elements in the interval Interval[MinMax[subset]] relative to parent set that are not in the subset:

In[16]:=
With[{s = {1, 5, 9}, p = Range @ 10},
 	Equal[Length @ ResourceFunction["ConvexDefect"][s, p],
  		Length[Select[p, LessEqual[Min[s], #, Max @ s] &]] - Length[s]
  	]
 ]
Out[16]=

The interval induced by a subset decomposes into the subset and its defect:

In[17]:=
With[{s = {1, 3, 7}, p = Range @ 10},
 	SameQ[Sort[Select[p, LessEqual[Min @ s, #, Max @ s] &]],
  		Sort @ Join[s, ResourceFunction["ConvexDefect"][s, p]]
  	]
 ]
Out[17]=

A subset containing all elements of its induced interval has empty defect:

In[18]:=
With[{s = {2, 8}, p = Range @ 10},
 	ResourceFunction["ConvexDefect"][
  Select[p, LessEqual[Min @ s, #, Max @ s] &], p]
 ]
Out[18]=

Monotonicity. If , then :

In[19]:=
With[
 	{
  		p = Range @ 10,
  		a = {2, 8},
  		b = {2, 5, 8}
  	},
 	SubsetQ[ResourceFunction["ConvexDefect"][a, p], ResourceFunction["ConvexDefect"][b, p]]
 ]
Out[19]=

Possible Issues (1) 

Requires a finite totally ordered parent set; elements must admit comparison via LessEqual or a user-defined ordering. Does not verify . If subset contains elements absent from parent set, the defect is computed relative to the closure filtered from parent set. If subset contains elements not in parent set, the defect only reports gaps from parent set:

In[20]:=
ResourceFunction["ConvexDefect"][{2, 8}, {1, 3, 5, 7, 9}]
Out[20]=

The elements 2 and 8 act as bounds but are not themselves in parent set, so the entire closure interval from parent set appears as defect.

Neat Examples (3) 

Visualize the convex defect of a subset on a number line. Grey marks the parent set, green the subset, red the defect (the missing elements):

In[21]:=
With[{s = {2, 5, 9}, p = Range @ 12},
 	NumberLinePlot[{p, s, ResourceFunction["ConvexDefect"][s, p]},
  		PlotStyle -> {Gray, Darker[Green], Red},
  		PlotLegends -> {"Parent Set", "Subset", "Defect"},
  		Spacings -> 0.5
  	]
 ]
Out[21]=

Plot how the defect size of the logistic map orbit varies with the parameter . The defect measures the number of grid points within the convex hull of the attractor that are not visited by the orbit at the chosen resolution, providing a discrete fingerprint of its Cantor-like fractal structure:

In[22]:=
ListLinePlot[
 	Table[
  		Module[{grid, orbit, disc},
   			grid = Range[0., 1., 0.001];
   			orbit = NestList[Function[r * # * (1 - #)], 0.1, 5000];
   			disc = DeleteDuplicates @ Round[orbit, 0.001];
   			{r, Length @ ResourceFunction["ConvexDefect"][disc, grid]}
   		],
  		{r, 3.5, 4., 0.005}
  	],
 	AxesLabel -> {"r", "|Defect|"},
 	PlotLabel -> "Convex Defect Size of the Logistic Map Attractor", Filling -> Bottom, PlotRange -> All
 ]
Out[22]=

The spikes correspond to periodic windows where the orbit collapses to a finite cycle, leaving a large convex hull mostly empty. Outside these windows, the defect decreases as the chaotic attractor becomes increasingly dense in the interval. At , the defect approaches zero, the orbit densely fills , leaving no gaps at the chosen discretization scale.


Compare the defect structure at two contrasting parameter values, one inside a periodic window (, period-3) and one fully chaotic ():

In[23]:=
With[{res = 0.001},
 	Module[{grid, f},
  		grid = Range[0., 1., res];
  		f[
Pattern[r, 
Blank[]]] := Module[
    			{disc},
    			disc = DeleteDuplicates[
      				Round[NestList[Function[r * # * (1 - #)], 0.1, 10000], res]
      			];
    			ResourceFunction["ConvexDefect"][disc, grid]
    		];
  		Column @ {
    			Row @ {"r = 3.83: |Defect| = ", Length @ f @ 3.83},
    			Row @ {"r = 4.00: |Defect| = ", Length @ f @ 4.}
    		}
  	]
 ]
Out[23]=

At , the system lies within a period-3 window of the logistic map. The orbit settles onto a 3-cycle whose three limit points occupy a negligible portion of the convex hull, leaving 796 out of roughly 800 grid points as defect - an essentially hollow interval. This provides a discrete manifestation of the fact that stable periodic orbits have zero Lebesgue measure.

At , the situation is subtler. The map is topologically conjugate to the tent map via the Ulam–von Neumann substitution , and its invariant measure is the arcsine distribution

The density diverges at the endpoints and attains its minimum at , so the orbit samples the interval least frequently near its center. The observed defect of 1 at resolution 0.001 reflects this: even after iterations, the region of minimal invariant density may still contain a grid point not yet visited by the orbit. In the , limit the defect vanishes, consistent with the ergodicity of the fully chaotic regime.

Publisher

Ramón Eduardo Chan López

Version History

  • 1.0.0 – 01 April 2026

Source Metadata

Related Resources

Author Notes

ConvexDefect quantifies the failure of a subset to be order-convex within a finite totally ordered set. The ambient order is determined by a parent set, whose elements are assumed to support comparison via LessEqual or a custom ordering. The function returns the set difference between the order-convex hull of the subset and the subset itself, corresponding to the missing elements required for interval completeness. It is particularly useful for analyzing gaps in discretized domains, including integer grids and sampled angular coordinates.

License Information