Difference between revisions of "Set (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
m
 
(4 intermediate revisions by one other user not shown)
Line 21: Line 21:
 
Context()->flags->set(reduceSets => 0);
 
Context()->flags->set(reduceSets => 0);
   
This will preserve the Set in whatever form it was originally created (this is set to 0 for student answers so that automatic reductions are not performed). A second flag, <code>reduceSetsForComparison</code>, determines whether Set are reduced (temporarily) before they are compared for equality, or whether the structures must match exactly.
+
This will preserve the Set in whatever form it was originally created (this is set to 0 for student answers so that automatic reductions are not performed). A second flag, <code>reduceSetsForComparison</code>, determines whether Sets are reduced (temporarily) before they are compared for equality, or whether the structures must match exactly.
   
   
 
=== Operations on Sets ===
 
=== Operations on Sets ===
   
Sets can be combined with each other and with Intervals via a Union (represented 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 Sets and other Sets, Intervals, or Unions can be obtained via subtraction.
+
Sets can be combined with each other and with Intervals via a Union (represented 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 Sets and other Sets, Intervals, or Unions can be obtained via subtraction. (Note that in order to ensure that the result of the union operation has only one copy of each element, you need to pass it through the Set constructor)
   
$U = Set(0,1,2) + Set(2,pi,sqrt(2)); # same as Set(0,1,2,pi,sqrt(2));
+
$U = Set(Set(0,1,2) + Set(2,pi,sqrt(2))); # same as Set(0,1,2,pi,sqrt(2));
 
$U = Set("{0,1,2} U {2,pi,sqrt(2)}");
 
$U = Set("{0,1,2} U {2,pi,sqrt(2)}");
 
$U = Union("{0,1,2} U {2,pi,sqrt(2)}");
 
$U = Union("{0,1,2} U {2,pi,sqrt(2)}");
Line 49: Line 49:
 
$S3->isSubsetOf($S2); # returns true
 
$S3->isSubsetOf($S2); # returns true
 
$S1->intersects($S2); # returns true
 
$S1->intersects($S2); # returns true
 
   
 
=== Answer Checker ===
 
=== Answer Checker ===
Line 103: Line 102:
 
| style="padding: 5px" | The string to use in error messages that identifies the Set itself.
 
| style="padding: 5px" | The string to use in error messages that identifies the Set itself.
 
| style="text-align:center; padding:5px" | <code>"a set"</code>
 
| style="text-align:center; padding:5px" | <code>"a set"</code>
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>extra => $object</code>
 
| style="padding: 5px" | A MathObject to use for comparison to student-provided entries that do not match any of the correct answers in the list. Use this in particular when the correct answer is a list of a single String (like <code>NONE</code>), and the expected type of elements in the list are complicated and need special syntax checking (e.g., for size of Vectors).
 
| style="text-align:center; padding:5px" | determined from entries
 
   
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>typeMatch => $object</code>
 
| style="padding: 5px; white-space: nowrap" | <code>typeMatch => $object</code>
| style="padding: 5px" | Specifies the type of object that the student should be allowed to enter in the list (determines what constitutes a type mismatch error). Can be either a MathObject or a string that is the class of a MathObject (e.g., <code>"Value::Vector"</code>).
+
| style="padding: 5px" | Specifies the type of object that the student should be allowed to enter in the Set (determines what constitutes a type mismatch error). Can be either a MathObject or a string that is the class of a MathObject (e.g., <code>"Value::Vector"</code>).
 
| style="text-align:center; padding:5px" | <code>"Value::Real"</code>
 
| style="text-align:center; padding:5px" | <code>"Value::Real"</code>
   
Line 126: Line 120:
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
 
| style="padding: 5px; white-space: nowrap" | <code>implicitList => 1</code> or <code>0</code>
 
| style="padding: 5px; white-space: nowrap" | <code>implicitList => 1</code> or <code>0</code>
| style="padding: 5px" | Force/don't force single entry answers to be lists (even if they are already lists). This way, if you are asking for a list of lists, and the student enters a single list, it will be turned into a list of a single list, and will be checked properly against the correct answer.
+
| style="padding: 5px" | Force/don't force single entry answers to be lists (even if they are already lists).
 
| style="text-align:center" | <code>0</code>
 
| style="text-align:center" | <code>0</code>
   
Line 136: Line 130:
 
=== Methods ===
 
=== Methods ===
   
The Set class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional ones:
+
The Set class supports the [[Common MathObject Methods]], and the following additional ones:
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 158: Line 152:
   
 
|- style="vertical-align: baseline"
 
|- style="vertical-align: baseline"
| style="padding: 5px; white-space: nowrap" | <code>$S->isEmtpy</code>
+
| style="padding: 5px; white-space: nowrap" | <code>$S->isEmpty</code>
 
| style="padding: 5px" | Returns <code>true</code> if <code>$S</code> is the empty set, and <code>undef</code> otherwise.
 
| style="padding: 5px" | Returns <code>true</code> if <code>$S</code> is the empty set, and <code>undef</code> otherwise.
   
Line 178: Line 172:
 
=== Properties ===
 
=== Properties ===
   
The Set class supports the [[MathObject_Methods_and_Properties| common MathObject properties]], and the following additional ones:
+
The Set class supports the [[Common MathObject Properties]], and the following additional ones:
   
 
{| class="wikitable"
 
{| class="wikitable"

Latest revision as of 17:42, 18 August 2012

Set Class

The Set class implements finite sets of real numbers. Sets are enclosed in curly braces, and can contain arbitrarily many real numbers, in any order. The empty set is formed by open and close braces with no numbers between them, i.e., {}. Set are most often created in the Interval context.

The answer checker for Sets reports a warning if an element is entered twice in the set (e.g., {1,1,2}), but this can be controlled by an option for the answer checker.


Creation

Sets are created via the Set() function, or by Compute().

   Context("Interval");
   
   $S = Set(0,sqrt(2),pi,-7);
   $S = Set([0,sqrt(2),pi,-7]);
   $S = Set("{0,sqrt(2),pi,-7}");
   $S = Compute("{0,sqrt(2),pi,-7}");

When Sets are created as part of the code of a problem, repeated elements are removed automatically so that {0,1,1,2,0} will be converted to {0,1,2}. In general, however, students must enter Sets without repeated elements (though this can be controlled by answer checker options). You can turn off automatic reduction by setting the flag reduceSets to 0 in the Context:

   Context()->flags->set(reduceSets => 0);

This will preserve the Set in whatever form it was originally created (this is set to 0 for student answers so that automatic reductions are not performed). A second flag, reduceSetsForComparison, determines whether Sets are reduced (temporarily) before they are compared for equality, or whether the structures must match exactly.


Operations on Sets

Sets can be combined with each other and with Intervals via a Union (represented by an upper-case U in student answers and parsed strings, and by addition or the Union() constructor in Perl code). Differences of Sets and other Sets, Intervals, or Unions can be obtained via subtraction. (Note that in order to ensure that the result of the union operation has only one copy of each element, you need to pass it through the Set constructor)

   $U = Set(Set(0,1,2) + Set(2,pi,sqrt(2)));    # same as Set(0,1,2,pi,sqrt(2));
   $U = Set("{0,1,2} U {2,pi,sqrt(2)}");
   $U = Union("{0,1,2} U {2,pi,sqrt(2)}");
   $U = Compute("{0,1,2} U {2,pi,sqrt(2)}");
   $W = Set(0,1,2) + Interval("(1,2)");         # same as Compute("{0} U [1,2]");
   
   $S = Set(0,1,2) - Set(2,pi);                 # same as Set(0,1);
   $S = Compute("{0,1,2} - {2,pi}");            # same as above
   
   $S = Compute("{0,1,2} - [1,2)");             # same as Set(1,2);

Intersections of Sets with other Sets, Intervals, or Unions can be obtained via the intersect() method of a Set. 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 Set is contained in another, or intersects one, or is a subset of another, etc. These methods can be applied to Intervals and Unions in addition to Sets.

   $S1 = Set(1,2,3,4);
   $S2 = Set(3,4,5);
   
   $S3 = $S1->intersect($S2);           # same as Set(3,4);
   
   $S1->contains($S2);                  # returns false
   $S3->isSubsetOf($S2);                # returns true
   $S1->intersects($S2);                # returns true

Answer Checker

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

   ANS(Compute("{-2.3,0,pi}")->cmp);

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

Option Description Default
showHints => 1 or 0 Do/don't show messages about which entries are incorrect. $showPartialCorrectAnswers
showLengthHints => 1 or 0 Do/don't show messages about having the correct number of entries (only shown when all the student answers are correct but there are more needed, or all the correct answsers are among the ones given, but some extras were given). $showPartialCorrectAnswers
partialCredit => 1 or 0 Do/don't give partial credit for when some answers are right, but not all. $showPartialCorrectAnswers
ordered => 1 or 0 Give credit only if the student answers are in the same order as the professor's answers. 0

Note that since a Set is a subclass of List, the other options for the List answer checker also can be used here, though their defaults have been set to appropriate values for a Set and it is unlikely you will need to change them.

Option Description Default
showParenHints => 1 or 0 Do/don't show messages about having the correct type of delimiters, or missing delimiters. 1
entry_type => "a (name)" The string to use in error messages that identifies the type of elements that make up the Set. "a number"
list_type => "a (name)" The string to use in error messages that identifies the Set itself. "a set"
typeMatch => $object Specifies the type of object that the student should be allowed to enter in the Set (determines what constitutes a type mismatch error). Can be either a MathObject or a string that is the class of a MathObject (e.g., "Value::Vector"). "Value::Real"
requireParenMatch => 1 or 0 Do/don't require the parentheses in the student's answer to match those in the professor's answer exactly. 1
removeParens => 1 or 0 Do/don't remove the parentheses from the professor's list as part of the correct answer string. This is so that if you use List() to create the list (which doesn't allow you to control the parens directly), you can still get a list with no parentheses. 0
implicitList => 1 or 0 Force/don't force single entry answers to be lists (even if they are already lists). 0

Note also that since Set is a subclass of List, if you supply a custom checker option, it operates the same as for a List. You probably want to use a list_checker instead. See Custom Answer Checkers for Lists for details.


Methods

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

Method Description
$S1->intersect($S2) Returns the intersection of $S1 with $S2. Note that $S2 can be an Interval, Set, or Union.
$S1->intersects($S2) Returns true if $S1 intersects $S2, and undef otherwise. Note that $S2 can be an Interval, Set, or Union.
$S1->contains($S2) Returns true if $S2 is a subset of $S1, and undef otherwise. Note that $S2 can be an Interval, Set, or Union.
$S1->isSubsetOf($S2) Returns true if $S1 is a subset of $S2, and undef otherwise. Note that $S2 can be an Interval, Set, or Union.
$S->isEmpty Returns true if $S is the empty set, and undef otherwise.
$S->isReduced Returns true if there are no repeated elements in $S, andundef otherwise.
$S->reduce Returns a copy of $S without any repeated elements.
$S->sort Returns a copy of $S with its elements sorted in ascending order.


Properties

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

Property Description Default
$r->{open} The symbol to use for the open brace {
$r->{close} The symbol to use for the close brace }