WeBWorK Problems

Weird behavior of matrix evaluation

Weird behavior of matrix evaluation

by Robert Mařík -
Number of replies: 9

Hello.

I am new in WeBWorK and tried to write exercise on matrix. See the attached file. One zip file with pg on matrix sum. It works. However, if I enter an expression in x rather than a number and click preview, I get the message that there is something wrong and the student should email the teacher. See the printscreens in the same zip file.

Is this intended behavior? Or is it possible to enhance the code of the question somehow?

With best regards

Robert Marik

In reply to Robert Mařík

Re: Weird behavior of matrix evaluation

by Davide Cervone -

Your problem works as expected for me. I get an appropriate error message in the results table indication that the entry is not a number but a formula.

What version of WeBWorK and PG are you using?

Attachment ww.png
In reply to Davide Cervone

Re: Weird behavior of matrix evaluation

by Robert Mařík -
Hello. Thank you for the answer. I use ww_version: 2.15 | pg_version 2.15 .
Also works for me if I submit the answer. The error message appears only if I click Preview. I fear that students will be disturbed by such a message.

Anyway thank you for confirmation that the question is OK.

Robert
In reply to Robert Mařík

Re: Weird behavior of matrix evaluation

by Glenn Rice -

I am seeing this also.  I have tested on both webwork 2.15 and the develop branch.  I haven't tested earlier versions of webwork.  It only happens when you click "Preview My Answers" and not with "Submit Answers" or "Check Answers".  This is clearly a bug.  I have not been able to determine where the bug is occurring yet though.

In reply to Robert Mařík

Re: Weird behavior of matrix evaluation

by Davide Cervone -
Sorry, I didn't realize it was only for previewing. I will check further.
In reply to Davide Cervone

Re: Weird behavior of matrix evaluation

by Glenn Rice -

It seems that the problem is with the class of the student answer.  In the case that a variable is entered in an entry of the matrix, the student answer will have type "Matrix", but class "Formula".  So the cmp_preprocess method passes line 1033 of pg/lib/Value/AnswerChecker.pm, and then fails on line 1034 as the Value::Formula package does not have a "dimensions" method.  If only numbers are entered in all matrix entries, then the student answer will have type and class of "Matrix", which does have the "dimensions" method.

It you change the check on line 1033 to check the "class" instead of the "type", then this issue seems to be fixed.  I am not sure if this is the proper fix for this though.

Davide:  What do you think?

In reply to Glenn Rice

Re: Weird behavior of matrix evaluation

by Davide Cervone -
Sorry, didn't see your answer before I wrote mine (I didn't refresh the page before answering). Yes, you are right, that is the issue, and the fix that I proposed as well.
In reply to Glenn Rice

Re: Weird behavior of matrix evaluation

by Glenn Rice -

No problem.  It is the typical issue that happens with the delay on responses being posted.

In reply to Robert Mařík

Re: Weird behavior of matrix evaluation

by Davide Cervone -

OK, I found the issue. When the student checks or submits the answer, the error message about the the student's answer being a formula is generated while the individual entries are being collected and processed into the matrix object, and because an error was produced, the student's matrix is not produced, and no further checking is done. But in preview mode, that message is not produced (the type checking is not performed), and so the student answer is created (as a Formula object returning a matrix value), and the rest of the answer checking is performed. One of the actions taken for matrix answer checking is a test to see if the student matrix is the same size as the correct answer matrix. That is done by calling the matrix's dimensions method. But when the student answer contains the x, that answer is a Formula, not a Matrix object, and so doesn't have a dimensions() method to call. That causes the answer checker to die (apparently silently), without returning the requires AnswerHash object, which is the source of the error you are seeing.

The checking of the dimensions is in Matrix object's cmp_preprocess() method. There is a check there that the student's answer is a Matrix, but the check is actually that its return type is a Matrix, whereas it probably should be that the object's class is a Matrix (since that is what guarantees the presence of the dimensions() method). So one possible fix is to change

return if $student->type ne 'Matrix';

to

return if $student->classMatch('Matrix');

at line 1033 of pg/lib/Value/AnswerChecker.pm.

Another would be to add a dimensions() method to the Formula object that returns the dimension of the matrix result (if it is a matrix), and a reasonable value for other things (like the size of a vector, or the length of a list, or 1 for a number). But that would suggest that it would be reasonable to have a dimensions() method for all MathObjects. That would be a reasonable addition, but the cmp_preprocess() fix is easier to do as a work-around, so I present that below.

One way to do it would be to add

package my::Matrix;
our @ISA = ('Value::Matrix');

sub cmp_preprocess {
  my $self = shift; my $ans = shift;
  my $student = $ans->{student_value};
  return if !$student->classMatch('Matrix');
  return $self->SUPER::cmp_preprocess($ans);
}

package main;

Context("Matrix");
Context->{value}{Matrix} = 'my::Matrix';

to your problem file. This defines a subclass of Value::Matrix that has the patched cmp_preprocess() function.

Of course, you don't want to have to do this for every file containing matrices, and since it is a general problem, you may want to work around it for all problems involving matrices. You can do that by using the File Manger to create a file in your course's templates/macros folder named parserCustomization.pl that contains

package my::Matrix;
our @ISA = ('Value::Matrix');

sub cmp_preprocess {
  my $self = shift; my $ans = shift;
  my $student = $ans->{student_value};
  return if !$student->classMatch('Matrix');
  return $self->SUPER::cmp_preprocess($ans);
}

package main;

$context{Matrix} = Context("Matrix");
Context->{value}{Matrix} = 'my::Matrix';

Context("Numeric");

This file will be loaded by every problem that loads the MathObjects.pl file. It makes a local copy of the Matrix context that will be used whenever Context("Matrix") is called and patches it to include the patched matrix subclass. That should take care of it for now until it is fixed in WeBWorK.