(Difference between revisions)

## Matrices and Custom Answer Checkers

This PG code shows how to use a multianswer answer checker to evaluate an open-ended question about matrices.

PG problem file Explanation

Problem tagging:

DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
);
$showPartialCorrectAnswers = 0; TEXT(beginproblem());  Initialization: Context('Matrix');$A = Matrix([[1,1],[0,1]]);
$B = Matrix([[1,0],[1,1]]);$multians = MultiAnswer($A,$B)->with(
singleResult => 1,
checker => sub {
my ( $correct,$student, $answerHash ) = @_; my @s = @{$student};
$s0 = Matrix($s);
$s1 = Matrix($s);
return $s0 *$s1 != $s1 *$s0;
}
);


Setup: Construct two matrices $A and $B that do not commute and therefore serve as a correct answer. Use a $multians object with a custom answer checker subroutine. The answer checker uses my ($correct, $student,$answerHash ) = @_; to grab the inputs (the correct answer, the student answer, and the answer hash table info). Then, put the student's two answers into an array @s using my @s = @{$student};. Make sure the student's first matrix $s is converted to a MathObject matrix $s0 using $s0 = Matrix($s); and similarly for the student's second matrix. The return value, which is boolean, is the truth value of the statement $s0 * $s1 !=$s1 * $s0. Context()->texStrings; BEGIN_TEXT Give an example of two $$2 \times 2$$ matrices $$A$$ and $$B$$ such that $$AB \ne BA$$.$BR
$BR $$A =$$ \{$multians->ans_array(5) \}
$BR$BR
$$B =$$
\{ $multians->ans_array(5) \} END_TEXT Context()->normalStrings;  Main Text: Make sure that both answer arrays are called as methods on the $multians object (i.e., $multians->ans_array(5) should be called for each answer array. Note that ans_array(w) produces an answer array of boxes each w characters wide. install_problem_grader(~~&std_problem_grader); ANS($multians->cmp() );

COMMENT('MathObject version.');

ENDDOCUMENT();