Difference between revisions of "Common MathObject Methods"

From WeBWorK_wiki
Jump to navigation Jump to search
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== 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 [[:Category:MathObject_Classes|MathObject Classes]]. You call a method on a MathObject as you would a method for any Perl object, using the <code>-></code> operator with the MathObject on the left and the method name and its arguments (if any) on the right. E.g.,
 
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 [[:Category:MathObject_Classes|MathObject Classes]]. You call a method on a MathObject as you would a method for any Perl object, using the <code>-></code> 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
+
$mo->method; # when there are no arguments
$mathObject->method($arg); # for one argument
+
$mo->method($arg); # for one argument
$mathObject->method($arg1,$arg2); # for two arguments
+
$mo->method($arg1,$arg2); # for two arguments
 
# and so on
 
# and so on
   
The common methods include:
 
   
* <code>cmp</code> or <code>cmp(options)</code>
 
  +
=== Methods Common to All Classes ===
: Returns an answer checker for the MathObject. The <code>cmp</code> 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 [http://webwork.maa.org/pod/pg_TRUNK/doc/MathObjects/MathObjectsAnswerCheckers.html MathObjects Answer Checkers] POD documentation. All of the answer checkers are defined in the file <code>pg/lib/Value/AnswerChecker.pm</code>.
 
   
* <code>value</code>
 
  +
{| class="wikitable"
: 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.
 
  +
! Method !! Description
   
: For a Formula, it is a little more complicated. If the Formula is an explicit Point, Vector, Matrix, Interval, etc. (e.g., <code>Formula("<x,x+1>")</code>), then the value is an array of Formulas that are the coordinates. If the Formula is an expression (e.g., <code>Formula("<x,1> + <2x,-x>")</code> or <code>Formula("norm(<x,x+1>)"</code>) then the value is just the original Formula.
 
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->cmp</code><br><code>$mo->cmp(options)</code>
  +
| style="padding: 5px" | Returns an answer checker for the MathObject. The <code>cmp</code> 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 [http://webwork.maa.org/pod/pg/doc/MathObjects/MathObjectsAnswerCheckers.html MathObjects Answer Checkers] POD documentation. All of the answer checkers are defined in the file <code>pg/lib/Value/AnswerChecker.pm</code>.
   
* <code>getFlag("flag-name")</code> or <code>getFlag("flag-name",default)</code>
 
  +
|- style="vertical-align: top"
: 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 <code>eval()</code> 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 <code>cmp</code> 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 <code>undef</code>. 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.
 
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->value</code>
  +
| style="padding: 5px" | Returns an array containing the data that represents the object.
   
* <code>with(name => value)</code>
 
  +
For a Real, it is just the perl real number that corresponds to it;
: 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.
 
  +
  +
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). (The strings for the open and close parentheses or brackets for the Interval should be obtained using <code>$mo->{open}</code> and <code>$mo->{close}</code>).
  +
  +
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., <code>Formula("<x,x+1>")</code>), then the value is an array of Formulas that are the coordinates. If the Formula is an expression (e.g., <code>Formula("<x,1> + <2x,-x>")</code> or <code>Formula("norm(<x,x+1>)"</code>) then the value is just the original Formula.
  +
  +
|- style="vertical-align: top" id="data"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->data</code>
  +
| style="padding: 5px" | Returns a reference to the array of data that represents the object.
  +
  +
|- style="vertical-align: top" id="length"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->length</code>
  +
| style="padding: 5px" | Returns the number of elements in the array returned by the <code>value()</code> method, e.g., the number of coordinates in a point or vector, or the number of rows in a matrix.
  +
  +
|- style="vertical-align: top" id="extract"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->extract(<i>i</i>,...)</code>
  +
| style="padding: 5px" | Returns the <math>i</math>-th element from the data for <code>$mo</code>. If more than one index is used, recursively extracts from the result, so that for a Matrix, <code>$M->extract(<i>i</i>,<i>j</i>)</code> returns the <math>(i,j)</math>-th entry, and for a list of lists, <code>$L->extract(<i>i</i>,<i>j</i>)</code> is the <math>j</math>-th element of the <math>i</math>-th list.
  +
  +
|- style="vertical-align: top" id="new"
  +
| style="padding: 5px; white-space: nowrap" | <code><I>class</i>->new(data)</code><br><code>$mo->new(data)</code>
  +
| style="padding: 5px" | Creates a new instance of the <i>class</i> or the class of <code>$mo</code> using the given data. This what the various class constructor functions do; e.g., <code>Real(5)</code> effectively calls <code>Value::Real->new(5)</code>.
  +
  +
|- style="vertical-align: top" id="make"
  +
| style="padding: 5px; white-space: nowrap" | <code><i>class</i>->make(data)</code><br><code>$mo->make(data)</code>
  +
| style="padding: 5px" | Creates a new instance of the <i>class</i> or the class of <code>$mo</code> using the given data, but without all the error checking of type conversion. So <code>data</code> must be an array of MathObjects of the correct type. This is used internally when the types of the data are known to be correct, for efficiency, but it is always safer to use <code>new</code>.
  +
  +
|- style="vertical-align: top" id="with"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->with(name => value)</code>
  +
| style="padding: 5px" | 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]);
 
$f = Formula("sqrt(x-10)")->with(limits => [10,12]);
 
$a = Real(pi/2)->with(period => 2*pi);
 
$a = Real(pi/2)->with(period => 2*pi);
   
* <code>typeMatch($object)</code>
 
  +
Note that the copy is not a deep copy (i.e., if <code>$mo</code> has references to other objects, the returned object will reference the same ones rather than copies of them). In particular, this is the case for the <code>{data}</code> property. Use <code>$mo->copy</code> to get a deeper copy, or <code>$mo->new($mo->string)</code> to get a separate version that shares no data with <code>$mo</code>
: This determines if the <code>$object</code> 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 <code>typeMatch</code> 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 <code>ref()</code> to check if the two are the same type of object (which is too restrictive, in general).
 
   
* <code>class</code> and <code>type</code>
 
  +
|- style="vertical-align: top" id="without"
: 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 <code>class</code> method tells you the class of object (like Real, Complex, Point, Formula, etc.), while the <code>type</code> method tells you what kind of return value a Formula has (non-Formulas are considered constant-valued Formulas when computing the <code>type</code>). The <code>class</code> is essentially the package name from the Value package of the MathObject, while the <code>type</code> is the package name from the Parser package name for the result of the Formula.
 
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->without("name",...)</code>
  +
| style="padding: 5px" | Returns a copy of <code>$mo</code> where the named properties have been removed. Note that, as with <code>with()</code>, the copy is not a deep one, so the two copies of <code>$mo</code> will share any references to other objects. In particular, this is the case for the <code>{data}</code> property. Use <code>$mo->copy</code> to get a deeper copy, or <code>$mo->new($mo->string)</code> to get a separate version that shares no data with <code>$mo</code>
  +
  +
|- style="vertical-align: top" id="copy"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->copy</code>
  +
| style="padding: 5px" | Returns a copy of <code>$mo</code> with its <code>{data}</code> array copied as well, so that the copy doesn't share the data with the original. Note that setting <code>$mo2 = $mo1</code> does not copy <code>$mo1</code>; instead, both <code>$mo2</code> and <code>$mo1</code> both point to the ''same'' MathObject, so changes to one will change the other. Use <code>$mo2 = $mo1->copy</code> to obtain a separate copy for <code>$mo2</code>.
  +
  +
|- style="vertical-align: top" id="context"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->context</code><br><code>$mo->context($context)</code>
  +
| style="padding: 5px" | The first form returns the context in which <code>$mo</code> was created. When passed a reference to a Context object, this sets the context for <code>$mo</code> to be the given context.
  +
  +
|- style="vertical-align: top" id="inContext"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->inContext($context)</code>
  +
| style="padding: 5px" | This calls <code>context($context)</code> and then returns <code>$mo</code> so that it can be used in a chain of method calls, e.g.
  +
  +
: <code>$f->inContext($g->context)->with(limits => $g->{limits});</code>
  +
  +
|- style="vertical-align: top" id="getFlag"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->getFlag("name")</code><br><code>$mo->getFlag("name",default)</code>
  +
| style="padding: 5px" | 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 <code>eval()</code> 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 <code>cmp</code> 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 <code>undef</code>. 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.
  +
  +
|- style="vertical-align: top" id="typeMatch"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->typeMatch($object)</code>
  +
| style="padding: 5px" | This determines if the <code>$object</code> 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 <code>typeMatch</code> 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 <code>ref()</code> to check if the two are the same type of object (which is too restrictive, in general).
  +
  +
|- style="vertical-align: top" id = "type"
  +
| style="padding: 5px; white-space: nowrap" id="class" | <code>$mo->class</code><br><code>$mo->type</code>
  +
| style="padding: 5px" | 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 <code>class</code> method tells you the class of object (like Real, Complex, Point, Formula, etc.), while the <code>type</code> method tells you what kind of return value a Formula has (non-Formulas are considered constant-valued Formulas when computing the <code>type</code>). The <code>class</code> is essentially the package name from the Value package of the MathObject, while the <code>type</code> is the package name from the Parser package name for the result of the Formula.
   
 
Real(5)->class; # produces "Real"
 
Real(5)->class; # produces "Real"
Line 41: Line 107:
 
Formula("3x+1")->type; # produces "Number"
 
Formula("3x+1")->type; # produces "Number"
   
* <code>TeX</code>
 
  +
|- style="vertical-align: top" id="TeX'
: Returns a string which represents the object in TeX format. For example
 
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->TeX</code>
  +
| style="padding: 5px" | Returns a string which represents the object in <math>\rm\TeX</math> format. For example
   
 
$f = Formula("(x+1)/(x-1)");
 
$f = Formula("(x+1)/(x-1)");
Line 49: Line 116:
 
END_TEXT
 
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 <code>Context()->texStrings</code> command, and turned off by <code>Context()->normalStrings</code>, as in the following example:
+
Since it is common to need the <math>\rm\TeX</math> expression in the problem's text, there is a special Context setting that tells MathObjects to output their <math>\rm\TeX</math> format whenever they are substituted into a string. That is enabled via the <code>Context()->texStrings</code> command, and turned off by <code>Context()->normalStrings</code>, as in the following example:
   
 
$f = Formula("(x+1)/(x-1)");
 
$f = Formula("(x+1)/(x-1)");
Line 58: Line 125:
 
Context()->normalStrings;
 
Context()->normalStrings;
   
: This avoids having to call the <code>TeX</code> method, and prevents the need for using <code>\{...\}</code> to get the TeX format.
+
This avoids having to call the <code>TeX()</code> method, and prevents the need for using <code>\{...\}</code> to get the <math>\rm\TeX</math> format.
  +
  +
|- style="vertical-align: top" id="string"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->string</code>
  +
| style="padding: 5px" | 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 <code>Compute()</code> 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.
  +
  +
|- style="vertical-align: top" id="perl"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->perl</code>
  +
| style="padding: 5px" | Returns a string which represents the object as Perl source code. This is used internally, and would rarely be needed in a PG problem.
  +
  +
|- style="vertical-align: top" id="eval"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->eval</code><br><code>$mo->reduce</code>
  +
| style="padding: 5px" | All MathObjects have these methods, but for most, they just return <code>$mo</code>. They are mostly used for the Formula class, but are available on all so that you don't have to check the type before calling them. A few classes, like Unions, implement <code>reduce()</code>.
  +
  +
|- style="vertical-align: top" id="ans_rule"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->ans_rule(width)</code><br><code>$mo->named_ans_rule(width)</code><br><code>$mo->named_ans_rule_extension(width)</code>
  +
| style="padding: 5px" | These operate like the PG macros of the same names, but are here as the counterpart to the <code>ans_array</code> methods below. That way, you can call one or the other in order to get the proper answer rules for <code>$mo</code>. It also helps to see what answer rule goes with what answer if you use <code>\{$mo->ans_rule(10)\}</code> in your <code>BEGIN_TEXT/END_TEXT</code> blocks rather than just <code>\{ans_rule(10)\}</code>.
  +
  +
|- style="vertical-align: top" id="ans_array"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->ans_array(width)</code><br><code>$mo->named_ans_array(width)</code><br><code>$mo->named_ans_array_extension(width)</code>
  +
| style="padding: 5px" | For Points, Vectors, and Matrices, these produce multiple answer blanks, one for each coordinate or entry, that are all tied to the one answer checker for <code>$mo</code>. This forces (or allows, depending on how you look at it) the student to enter each coordinate separately, while you still only need to handle one answer checker. See the [[Matrix (MathObject Class)| Matrix]] documentation for an example.
  +
  +
|- style="vertical-align: top" id="isZero"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->isZero</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$mo</code> corresponds to whatever zero means for that type (e.g., for a Vector, it is a zero vector), and <code>0</code> otherwise.
  +
  +
|- style="vertical-align: top" id="isOne"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->isOne</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$mo</code> corresponds to whatever one means for that type (e.g., for a Matrix, it is an identity matrix), and <code>0</code> otherwise.
  +
  +
|- style="vertical-align: top" is="isSetOfReals"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->isSetOfReals</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$mo</code> is an Interval, Set, or Union (or subclasses of those), and <code>0</code> otherwise.
  +
  +
|- style="vertical-align: top" id="conBeInUnion"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->canBeInUnion</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$mo</code> is an Interval, Set, or Union, or is another object whose string version could look like an Interval (e.g., a Point with two coordinates where the first is less than the right). This is used in contexts where parentheses can produce Points, for example, to promote them to Intervals when appropriate.
  +
  +
|- style="vertical-align: top" id="showClass"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->showClass</code>
  +
| style="padding: 5px" | Returns a string representing the class of <code>$mo</code> suitable for use in error messages. The string includes "an" or "a", so
  +
  +
: <code>Value->Error("You can't take the square root of %s",$mo->showClass)</code>
  +
  +
could produce the message "You can't take the square root of a Vector".
  +
  +
|- style="vertical-align: top" id="showType"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->showType</code>
  +
| style="padding: 5px" | Returns a string representing the type of <code>$mo</code> suitable for use in error messages. Note that the class and the type are not the same; e.g., the type of a Formula is the type of it return value, while it's class is Formula (actually <code>Value::Formula</code>). So <code>$f->showClass</code> might be "a Formula returning a Real Number" for a formula <code>$f</code>, while <code>$f->showType</code> would produce "a Real Number".
  +
  +
|- style="vertical-align: top" id="inherit"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->inherit($mo1,...)</code>
  +
| style="padding: 5px" | Copy the properties that are not already present in <code>$mo</code> from <code>$mo1</code> (and any other objects passed to it). This is used by things like the binary operations to make sure that the result inherits the attributes of the the two operands.
  +
  +
|- style="vertical-align: top" id="noinherit"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->noinherit</code>
  +
| style="padding: 5px" | Returns the list of properties that should ''not'' be copied by <code>inherit()</code>.
  +
  +
|- style="vertical-align: top" id="Package"
  +
| style="padding: 5px; white-space: nowrap" | <code>$mo->Package($context,"class",$noerrors)</code>
  +
| style="padding: 5px" | Returns the name of the package that implements the given class in the given Context. For example, <code>$mo->Package($mo->context,"Real")</code> probably will return <code>"Value::Real"</code>. The Context could have override the default package for Reals, however, to use a subclass of Reals that implements additional functionality, in which case that subclass package name would be returned. If there is no package for the given class, an error will be produced, unless <code>$noerrors</code> is <code>1</code>.
  +
  +
|}
  +
  +
<!--
  +
typeRef
  +
entryType
  +
  +
getType
  +
getValueType
  +
toFormula
  +
formula
  +
  +
promotePrecedence
  +
precedence
  +
promote
  +
checkOpOrder
  +
chekcOpOrderWithPromote
  +
  +
nomethod
  +
nodef
  +
add
  +
sub
  +
mult
  +
div
  +
power
  +
cross
  +
modulo
  +
twiddle
  +
neg
  +
abs
  +
sqrt
  +
exp
  +
log
  +
sin
  +
cos
  +
dot
  +
pdot
  +
compare
  +
compare_string
  +
  +
transferFlags
  +
transferTolerances
  +
  +
  +
correct_ans
  +
cmp_defaults
  +
cmp_contextFlags
  +
cmp_diagnostics
  +
cmp_parse
  +
cmp_collect
  +
cmp_equal
  +
cmp_compare
  +
cmp_list_compare
  +
cmp_class
  +
cmp_error
  +
cmp_Error
  +
cmp_Message
  +
cmp_preprocess
  +
cmp_postprocess
  +
cmp_call_filter
  +
  +
answer_matrix
  +
format_matrix
  +
format_matrix_tex
  +
format_matrix_HTML
  +
format_delimiter
  +
format_delimiter_tth
  +
ans_collect
  +
Value::entryMessage
  +
Value::entryCheck
  +
Value::ANS_NAME
  +
Value::EVALUATE
  +
Value::VERBATIM
  +
  +
  +
Value::pgCall
  +
Value::pgRef
  +
getPG
  +
  +
isa
  +
can
  +
subclassed(obj,meth)
  +
hash
  +
-->
  +
  +
=== Support Functions ===
  +
  +
These are functions that are part of the Value package, but do not refer to a MathObject directly, so are not methods of any class. You call them from the Value package directly. Mostly they are general utility functions, or functions that test an unknown value to see if it is a MathObject or could be converted to one.
  +
  +
{| class="wikitable"
  +
! Method !! Description
  +
  +
|- style="vertical-align: top" id="Error"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value->Error(message,$s1,...)</code>
  +
| style="padding: 5px" | Produces an error message by inserting the values of <code>$s1</code> and any other arguments into the <code>message</code> using <code>printf</code>-style substitution, and then throws an error condition (i.e., your program or subroutine will stop at that point). If used in a custom answer checker, the error usually will appear in the messages area of the results table at the top of the page. For example,
  +
  +
: <code>Value->Error('You can't take the square root of %s',$mo->showType)</code>
  +
  +
might produce the message "You can't take the square root of a Vector".
  +
  +
|- style="vertical-align: top" id="NameForNumber"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value->NameForNumber(<i>n</i>)</code>
  +
| style="padding: 5px" | Returns a string like <code>"first"</code>, <code>"second"</code>, etc. that can be used in an error message. For example,
  +
  +
: <code>Value->Error("Your %s point is incorrect",Value->NameForNumber(i))</code>
  +
  +
would produce the message "Your fifth point is incorrect" when <code>i</code> is 5. This can be useful in custom answer checkers that deal with lists of objects or coordinates of points or vectors.
  +
  +
|- style="vertical-align: top" id="Value::contextSet"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::contextSet($context,flags)</code>
  +
| style="padding: 5px" | This sets the given flags to the given values in the given context and returns a hash representing the flags and their original settings. This is useful if you want to change the settings of flags temporarily (e.g., in an answer checker), but want to reset them when done. For example,
  +
  +
my $flags = Value::contextSet($correct->context,
  +
reduceConstants => 1,
  +
reduceConstantFunctions => 0,
  +
);
  +
... do something with Formulas here ...
  +
Value::contextSet($correct->context, %{$flags});
  +
  +
would set the value of <code>reduceConstants</code> and unset <code>reduceConstantFunctions</code> and return the original settings in <code>$flags</code>. After the processing that needs these flag settings is complete, the second call to <code>contestSet()</code> returns the values to their original settings.
  +
  +
|- style="vertical-align: top" id="Value::protectHTML"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::protectHTML(string)</code>
  +
| style="padding: 5px" | Returns a string where HTML special characters have been properly escaped (e.g., <code>&lt;</code>, <code>&gt;</code> and <code>&amp;</code> have been replaced by their corresponding HTML entities.
  +
  +
|- style="vertical-align: top" id="Value::preformat"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::preformat(string)</code>
  +
| style="padding: 5px" | Returns a string where HTML special characters have been replaced by their HTML entities, and newlines have been replaced by <code>&lt;br&gt;</code>.
  +
  +
|- style="vertical-align: top" id="Value::makeValue"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::makeValue(data)</code>
  +
| style="padding: 5px" | Attempts to convert the <code>data</code> into an appropriate MathObject and returns that. For example, a Perl real will be converted to a Real object, and a string will be converted to a String, provided it is in the current Context. It is safe to pass a MathObject to <code>makeValue()</code>, as it will be returned without change; thus you can use <code>$x = makeValue($x)</code> to guarantee that <code>$x</code> is a MathObject.
  +
  +
|- style="vertical-align: top" id="Value::matchNumber"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::matchNumber(string)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if the contents of the string would be interpreted as a (signed) number in the current Context, <code>undef</code> otherwise.
  +
  +
|- style="vertical-align: top" id="Value:matchInfinite"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::matchInfinite(string)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if the contents of the string would be interpreted as an infinity in the current Context, <code>undef</code> otherwise.
  +
  +
|- style="vertical-align: top" id="Value::classMatch"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::classMatch($mo,"class",...)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$mo</code> is a MathObject belonging to one of the classes specified, and <code>0</code> otherwise. To be considered in the given class, <code>$mo</code> must satisfy one of the following:
  +
  +
# Its <code>class()</code> method returns the name of the given class. (Note that <code>class()</code> usually returns the last component of the object's package name, so an object in package <code>my::Real</code> would have <code>class()</code> return <code>"Real"</code> so could match <code>Value::matchClass($mo,"Real")</code>.
  +
# Its package name is <code>"Value::<i>class</i>"</code> for the given class.
  +
# The value of <code>$mo->{is<i>Class</i>}</code> is defined an non-zero (e.g., if <code>$mo->{isReal} = 1</code>, then <code>Value::matchClass($mo,"Real")</code> is true).
  +
# The package for <code>$mo</code> is the same as the Context's package associated with the given class (e.g., <code>Value::matchClass($mo,"Real")</code> will be true if <code>$mo->context->{value}{Real}</code> is <code>ref($mo)</code>).
  +
# Its class is a subclass of <code>Value::<i>Class</i></code> for the given class, and <code>$mo->{is<i>Class</i>}</code> is not <code>0</code>.
  +
  +
These conditions are checked for each class name passed to <code>classMatch()</code> until one matches or the end of the list is reached. It is safe to pass a non-MathObject to <code>classMatch()</code> as no methods are called directly on <code>$mo</code>.
  +
  +
|- style="vertical-align: top" id="Value::isContext"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isContext($x)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$x</code> is a reference to a Context object, <code>undef</code> otherwise.
  +
  +
|- style="vertical-align: top" id="Value::isParser"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isParser($x)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$x</code> is an instance of a class that is a subclass of <code>Parser::Item</code> (i.e., is part of a parse tree for a Formula object), and <code>undef</code> otherwise.
  +
  +
|- style="vertical-align: top" id="Value::isValue"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isValue($x)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$x</code> is a MathObject, and <code>undef</code> otherwise. To be a MathObject, <code>$x</code> must satisfy one of the following
  +
  +
# It is in a package whose name begins with <code>Value::</code>
  +
# It has the <code>{isValue}</code> set to something other than 0 (e.g., <code>$mo->{isValue} = 1</code>, or
  +
# It is an instance of a subclass of the <code>Value</code> class.
  +
  +
The latter two are the usual ways to make your own classes that act as MathObjects.
  +
  +
|- style="vertical-align: top" id="Value::isFormula"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isFormula($x)</code>
  +
| style="padding: 5px" | Shorthand for <code>Value::classMatch($x,"Formula")</code>
   
* <code>string</code>
 
  +
|- style="vertical-align: top" id="Value::isReal"
: 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 <code>Compute()</code> 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.
 
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isReal($x)</code>
  +
| style="padding: 5px" | Shorthand for <code>Value::classMatch($x,"Real")</code>
   
* <code>perl</code>
 
  +
|- style="vertical-align: top" id="Value::isComplex"
: Returns a string which represents the object as Perl source code. This is used internally, and would rarely be needed in a PG problem.
 
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isComplex($x)</code>
  +
| style="padding: 5px" | Shorthand for <code>Value::classMatch($x,"Complex")</code>
   
  +
|- style="vertical-align: top" id="Value::isNumber"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isNumber($x)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$x</code> represents a number, <code>undef</code> otherwise. If <code>$x</code> is a Formula, this is true when its return value is a number; otherwise it is true when <code>Value::classMatch($x,"Real","Complex")</code> or <code>Value::matchNumber($x)</code> is true. Note that this is true when <code>$x</code> is a Complex number as well as a Real number.
   
'''(Still need to add answer checker methods, like ans_rule and ans_array. Also, look at Value.pm for more, like isOne, isZero, etc.)'''
 
  +
|- style="vertical-align: top" id="Value::isRealNumber"
  +
| style="padding: 5px; white-space: nowrap" | <code>Value::isRealNumber($x)</code>
  +
| style="padding: 5px" | Returns <code>1</code> if <code>$x</code> represents a real number, <code>undef</code> otherwise. If <code>$x</code> is a Formula, this is true when its return value is a Real number; otherwise it is true when <code>Value::classMatch($x,"Real")</code> or <code>Value::matchNumber($x)</code> is true.
   
'''(Still need to add properties, like {correct_ans})'''
 
  +
|}
   
'''(Reformat list as table?)'''
 
  +
<!--
  +
Value::isHash
  +
Value::address
  +
Value::isBlessed
  +
Value::blessedClass
  +
Value::blessedType
  +
-->
   
 
<br>
 
<br>
   
 
[[Category:MathObjects]]
 
[[Category:MathObjects]]
  +
[[Category:Reference Tables]]
  +
[[Category:MathObject_Classes]]

Latest revision as of 16:30, 7 April 2021

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.,

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


Methods Common to All Classes

Method Description
$mo->cmp
$mo->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.
$mo->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). (The strings for the open and close parentheses or brackets for the Interval should be obtained using $mo->{open} and $mo->{close}).

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.

$mo->data Returns a reference to the array of data that represents the object.
$mo->length Returns the number of elements in the array returned by the value() method, e.g., the number of coordinates in a point or vector, or the number of rows in a matrix.
$mo->extract(i,...) Returns the [math]i[/math]-th element from the data for $mo. If more than one index is used, recursively extracts from the result, so that for a Matrix, $M->extract(i,j) returns the [math](i,j)[/math]-th entry, and for a list of lists, $L->extract(i,j) is the [math]j[/math]-th element of the [math]i[/math]-th list.
class->new(data)
$mo->new(data)
Creates a new instance of the class or the class of $mo using the given data. This what the various class constructor functions do; e.g., Real(5) effectively calls Value::Real->new(5).
class->make(data)
$mo->make(data)
Creates a new instance of the class or the class of $mo using the given data, but without all the error checking of type conversion. So data must be an array of MathObjects of the correct type. This is used internally when the types of the data are known to be correct, for efficiency, but it is always safer to use new.
$mo->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);

Note that the copy is not a deep copy (i.e., if $mo has references to other objects, the returned object will reference the same ones rather than copies of them). In particular, this is the case for the {data} property. Use $mo->copy to get a deeper copy, or $mo->new($mo->string) to get a separate version that shares no data with $mo

$mo->without("name",...) Returns a copy of $mo where the named properties have been removed. Note that, as with with(), the copy is not a deep one, so the two copies of $mo will share any references to other objects. In particular, this is the case for the {data} property. Use $mo->copy to get a deeper copy, or $mo->new($mo->string) to get a separate version that shares no data with $mo
$mo->copy Returns a copy of $mo with its {data} array copied as well, so that the copy doesn't share the data with the original. Note that setting $mo2 = $mo1 does not copy $mo1; instead, both $mo2 and $mo1 both point to the same MathObject, so changes to one will change the other. Use $mo2 = $mo1->copy to obtain a separate copy for $mo2.
$mo->context
$mo->context($context)
The first form returns the context in which $mo was created. When passed a reference to a Context object, this sets the context for $mo to be the given context.
$mo->inContext($context) This calls context($context) and then returns $mo so that it can be used in a chain of method calls, e.g.
$f->inContext($g->context)->with(limits => $g->{limits});
$mo->getFlag("name")
$mo->getFlag("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.
$mo->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).
$mo->class
$mo->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"
$mo->TeX Returns a string which represents the object in [math]\rm\TeX[/math] 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 [math]\rm\TeX[/math] expression in the problem's text, there is a special Context setting that tells MathObjects to output their [math]\rm\TeX[/math] 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 [math]\rm\TeX[/math] format.

$mo->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.
$mo->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.
$mo->eval
$mo->reduce
All MathObjects have these methods, but for most, they just return $mo. They are mostly used for the Formula class, but are available on all so that you don't have to check the type before calling them. A few classes, like Unions, implement reduce().
$mo->ans_rule(width)
$mo->named_ans_rule(width)
$mo->named_ans_rule_extension(width)
These operate like the PG macros of the same names, but are here as the counterpart to the ans_array methods below. That way, you can call one or the other in order to get the proper answer rules for $mo. It also helps to see what answer rule goes with what answer if you use \{$mo->ans_rule(10)\} in your BEGIN_TEXT/END_TEXT blocks rather than just \{ans_rule(10)\}.
$mo->ans_array(width)
$mo->named_ans_array(width)
$mo->named_ans_array_extension(width)
For Points, Vectors, and Matrices, these produce multiple answer blanks, one for each coordinate or entry, that are all tied to the one answer checker for $mo. This forces (or allows, depending on how you look at it) the student to enter each coordinate separately, while you still only need to handle one answer checker. See the Matrix documentation for an example.
$mo->isZero Returns 1 if $mo corresponds to whatever zero means for that type (e.g., for a Vector, it is a zero vector), and 0 otherwise.
$mo->isOne Returns 1 if $mo corresponds to whatever one means for that type (e.g., for a Matrix, it is an identity matrix), and 0 otherwise.
$mo->isSetOfReals Returns 1 if $mo is an Interval, Set, or Union (or subclasses of those), and 0 otherwise.
$mo->canBeInUnion Returns 1 if $mo is an Interval, Set, or Union, or is another object whose string version could look like an Interval (e.g., a Point with two coordinates where the first is less than the right). This is used in contexts where parentheses can produce Points, for example, to promote them to Intervals when appropriate.
$mo->showClass Returns a string representing the class of $mo suitable for use in error messages. The string includes "an" or "a", so
Value->Error("You can't take the square root of %s",$mo->showClass)

could produce the message "You can't take the square root of a Vector".

$mo->showType Returns a string representing the type of $mo suitable for use in error messages. Note that the class and the type are not the same; e.g., the type of a Formula is the type of it return value, while it's class is Formula (actually Value::Formula). So $f->showClass might be "a Formula returning a Real Number" for a formula $f, while $f->showType would produce "a Real Number".
$mo->inherit($mo1,...) Copy the properties that are not already present in $mo from $mo1 (and any other objects passed to it). This is used by things like the binary operations to make sure that the result inherits the attributes of the the two operands.
$mo->noinherit Returns the list of properties that should not be copied by inherit().
$mo->Package($context,"class",$noerrors) Returns the name of the package that implements the given class in the given Context. For example, $mo->Package($mo->context,"Real") probably will return "Value::Real". The Context could have override the default package for Reals, however, to use a subclass of Reals that implements additional functionality, in which case that subclass package name would be returned. If there is no package for the given class, an error will be produced, unless $noerrors is 1.


Support Functions

These are functions that are part of the Value package, but do not refer to a MathObject directly, so are not methods of any class. You call them from the Value package directly. Mostly they are general utility functions, or functions that test an unknown value to see if it is a MathObject or could be converted to one.

Method Description
Value->Error(message,$s1,...) Produces an error message by inserting the values of $s1 and any other arguments into the message using printf-style substitution, and then throws an error condition (i.e., your program or subroutine will stop at that point). If used in a custom answer checker, the error usually will appear in the messages area of the results table at the top of the page. For example,
Value->Error('You can't take the square root of %s',$mo->showType)
might produce the message "You can't take the square root of a Vector".
Value->NameForNumber(n) Returns a string like "first", "second", etc. that can be used in an error message. For example,
Value->Error("Your %s point is incorrect",Value->NameForNumber(i))

would produce the message "Your fifth point is incorrect" when i is 5. This can be useful in custom answer checkers that deal with lists of objects or coordinates of points or vectors.

Value::contextSet($context,flags) This sets the given flags to the given values in the given context and returns a hash representing the flags and their original settings. This is useful if you want to change the settings of flags temporarily (e.g., in an answer checker), but want to reset them when done. For example,
   my $flags = Value::contextSet($correct->context,
      reduceConstants => 1,
      reduceConstantFunctions => 0,
   );
   ... do something with Formulas here ...
   Value::contextSet($correct->context, %{$flags});

would set the value of reduceConstants and unset reduceConstantFunctions and return the original settings in $flags. After the processing that needs these flag settings is complete, the second call to contestSet() returns the values to their original settings.

Value::protectHTML(string) Returns a string where HTML special characters have been properly escaped (e.g., <, > and & have been replaced by their corresponding HTML entities.
Value::preformat(string) Returns a string where HTML special characters have been replaced by their HTML entities, and newlines have been replaced by <br>.
Value::makeValue(data) Attempts to convert the data into an appropriate MathObject and returns that. For example, a Perl real will be converted to a Real object, and a string will be converted to a String, provided it is in the current Context. It is safe to pass a MathObject to makeValue(), as it will be returned without change; thus you can use $x = makeValue($x) to guarantee that $x is a MathObject.
Value::matchNumber(string) Returns 1 if the contents of the string would be interpreted as a (signed) number in the current Context, undef otherwise.
Value::matchInfinite(string) Returns 1 if the contents of the string would be interpreted as an infinity in the current Context, undef otherwise.
Value::classMatch($mo,"class",...) Returns 1 if $mo is a MathObject belonging to one of the classes specified, and 0 otherwise. To be considered in the given class, $mo must satisfy one of the following:
  1. Its class() method returns the name of the given class. (Note that class() usually returns the last component of the object's package name, so an object in package my::Real would have class() return "Real" so could match Value::matchClass($mo,"Real").
  2. Its package name is "Value::class" for the given class.
  3. The value of $mo->{isClass} is defined an non-zero (e.g., if $mo->{isReal} = 1, then Value::matchClass($mo,"Real") is true).
  4. The package for $mo is the same as the Context's package associated with the given class (e.g., Value::matchClass($mo,"Real") will be true if $mo->context->{value}{Real} is ref($mo)).
  5. Its class is a subclass of Value::Class for the given class, and $mo->{isClass} is not 0.

These conditions are checked for each class name passed to classMatch() until one matches or the end of the list is reached. It is safe to pass a non-MathObject to classMatch() as no methods are called directly on $mo.

Value::isContext($x) Returns 1 if $x is a reference to a Context object, undef otherwise.
Value::isParser($x) Returns 1 if $x is an instance of a class that is a subclass of Parser::Item (i.e., is part of a parse tree for a Formula object), and undef otherwise.
Value::isValue($x) Returns 1 if $x is a MathObject, and undef otherwise. To be a MathObject, $x must satisfy one of the following
  1. It is in a package whose name begins with Value::
  2. It has the {isValue} set to something other than 0 (e.g., $mo->{isValue} = 1, or
  3. It is an instance of a subclass of the Value class.

The latter two are the usual ways to make your own classes that act as MathObjects.

Value::isFormula($x) Shorthand for Value::classMatch($x,"Formula")
Value::isReal($x) Shorthand for Value::classMatch($x,"Real")
Value::isComplex($x) Shorthand for Value::classMatch($x,"Complex")
Value::isNumber($x) Returns 1 if $x represents a number, undef otherwise. If $x is a Formula, this is true when its return value is a number; otherwise it is true when Value::classMatch($x,"Real","Complex") or Value::matchNumber($x) is true. Note that this is true when $x is a Complex number as well as a Real number.
Value::isRealNumber($x) Returns 1 if $x represents a real number, undef otherwise. If $x is a Formula, this is true when its return value is a Real number; otherwise it is true when Value::classMatch($x,"Real") or Value::matchNumber($x) is true.