Set (MathObject Class)

From WeBWorK_wiki
Revision as of 17:36, 5 August 2012 by Dpvc (talk | contribs) (Added data on reduceSets and reduceSetsForComparison)
Jump to navigation Jump to search

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., {}.

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 Set 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.

   $U = 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"
extra => $object 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 NONE), and the expected type of elements in the list are complicated and need special syntax checking (e.g., for size of Vectors). determined from entries
typeMatch => $object 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., "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). 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. 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->isEmtpy 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 }