## WeBWorK Problems

### Re: computing set union and set intersection

by Davide Cervone -
Number of replies: 0
I'm not sure if you are looking for this as part of the code of the problem, or whether you want students to be able to produce these.

For students, there is also a union operation (indicated by a capital "U"), so a student could write "(-infinity,-2) U (2,infinity)" as an answer. There is no built-in method for students to write intersections (though it certainly would be possible to write an extension that does that).

For use within the code of a problem, you can use

   $ans5 = Compute("(-inf,-2) U (2,inf)");  just like a student can. If you have two intervals (or sets or unions), then you can use simple addition to get unions: $B = Compute("(-infinity,-2)");
$C = Compute("(2,infinity)");$D = $B +$C;  # equivalent to Compute("$B U$C");

Similarly, you can use subtraction for set difference:
    $D = Interval("(-10,10)") - Set(-1,0,1);  You can obtain set intersections via methods of the interval, set, and union objects: $B = Compute("(0,2pi)");
$C = Compute("(-pi,pi)");$D = $B->intersect($C);  # equivalent to Compute("(0,pi)");

There are other methods like isSubsetOf, contains, isEmpty, intersects, reduce and isReduced that can be used on interval, set, and union objects as well. The first of these returns true or false depending on whether the condition is true; reduce takes a union and rewrites it without redundancy (e.g., overlapping intervals are simplified); the last returns true/false depending on whether it is already in that form.