## WeBWorK Problems

### computing set union and set intersection

by Jay Yellen -
Number of replies: 1
I was wondering if there is a direct way to compute set operations. I've seen problems that directly compute the set difference using the statement
$ans5 = Compute("$B-$C"); But I haven't been able to find something similar for intersection or union. Do they exist? Thanks in advance, --Jay In reply to Jay Yellen ### Re: computing set union and set intersection by Davide Cervone - 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.