Set (MathObject Class)

Contents

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

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 }