RE: Interval arithmetic, of a sort

*To*: mathgroup at smc.vnet.net*Subject*: [mg33762] RE: [mg33721] Interval arithmetic, of a sort*From*: F.H.Simons at tue.nl*Date*: Thu, 11 Apr 2002 02:14:23 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Here is a function that does the job: visibleintervals[intervals_] := Module[{f, n = Length[intervals] + 1, res}, (* define the function *) (n = n - 1; f[x_ /; #[[1]] <= x <= #[[2]]] = n) & /@ Reverse[intervals]; (* find all subintervals and the corresponding value of the function *) res = {f[(#[[1]] + #[[2]])/2], #} & /@ Partition[Union @@ intervals, 2, 1]; (* combine neighbouring subintervals with the same function value *) {#[[1, 1]], {#[[1, 2, 1]], #[[-1, 2, 2]]}} & /@ Split[res, #1[[1]] == #2[[1]] && #1[[2, 2]] == #2[[2, 1]] &] ] visibleintervals[{{0, 100}, {2, 3}, {1, 20}, {15, 30}}] {{1, {0, 1}}, {3, {1, 15}}, {4, {15, 30}}, {1, {30, 100}}} In the first line of the Module for each interval the function f is defined only on that interval with value the number of that interval. We start with the last interval, so our definitions are ordered according to decreasing function values. Mathematica uses the definitions in this order, so for any argument in any interval the returned value is the highest interval. Fred Simons Eindhoven University of Technology