Union (MathObject Class)
Union Class
The Union class implements finite unions of Intervals and Sets. Unions are formed via the U
symbol in student answers or parsed strings, or by addition or the dot operator or the Union()
constructor in Perl code. The order of the Sets and Intervals in the Union does not matter. Unions are created most often in the Interval
Context.
The answer checker for Unions will issue warnings if the Union contains overlapping Intervals or Sets, or if Sets contains repeated elements. These warnings can be controlled by answer checker options.
Creation
Context("Interval"); $U = Interval("[1,1]") + Interval("(5,infinity)"); $U = Union(Interval("[1,1]"),Interval("(5,infinity)")); $U = Union("[1,1] U (5,infinity)"); $U = Compute("[1,1] U (5,infinity)");
When Unions are created as part of the code of a problem, things like overlapping intervals are reduced automatically so that (1,2) U [0,3]
will be converted to (1,3]
. In general, however, students must enter unions of nonoverlapping intervals and sets, though this can be controlled by answer checker flags. You can turn off automatic reduction by setting the flag reduceUnions
to 0 in the Context:
Context()>flags>set(reduceUnions => 0);
This will preserve the Union 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, reduceUnionsForComparison
, determines whether Unions are reduced (temporarily) before they are compared for equality, or whether the structures must match exactly.
Operations on Unions
Differences of Unions with Sets, Intervals, or other Unions can be obtained via subtraction.
$W = Union("(0,1) U (2,5)")  Interval("(3,4)"); # same as Union("(0,1) U (2,3] U [4,5)"); $W = Compute("(0,1) U (2,5)  (3,4)"); # same as above
Intersections of Unions with other Sets, Intervals, or Unions can be obtained via the intersect()
method of a Union. There is no builtin method for students to form intersections (though one could be added to the Context by hand). There are other methods for determining if one Union is contained in another, or intersects one, or is a subset of another, etc.
$U1 = Union("(0,3] U {4,5}"); $U2 = Union("[3,4] U {0}"); $U3 = $U1>intersect($U2); # same as Set(3,4); $U1>contains($U2); # returns false $U3>isSubsetOf($U2); # returns true $U1>intersects($U2); # returns true
Answer Checker
As with all MathObjects, you obtain an answer checker for a Union object via the cmp()
method:
ANS(Compute("(infinity,1] U [1,infinity)")>cmp);
The Union class supports the common answerchecker options, and the following additional options:
Option  Description  Default 

showHints => 1 or 0

Do/don't show messages about which entries in the Union 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 entries 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 Union 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 

entry_type => "a (name)"

The string to use in error messages that identifies the type of elements that make up the Union.  "an interval or set"

list_type => "a (name)"

The string to use in error messages that identifies the Union itself.  "an interval, set or union"

short_type => "a (name)"

A short string to use in error messages that identifies the Union itself.  "a union"

typeMatch => $object

Specifies the type of object that the student should be allowed to enter in the union (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" ).

Interval("(0,1)")

Note also that since Union 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 Union class supports the Common MathObject Methods, and the following additional ones:
Method  Description 

$U1>intersect($U2)

Returns the intersection of $U1 with $U2 . Note that $U2 can be an Interval, Set, or Union.

$U1>intersects($U2)

Returns true if $U1 intersects $U2 , and undef otherwise. Note that $U2 can be an Interval, Set, or Union.

$U1>contains($U2)

Returns true if $U2 is a subset of $U1 , and undef otherwise. Note that $U2 can be an Interval, Set, or Union.

$U1>isSubsetOf($U2)

Returns true if $U1 is a subset of $U2 , and undef otherwise. Note that $U2 can be an Interval, Set, or Union.

$U>isEmpty

Returns true if $U is the empty set, and undef otherwise.

$U>isReduced

Returns true if there are no overlapping Intervals or Sets, and no Sets contain repeated elements in $U . Returns undef otherwise.

$U>reduce

Returns a copy of $U without any overlapping Intervals or Sets, and where no Sets contain repeated elements.

$U>sort

Returns a copy of $U with its Intervals and Sets sorted lexicographically.

Properties
The Union class supports the Common MathObject Properties. There are no additional properties for this class.