MathObject Answers - PGML

From WeBWorK_wiki
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,

 Context("Interval");
 BEGIN_PGML
 The interval from 0 to 1 excluding 0 but
 including 1 is written: [___________]{"(0,1]"}
 END_PGML

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.

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

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.

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

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.

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

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

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

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

 Context("Point");
 $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
   }
 );
 
 BEGIN_PGML
 Find a point [:(x,y):] that is a solution to [: x+y = [$a] :].
 
 [: (x,y) :] = [______________________]{$cmp}
 END_TEXT

Answer Arrays

MultiAnswer Checkers

Custom Checkers