Modifying Contexts (advanced)

From WeBWorK_wiki
Revision as of 14:44, 12 August 2012 by Dpvc (talk | contribs) (Add information about Variables, Constants, and Course-Wide Cusomization)
Jump to navigation Jump to search

Advanced Context Modifications

The Introduction to Contexts describes how to make basic modifications to a Context's variables, constants, strings, flags, functions, operators, and reduction rules. Here we will describe more advanced modifications and techniques involving the Context.

Number Formats

Real numbers are stored using a format that retains about 16 or 17 significant digits, making computations very accurate in most situations. When a number is displayed, you probably don't want to see all 17 digits (that would make a vector in three-space take up around 35 characters, for example). To make answers easier to read, MathObjects usually display only 6 significant digits. You can change the format used, however, to suit your needs. The format is determined by the Context()->{format}{number}, which is a printf-style string indicating how real numbers should be formatted for display.

The format always should begin with % and end with one of f, e, or g, possibly followed by #. Here, f means fixed-point notation (e.g. 452.116), e means exponential notation (e.g, 3.578E-5), and g means use the form most appropriate for the magnitude of the number. Between the % and the letter you can (optionally) include .n where n is the number of decimal digits to use for the number. If the format ends in #, then trailing zeros are removed after the number is formatted. (More sophisticated formats are possible, but this describes the basics.)

   Context()->{format}{number} = "%.2f";    # format numbers using 2-place decimals (e.g., for currency values).
   Context()->{format}{number} = "%.4f#";   # format numbers using 4-place decimals, but remove trailing zeros, if any.

The default format is "%g".

The Context also includes information about what should count as a number when an answer is parsed. There are two patterns for this, a signed number and an unsigned number. The latter is what is used in parsing numbers (and the sign is treated as unary minus); former is used in the Value::matchNumber() function. These are stored in the Context()->{pattern} hash; the default values are:

     Context()->{pattern}{number} = '(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?';
     Context()->{pattern}{signedNumber} = '[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?';

These are fairly complicated regular expressions that match the usual fixe-point and exponential notation for numbers in WeBWorK. It is possible to change these patterns to handle things like commas instead of decimals for European usage, or to allow commas every three digits. Note, however, that you would need to include a NumberCheck routine that would translate the special format into the required internal format. For example, this allows you to enter numbers as hexadecimal values:

   #
   #  Numbers in hexadecimal
   #
   Context()->{pattern}{number} = '[0-9A-F]+'; 
   Context()->{pattern}{signedNumber = '[-+]?[0-9A-F]+';
   Context()->flags->set(NumberCheck => sub {
     my $self = shift;                              # the Number object
     $self->{value} = hex($self->{value_string});   # convert hex to decimal via perl hex() function
     $self->{isOne} = ($self->{value} == 1);        # set marker indicating if the value is 1
     $self->{isZero} = ($self->{value} == 0);       # set marker indicating if the value is 0
   });
   Context()->update;

Note that after changing the pattern you must call Context()->update to remake the tokenization patterns used by the Context.

Here is an example that lets you use commas in your numbers:

   #
   # Allow commas every three digits in numbers
   #
   Context()->{pattern}{number} = '(:?(:?\d{1,3}(:?\,\d{3})+|\d+)(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?';
   Context()->{pattern}{signedNumber} = '[-+]?(:?(:?\d{1,3}(:?\,\d{3})+|\d+)(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?';
   Context()->flags->set(NumberCheck => sub {
     my $self = shift;                              # the Number object
     my $value = $self->{value_string};             # the original string
     $value =~ s/,//g;                              # remove commas
     $self->{value} = $value + 0;                   # make sure it is converted to a number
     $self->{isOne} = ($self->{value} == 1);        # set marker indicating if the value is 1
     $self->{isZero} = ($self->{value} == 0);       # set marker indicating if the value is 0
   });
   Context()->update;

If you want to make the numbers display with commas, then you will need to subclass the Value::Real object and override the string() and TeX() methods to insert the commas again, and then tie your new class into the Context()->{value}{Real} value. For example, in addition to the changes above, you might do

   #
   #  Subclass the Value::Real class and override its string() and TeX()
   #  methods to insert commas back into the output
   #
   package my::Real;
   our @ISA = ('Value::Real');    # subclass of this Value::Real
   
   sub string {
     my $self = shift; my $x = $self->SUPER::string(@_);  # get the original string output
     my ($n,@rest) = split(/([.E])/,$x,1);                # break it into the integer part and the rest
     while ($n =~ m/[0-9]{4}(,|$)/)                       # add commas as needed
       {$n =~ s/([0-9])([0-9]{3})(,|$)/$1,$2$3/}
     return join("",$n,@rest);                            # return the final string
   }
   
   sub TeX {
     my $self = shift;
     my $n = $self->SUPER::TeX(@_);     # original TeX uses string(), so commas are already there
     $n =~ s/,/{,}/g;                   # just make sure they have the correct spacing
     return $n;
   }
   
   package main;    # end of package my::Real;
   
   Context()->{value}{Real} = "my::Real";    # make the Context use my::Real rather then Value::Real
   Context()->{format}{number} = "%f#";      # format using "f" rather than "g", so no exponential notation

This could be put into a separate macro file that you could load into your problems whenever it is needed. See Creating Custom Contexts for details.


Lists and Delimiters

The Context object contains two more collections of data that were not mentioned in the Introduction to Contexts: the lists and parens objects. These are closely related, and determine what types of objects are created from various delimiters like braces and brackets. For example, in some contexts parentheses form Points, while in others they form Intervals or Lists. This it controlled by the settings in these two objects.

The lists object contains the definitions for the various types of list-like objects such as Points, Vectors, and Intervals. Each of the types of list has an entry that tells the parser what class implements the list, and specifies the open and close delimiters and the separators that will be used by default to display an instance of the class. A special case is AbsoluteValue, which is treated as a list since it has open and close delimiters, even though the list can only contain one element.

List Open Close Separator
Point ( ) ,
Vector < > ,
Matrix [ ] ,
List ,
Interval ( ) ,
Set { } ,
Union U
AbsoluteValue | |

This delimiters listed in this table are used when the object doesn't specify the delimiters explicitly via its {open} and {close} properties. This is usually the case when objects are created via the class constructors rather than parsing a string. For example, Vector(4,0,-1) would not have its {open} and {close} properties set, so would use the defaults in the lists object. Note that the Interval object has parentheses as its default delimiters, but the Interval() constructor will set the open and close properties automatically so that you can form open and closed intervals easily:

   $I1 = Interval(1,2);          # an open interval
   $I2 = Interval([1,2]);        # a closed interval
   $I3 = Interval("(",1,2,"]");  # a half-open interval

It is also possible to put the delimiters at the end of the interval (which is how the Interval's value() method returns them): Interval(0,1,"(","]").

On the other hand, instances of these objects created by parsing a string usually save the open and closing delimiters in the object's {open} and {close} properties, so the default will not be used in those cases. E.g., $v = Compute("<4,0,-1>") would produce a Vector object with the $v->{open} = "<" and $v->{close} = ">".

To change the list settings, use the set() method, as usual:

   Context()->lists->set(Vector => {open => "(", close => ")"};

Note that this only affects the case where the Vector object doesn't specify the open and close delimiters explicitly. It also doesn't change the delimiters that the parser uses to identify a vector, since the list values are only for output.

To change what delimiter to use for a given object type, you need to change the parens object. This associates each open delimiter to one of the list types given above, and also gives the close delimiter that is needed to match it, and some other data about how it can be used. So to complete the change for Vectors, we would need to use

   Context()->parens->set("(" => {type => "Vector", close => ")"});

The other possible data for a paren object includes:

Name Description
type=>"name" Specifies the list type that this open delimiter will genarate.
close=>"c" The closing delimiter for this opening one.
removable=>1 or 0 Do/don't remove delimiters when used around a single element. When 1, don't create a list, just return the element.
formInterval=>"c" When present, this indicates that an Interval should be formed when the list is closed by the character c rather than the usual close character.
emptyOK=>1 or 0 Do/don't allow empty delimiters. When 0 there must be at least one element between the open and close delimiters.


More about Variables

The Introduction to Contexts shows how to add variables to a Context. One thing to keep in mind is that most Contexts come with some variables pre-defined, and when you add new ones, the originals are still available. If you wish to have only the variables that you define, then use are() rather than add() to add the variables. For example,

   Context("Numeric");
   Context()->variables->add(t => "Real");

would add a new real variable [math]t[/math], which would be in addition to the [math]x[/math] that is already in the Numeric context, while

   Context("Numeric");
   Context()->variables->are(t => "Real");

would remove any pre-defined variables and leave you with only one variable, [math]t[/math].


More about Constants

The Introduction to Contexts shows how to add constants to a Context. Usually, the output for a constant is its name, but you might want to specify a different value, particular for its [math]\rm\TeX[/math] output. You can set the constant's TeX, string, and perl values to control the output in those formats. For example,

   Context("Complex");
   Context()->constants->set(i => {TeX=>'\boldsymbol{i}', perl=>'(i)'});

would indicate that [math]\rm\TeX[/math] output should be [math]\boldsymbol{i}[/math], while its Perl form should be just (i). Similarly,

   Context("Interval");
   Context()->constants->set(R => {TeX=>'\mathbb{R}'});

would set the R constant to produce [math]\mathbb{R}[/math] rather than [math]{\bf R}[/math] in [math]\rm\TeX[/math] output.


Adding New Functions

Adding New Operators

Course-Wide Customization

It is often the case that different courses use different notation for the same concept. For example, some use angle brackets for matrix delimiters, as in <4,0,-1>, while others use parentheses. Some use [math]{\bf i}[/math], [math]{\bf j}[/math], and [math]{\bf k}[/math] for the coordinate unit vectors, while others use [math]\hat\imath[/math], [math]\hat\jmath[/math], and [math]\hat k[/math], or [math]\vec\imath[/math], [math]\vec\jmath[/math], and [math]\vec k[/math], or some other notation. Some prefer the [math]ijk[/math]-notation and others the delimited coordinate form.

It would be nice if you could set your preferences for this globally within your course and have it affect all the problems you use, rather than having to edit them all to use the notation that you would like. Although you don't have perfect control over this, there are some things you can do to move in that direction. The mechanism for doing this is to use the parserCustomization.pl file to make customized versions of the Contexts from the problems you are using so that the Context settings match your preferences.

First, make a copy of pg/macros/parserCustomization.pl and put it in your course's templates/macros folder (using the File Manager). Then edit it to include Context changes that you desire. If you want to make changes to the Vector Context, for example, you will need to make a copy of that in the %context hash, and then make your modifications, as in the example below:

   $context{Vector} = Parser::Context->getCopy("Vector");
   $context{Vector}->flags->set(ijk=>1);                 # force output in ijk-notation
   $context{Vector}->parens->remove('<');              # force entry of vectors in ijk-notation

Note that the last command will cause questions that use Compute("<...>") to produce error messages, since you have disabled the angle brackets as a means of creating vectors.

To allow students to enter vectors using parentheses rather than angle brackets, use

   $context{Vector} = Parser::Context->getCopy("Vector");
   $context{Vector}->{cmpDefaults}{Vector} = {promotePoints => 1};
   $context{Vector}->lists->set(Vector=>{open=>'(', close=>')'});

This actually just turns Points into Vectors in the answer checker for Vectors, and displays Vectors using parens rather than angle brackets. The student is really still entering what MathObjects thinks is a Point, but since Points get promoted automatically, that should work. If a problem checks if a student's value is actually a Vector, however, that will not be true.

An alternative would be to use

   $context{Vector} = Parser::Context->getCopy("Vector");
   $context{Vector}->lists->set(Vector=>{open=>'(', close=>')'});
   $context{Vector}->parens->set('('=>{type=>'Vector'});

which actually does force the student's answer to be a Vector object. The problem here is that there is now no way to enter an actual Point, since parentheses now produce Vectors.

Other similar changes can be made, but such changes may cause some problems to fail, since the defaults are no longer what they expect them to be. So if you take this route, be sure to check the problems you use very carefully before assigning them to students.


See also