Union (MathObject Class)
The 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 Union()
constructor in Perl code. The order of the Sets and Intervals in the Union does not matter.
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)");
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 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 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
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 non-overlapping 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.