Difference between revisions of "Interval (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
m (Corrected broken link)
(Break into sections, add Answer Checker, Methods, and Properties)
Line 1: Line 1:
== The Interval Class ==
+
=== 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, <code>(0,infinity)</code> is the set of <math>x</math> where <math>x > 0</math> and <code>[-1,1]</code> is the set of <math>x</math> where <math>-1\le x\le 1</math>. The interval <code>(-infinity,infinity)</code> is the entire real line (and the constant <code>R</code> refers to this set in the Interval Context). The individual point <math>a</math> on the line can be represented as <code>[a,a]</code>, but this is better handled via a Set (i.e., <code>{a}</code>).
 
The Interval class implements intervals on the real line. They can be open or closed at each end, and can be infinite. For example, <code>(0,infinity)</code> is the set of <math>x</math> where <math>x > 0</math> and <code>[-1,1]</code> is the set of <math>x</math> where <math>-1\le x\le 1</math>. The interval <code>(-infinity,infinity)</code> is the entire real line (and the constant <code>R</code> refers to this set in the Interval Context). The individual point <math>a</math> on the line can be represented as <code>[a,a]</code>, but this is better handled via a Set (i.e., <code>{a}</code>).
  +
  +
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 <code>Interval()</code> function, or by <code>Compute()</code>.
   
 
Context("Interval");
 
Context("Interval");
Line 9: Line 15:
 
$I = Compute("[0,infinity)");
 
$I = Compute("[0,infinity)");
   
The union of two Intervals is represent by an upper-case <code>U</code> in student answers and parsed strings and by addition or the <code>Union()</code> 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.
 
  +
=== Operations on Intervals ===
   
$U = Interval("(-infinity,-1]") + Interval("[1,infinity)");
 
  +
The union of two Intervals is represent by an upper-case <code>U</code> in student answers and parsed strings, and by addition or the dot operator or the <code>Union()</code> 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 = Union(Interval("(-infinity,-1]"),Interval("[1,infinity)"));
 
  +
  +
$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 = Union("(-infinity,-1] U [1,infinity)");
 
$U = Compute("(-infinity,-1] U [1,infinity)");
 
$U = Compute("(-infinity,-1] U [1,infinity)");
Line 32: Line 44:
 
$I1->intersects($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.
 
  +
  +
=== Answer Checker ===
  +
  +
As with all MathObjects, you obtain an answer checker for an Interval object via the <code>cmp()</code> method:
  +
  +
ANS(Compute("[1,infinity)")->cmp);
  +
  +
The Interval class supports the [[Answer_Checker_Options_(MathObjects)| common answer-checker options]], and the following additional options:
  +
  +
{| class="wikitable"
  +
! Option !! Description !! style="padding:5px" | Default
  +
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>showEndpointHints => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Do/don't show messages about which endpoints are correct.
  +
| style="text-align:center" | <code>1</code>
  +
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>showEndTypeHints => 1</code> or <code>0</code>
  +
| style="padding: 5px" | 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).
  +
| style="text-align:center" | <code>1</code>
  +
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>requireParenMatch => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Do/don't require that the open/closed status of the end-points be correct.
  +
| style="text-align:center" | <code>1</code>
  +
  +
|}
  +
  +
  +
=== Methods ===>
  +
  +
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional ones:
  +
  +
{| class="wikitable"
  +
! Method !! Description
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I1->intersect($I2)</code>
  +
| style="padding: 5px" | Returns the intersection of <code>$I1</code> with <code>$I2</code>. Note that <code>$I2</code> can be an Interval, Set, or Union.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I1->intersects($I2)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$I1</code> intersects <code>$I2</code>, and <code>0</code> otherwise. Note that <code>$I2</code> can be an Interval, Set, or Union.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I1->contains($I2)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$I2</code> is a subset of <code>$I1</code>, and <code>0</code> otherwise. Note that <code>$I2</code> can be an Interval, Set, or Union.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I1->isSubsetOf($I2)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$I1</code> is a subset of <code>$I2</code>, and <code>0</code> otherwise. Note that <code>$I2</code> 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.
  +
  +
{| class="wikitable"
  +
! Method !! Description
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I->isEmtpy</code>
  +
| style="padding: 5px" | Returns </code>0</code>.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I->isReduced</code>
  +
| style="padding: 5px" | Returns </code>1</code>.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I->reduce</code>
  +
| style="padding: 5px" | Returns </code>$I</code> itself.
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$I->sort</code>
  +
| style="padding: 5px" | Returns </code>$I</code> itself.
  +
  +
|}
  +
  +
  +
=== Properties ===
  +
  +
The Interval class supports the [[MathObject_Methods_and_Properties| common MathObject properties]], and the following additional ones:
  +
  +
{| class="wikitable"
  +
! Property !! Description !! style="padding:5px" | Default
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$r->{open}</code>
  +
| style="padding: 5px" | <code>(</code> for an open left-hand endpoint, <code>[</code> for closed.
  +
| style="text-align:center; padding:5px" | given when created
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$r->{close}</code>
  +
| style="padding: 5px" | <code>)</code> for an open right-hand endpoint, <code>]</code> for closed.
  +
| style="text-align:center; padding:5px" | given when created
  +
|}
  +
   
 
<br>
 
<br>

Revision as of 16:28, 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