Difference between revisions of "Interval (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
m
(Add more creation examples for open and closed intervals using constructor function)
 
(One intermediate revision by the same user not shown)
Line 12: Line 12:
 
Context("Interval");
 
Context("Interval");
 
 
$I = Interval("[",0,Infinity,")"); # the hard way
+
$I = Interval(0,1); # the open interval (0,1)
  +
$I = Interval([0,1]); # the closed interval [0,1]
  +
  +
$I = Compute("(0,1)");
  +
$I = Compute("[0,1]");
  +
  +
$I = Interval("[",0,Infinity,")"); # half-open the hard way
 
$I = Interval("[0,infinity)"); # the easy way
 
$I = Interval("[0,infinity)"); # the easy way
 
$I = Compute("[0,infinity)");
 
$I = Compute("[0,infinity)");
  +
  +
Note that you can produce open and closed Intervals using the constructor function without explicit delimiters, but for half-open Intervals, you need to supply the delimiters; it is easiest to do that using a single string that will be parsed rather than the delimiters and endpoints individually.
   
   
Line 84: Line 86:
 
=== Methods ===
 
=== Methods ===
   
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional ones:
+
The Interval class supports the [[Common MathObject Methods]], and the following additional ones:
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 133: Line 135:
 
=== Properties ===
 
=== Properties ===
   
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject properties]], and the following additional ones:
+
The Interval class supports the [[Common MathObject Properties]], and the following additional ones:
   
 
{| class="wikitable"
 
{| class="wikitable"

Latest revision as of 12:50, 12 August 2012

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}). Intervals are most often created in the Interval Context.

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.


Construction

Intervals are created via the Interval() function, or by Compute().

   Context("Interval");
   
   $I = Interval(0,1);            # the open interval (0,1)
   $I = Interval([0,1]);          # the closed interval [0,1]
   
   $I = Compute("(0,1)");
   $I = Compute("[0,1]");
   
   $I = Interval("[",0,Infinity,")");    # half-open the hard way
   $I = Interval("[0,infinity)");        # the easy way
   $I = Compute("[0,infinity)");

Note that you can produce open and closed Intervals using the constructor function without explicit delimiters, but for half-open Intervals, you need to supply the delimiters; it is easiest to do that using a single string that will be parsed rather than the delimiters and endpoints individually.


Operations on Intervals

The union of two Intervals is represent by an upper-case U in student answers and parsed strings, and by addition or the dot operator 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.

   $I1 = Interval("(-infinity,-1]");
   $I2 = Interval("[1,infinity)");
   
   $U = $I1 + $I2;
   $U = $I1 . $I2;
   $U = Union($I1,$I2);
   $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

When intervals are compared, they must match not only the endpoints, but also the type of endpoint (open or closed), though an answer checker option can be used to change that for student answers. This condition can be relaxed, however, by setting the ignoreEndpointTypes flag in the Context:

   Context()->flags->set(ignoreEndpointTypes => 1);

This will mean that the open/closed type of each endpoint will be ignored, and so intervals only have to match the numbers at the endpoints, not the type.


Answer Checker

As with all MathObjects, you obtain an answer checker for an Interval object via the cmp() method:

   ANS(Compute("[1,infinity)")->cmp);

The Interval class supports the common answer-checker options, and the following additional options:

Option Description Default
showEndpointHints => 1 or 0 Do/don't show messages about which endpoints are correct. 1
showEndTypeHints => 1 or 0 Do/don't show messages about whether the open/closed status of the end-points are correct (only shown when the end-points themselves are correct). 1
requireParenMatch => 1 or 0 Do/don't require that the open/closed status of the end-points be correct. 1


Methods

The Interval class supports the Common MathObject Methods, and the following additional ones:

Method Description
$I1->intersect($I2) Returns the intersection of $I1 with $I2. Note that $I2 can be an Interval, Set, or Union.
$I1->intersects($I2) Returns true if $I1 intersects $I2, and undef otherwise. Note that $I2 can be an Interval, Set, or Union.
$I1->contains($I2) Returns true if $I2 is a subset of $I1, and undef otherwise. Note that $I2 can be an Interval, Set, or Union.
$I1->isSubsetOf($I2) Returns true if $I1 is a subset of $I2, and undef otherwise. Note that $I2 can be an Interval, Set, or Union.

In addition to these, the following are defined so that Intervals have the same methods as Unions and Sets; that way, you can call these on a student's input that might be an Interval, Set, or Union, without having to check the type of object first.

Method Description
$I->isEmpty Returns 0.
$I->isReduced Returns 1.
$I->reduce Returns $I itself.
$I->sort Returns $I itself.


Properties

The Interval class supports the Common MathObject Properties, and the following additional ones:

Property Description Default
$r->{open} ( for an open left-hand endpoint, [ for closed. given when created
$r->{close} ) for an open right-hand endpoint, ] for closed. given when created