Set (MathObject Class)

From WeBWorK_wiki
Revision as of 15:59, 2 August 2012 by Dpvc (talk | contribs) (Created page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The 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., {}.

   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}");

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.

   $U = 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.

   $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

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 a flag on the answer checker.