Difference between revisions of "Interval (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
(Break into sections, add Answer Checker, Methods, and Properties)
m (Fix some typos)
Line 74: Line 74:
   
   
=== Methods ===>
+
=== Methods ===
   
 
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional ones:
 
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional ones:
Line 106: Line 106:
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
 
| style="padding: 5px; white-space: nowrap" | <code>$I->isEmtpy</code>
 
| style="padding: 5px; white-space: nowrap" | <code>$I->isEmtpy</code>
| style="padding: 5px" | Returns </code>0</code>.
+
| style="padding: 5px" | Returns <code>0</code>.
   
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
 
| style="padding: 5px; white-space: nowrap" | <code>$I->isReduced</code>
 
| style="padding: 5px; white-space: nowrap" | <code>$I->isReduced</code>
| style="padding: 5px" | Returns </code>1</code>.
+
| style="padding: 5px" | Returns <code>1</code>.
   
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
 
| style="padding: 5px; white-space: nowrap" | <code>$I->reduce</code>
 
| style="padding: 5px; white-space: nowrap" | <code>$I->reduce</code>
| style="padding: 5px" | Returns </code>$I</code> itself.
+
| style="padding: 5px" | Returns <code>$I</code> itself.
   
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
 
| style="padding: 5px; white-space: nowrap" | <code>$I->sort</code>
 
| style="padding: 5px; white-space: nowrap" | <code>$I->sort</code>
| style="padding: 5px" | Returns </code>$I</code> itself.
+
| style="padding: 5px" | Returns <code>$I</code> itself.
   
 
|}
 
|}

Revision as of 16:29, 5 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}).

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

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

   Context("Interval");
   
   $I = Interval("[",0,Infinity,")");    # the hard way
   $I = Interval("[0,infinity)");        # the easy way
   $I = Compute("[0,infinity)");

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


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 1 if $I1 intersects $I2, and 0 otherwise. Note that $I2 can be an Interval, Set, or Union.
$I1->contains($I2) Returns 1 if $I2 is a subset of $I1, and 0 otherwise. Note that $I2 can be an Interval, Set, or Union.
$I1->isSubsetOf($I2) Returns 1 if $I1 is a subset of $I2, and 0 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->isEmtpy 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