MatrixCustomAnswerChecker1

From WeBWorK_wiki
Jump to navigation Jump to search
This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Matrices and Custom Answer Checkers

Click to enlarge

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


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
"AnswerFormatHelp.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[0]); 
      $s1 = Matrix($s[1]);
      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[0] is converted to a MathObject matrix $s0 using $s0 = Matrix($s[0]); 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();

Answer Evaluation:

Templates by Subject Area