Interval (MathObject Class)
The Interval Class
The Interval class implements intervals on the real line. They can be open or closed at each end, and can be infinite. For example, (0,infinity)
is the set of [math]x[/math] where [math]x \gt 0[/math] and [-1,1]
is the set of [math]x[/math] where [math]-1\le x\le 1[/math]. The interval (-infinity,infinity)
is the entire real line (and the constant R
refers to this set in the Interval Context). The individual point [math]a[/math] on the line can be represented as [a,a]
, but this is better handled via a Set (i.e., {a}
).
Context("Interval"); $I = Interval("[",0,Infinity,")"); # the hard way $I = Interval("[0,infinity)"); # the easy way $I = Compute("[0,infinity)");
The union of two Intervals is represent by an upper-case U
in student answers and parsed strings and by addition or the Union()
constructor in Perl code. Differences of intervals can be obtained via subtraction. Intervals can be combined with Sets or Unions in these ways as well.
$U = Interval("(-infinity,-1]") + Interval("[1,infinity)"); $U = Union(Interval("(-infinity,-1]"),Interval("[1,infinity)")); $U = Union("(-infinity,-1] U [1,infinity)"); $U = Compute("(-infinity,-1] U [1,infinity)"); $S = Interval("(-infinity,1]") - Interval("[-1,1)"); # same as Compute("(-infinity,-1) U {1}"); $S = Compute("(-infinity,1] - [-1,1)"); # same as above $S = Compute("R - (-1,1)"); # same as $U above
Intersections of Intervals (or Sets or Unions) can be obtained via the intersect
method of an Interval. There is no built-in method for students to form intersections (though one could be added to the Context by hand). There are other methods for determining if one Interval is contained in another, or intersects another, or is a subset of another, etc. These methods can be applied to Sets and Unions in addition to Intervals.
$I1 = Interval("(-infinity,1]"); $I2 = Interval("(-1,5]"); $I3 = $I1->intersect($I2); # same as Interval("(-1,1]"); $I1->contains($I2); # returns false $I3->isSubsetOf($I2); # returns true $I1->intersects($I2); # returns true
The answer checker for Intervals can give hints about whether each endpoint is correct or not, and about whether the type of endpoint (open/closed) is correct or not. These features can be controlled through flags for the answer checker.