MathObject Answers - PGML

From WeBWorK_wiki
Revision as of 09:16, 13 May 2015 by Dpvc (talk | contribs) (Move custom checker example to custom checkers section.)
Jump to navigation Jump to search

Answers from MathObejcts

When you specify an answer following an answer blank and provide a number or a swing containing a formula, PGML turns your answer into a MathObject (essentially by passing it to Compute()). So you can provide any sort of MathObject-based answer by enclosing it in quotation marks. For example,

 The interval from 0 to 1 excluding 0 but
 including 1 is written: [___________]{"(0,1]"}

provides an answer that is an interval. The answer is parsed in the current context, which is the Interval context in this example. This means that you are giving the answer exactly as the student will.

Instead of using quotation marks, you can use a MathObject creator function, like Real() or Matrix() if you prefer.

 As a complex number, [: sqrt(-1) :] is written [__________]{Complex(0,1)}

If the determination of the answer involves computations, however, it may be more convenient to produce a MathObject earlier in the problem and pass that to PGML. You can do that by putting the variable that holds the math object into the braces following the answer blank.

 $p = Point(0,2), $q = Point(1,-1);
 $v = Vector($q-$p);
 A vector from [`[$p]`] to [`[$q]`] is [______________]{$v}

Passing Options to Answer Checkers

If you need to pass options to the answer checker for a MathObject, you can pass the answer checker to PGML rather than the MathObject itself.

 $a = 3, $b = 5;
 $v = Vector(-$b,$a);
 A vector perpendicular to [: <[$a],[$b]> :]* is [_____________}{$v->cmp(parallel=>true)}

Alternatively, you can save the answer checker in a variable and pass that to PGML for easier reading.

 $a = 3, $b = 5;
 $v = Vector(-$b,$a);
 $cmp = $v->cmp(parallel=>true);
 A vector perpendicular to [: <[$a],[$b]> :]* is [_____________}{$cmp}

This is particularly useful if you want to provide a custom checker.

Answer Arrays

For a Matrix, Vector, or Point object, you may want to ask the student to type each entry in a separate answer blank, rather than entering the whole object in one answer blank (for example, to prevent the use of vector calculations). In the traditional BEGIN_TEXT/END_TEXT setting, you would use its an_array() rather than its ans_rule() method.

In PGML, you indicate that an answer blank should produce an answer array by putting an asterisk between the blank and the answer.

 $M = Matrix([1,2],[3,4]);
 If [`M = [$M]`], then [`M^2 =`] [___]*{$M**2}

Here, the size of the answer rule determines the size of each rule in the answer array.

MultiAnswer Checkers

To use a MultiAnswer object in PGML, create it outside the PGML block, and use the MultiAnswer variable for more than one answer blank.

 $mp = MultiAnswer(12,6)->with(
   singleResult => 1,
   separator => " and ",
   tex_separator => "\text{ and }",
   checker => sub {
     my $correct= shift; my $student = shift;
     my ($ca,$cb) = @$correct;
     my ($sa,$sb) = @$student;
     my $ok = ($ca == $sa && $cb == $sb) ||
              ($ca == $sb && $cb == $sa);
     return ($ok ? (1,1) : (0,0));
 [_______]{$mp} and [_______]{$mp}

See the [documentation for] for more details about using MultiAnswer objects.

Custom Checkers

Since custom checkers for math objects usually consist of more than one line, it is awkward to include them within the PGML block itself. Instead, use a variable to store the answer checker with the custom checker and pass that to PGML.

 $a = random(2,10,1);
 $x = random(-5,5,1);
 $y = $a - $x;
 $cmp = Point($x,$y)->cmp(
   showCoordinateHints => 0,                # doesn't make sense to give hints in this case
   checker => sub {
     my ($correct,$student,$ansHash) = @_;  # get correct and student MathObjects
     my ($sx,$sy) = $student->value;        # get coordinates of student answer
     return ($sx + $sy == $a ? 1 : 0);      # return 1 if correct, 0 otherwise
 Find a point [:(x,y):] that is a solution to [: x+y = [$a] :].
 [: (x,y) :] = [______________________]{$cmp}