WeBWorK Problems

error messages for matrix answers

error messages for matrix answers

by Richard Lynch -
Number of replies: 2
Hi Everyone,

I'm writing a T/F problem that says "Every matrix A has a determinant." This is obviously false, and so in order to make the students have to do something more, I would like them enter a non-square matrix as an example. I have actually successfully made this possible in the Matrix context using:

ANS(Matrix([[0,2]])->cmp(
  checker=>sub {
    ($correct,$student) = @_;
    return 0 unless Value::classMatch($student, 'Matrix');
    return !($student->isSquare);
  }
));

However, if they enter a square matrix, it throws the message "Matrix dimension is not correct." I would like to make it say "The matrix you entered has a determinant" so I tried to add the line 

$correct->context->setError("The matrix you entered has a determinant", $Value::CMP_WARNING) if $student->isSquare;

inside of the answer checker, but it didn't work. I also tried the usual Context()->{error}{msg}{""} = "" method, but that also didn't work. Could someone tell me where I'm going wrong?

Thanks a ton!
Rick
In reply to Richard Lynch

Re: error messages for matrix answers

by Richard Lynch -
After a more careful read of of the Matrix class wiki page, I realized I just need to add showDimensionHints=>0 to the checker, so never mind my question!
In reply to Richard Lynch

Re: error messages for matrix answers

by Davide Cervone -
Here is a version that sets the error message that you requested.
    ANS(Matrix([[1,2]])->cmp(
      showDimensionHints => 0,
      checker=>sub {
        ($correct,$student,$ans) = @_;
        my $isSquare = $student->isSquare;
        $ans->{ans_message} = "Your matrix has a determinant"
           if $isSquare && !$ans->{isPreview};
        return !$isSquare;
      }
    ));