Common MathObject Methods

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

MathObject Methods and Properties

There are a number of methods that are common to all MathObjects, which are described below. Some classes have additional methods, and many of these are listed in the documentation for the individual MathObject Classes. You call a method on a MathObject as you would a method for any Perl object, using the -> operator with the MathObject on the left and the method name and its arguments (if any) on the right. E.g.,

   $mathObject->method;              # when there are no arguments
   $mathObject->method($arg);        # for one argument
   $mathObject->method($arg1,$arg2); # for two arguments
   # and so on

The common methods include:

  • cmp or cmp(options)
Returns an answer checker for the MathObject. The cmp method can accept a number of settings that control the tolerances for the comparison, special options of the comparison (e.g., parallel vectors rather than equal vectors), the types of error messages produced (e.g., messages about individual coordinates that are wrong), and other features of the check. These are described in the MathObjects Answer Checkers POD documentation. All of the answer checkers are defined in the file pg/lib/Value/AnswerChecker.pm.
  • value
Returns an array containing the data that represents the object. For a Real, it is just the perl real number that corresponds to it; for a Complex number, it is the real and imaginary parts (as Reals). For an Infinity, it is the string needed to obtain the Infinity. For a Point or Vector, it is the coordinates of the Point or Vector (as MathObjects). For a Matrix, it is an array of rows of the Matrix, where the rows are references to arrays of MathObjects. For an Interval, it is the two endpoints (as Reals) followed by strings that are the open and close parentheses or brackets for the Interval. For a Set, it is an array of the elements of the set (as Reals). For a Union, it is an array of the Sets and Intervals that make up the Union. For a String, it is a perl string representing the value of the string.
For a Formula, it is a little more complicated. If the Formula is an explicit Point, Vector, Matrix, Interval, etc. (e.g., Formula("<x,x+1>")), then the value is an array of Formulas that are the coordinates. If the Formula is an expression (e.g., Formula("<x,1> + <2x,-x>") or Formula("norm(<x,x+1>)") then the value is just the original Formula.
  • getFlag("flag-name") or getFlag("flag-name",default)
Returns the object's value for a given context flag. The value can come from a variety of locations, which are searched in the following order (the first that has a property with the given flag name will have that propery's value returned): the object itself, the Formula that created it (if it was the result of an eval() call, for example), the AnswerHash associated with the object (if it was the source for an answer checker; this gives access to the flags passed to the cmp method), the Context in which the object was created, the currently active Context, or the default value (if given as the second argument), otherwise the return value is undef. This is useful in custom answer checkers for finding out the settings of things like the tolerances, the flags passed to the answer checker, and so on.
  • with(name => value)
This copies the object, and sets its property with the given name to the given value. You can supply multiple name/value pairs separated by commas. This gives you the ability to initialize the object when you create it, or to make a copy with specific settings. E.g.
   $f = Formula("sqrt(x-10)")->with(limits => [10,12]);
   $a = Real(pi/2)->with(period => 2*pi);
  • typeMatch($object)
This determines if the $object is "compatible" with the given MathObject for equality or inequality comparisons. For example, a Real will allow equality comparison to an Infinity, and a Complex will allow comparison to a Real. It is best to use typeMatch in your own custom answer checkers if you need to check if a student's answer is of the correct type rather than using something like ref() to check if the two are the same type of object (which is too restrictive, in general).
  • class and type
These are two methods that help you determine what kind of MathObject you are working with. They can be useful in custom answer checkers if you want to know more about what kind of object a student answer is. The class method tells you the class of object (like Real, Complex, Point, Formula, etc.), while the type method tells you what kind of return value a Formula has (non-Formulas are considered constant-valued Formulas when computing the type). The class is essentially the package name from the Value package of the MathObject, while the type is the package name from the Parser package name for the result of the Formula.
   Real(5)->class;          # produces "Real"
   Real(5)->type;           # produces "Number"
   Point(1,2)->class;       # produces "Point"
   Point(1,2)->type;        # produces "Point"
   
   Formula("3x+1")->class;  # produces "Formula"
   Formula("3x+1")->type;   # produces "Number"
  • TeX
Returns a string which represents the object in TeX format. For example
   $f = Formula("(x+1)/(x-1)");
   BEGIN_TEXT
   The formula is \(f(x) = \{$f->TeX\}\).
   END_TEXT
Since it is common to need the TeX expression in the problem's text, there is a special Context setting that tells MathObjects to output their TeX format whenever they are substituted into a string. That is enabled via the Context()->texStrings command, and turned off by Context()->normalStrings, as in the following example:
   $f = Formula("(x+1)/(x-1)");
   Context()->texStrings;
   BEGIN_TEXT
   The formula is \(f(x) = $f\)
   END_TEXT
   Context()->normalStrings;
This avoids having to call the TeX method, and prevents the need for using \{...\} to get the TeX format.
  • string
Returns a string similar to that used to create the object, in the form that a student would use to enter the object in an answer blank, or that could be used in Compute() to create the object. The string may have more parentheses than the original string used to create the object, and may include explicit multiplication rather than implicit multiplication, and other normalization of the original format.
  • perl
Returns a string which represents the object as Perl source code. This is used internally, and would rarely be needed in a PG problem.

(Still need to add properties, like {correct_ans})