WeBWorK Problems

Multiplying matrices

Multiplying matrices

by Jason Aubrey -
Number of replies: 2
Hi,

In the problem below students are getting the message "matrix dimensions not correct" for the first part whose dimensions are 1x1. I cant't find the problem. Any ideas?

Thanks,
Jason


DOCUMENT(); # This should be the first executable line in the problem.

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"MathObjects.pl",
"PGmatrixmacros.pl"
);

TEXT(beginproblem());

$a11 = non_zero_random(-9,9,1);
$a12 = non_zero_random(-9,9,1);
$a13 = non_zero_random(-9,9,1);
$a14 = non_zero_random(-9,9,1);

$b11 = non_zero_random(-9,9,1);
$b21 = non_zero_random(-9,9,1);
$b31 = random(-9,9,1);
$b41 = random(-9,9,1);



Context("Matrix");
$A = Matrix( [ [$a11,$a12,$a13,$a14] ]);
$B = Matrix( [ [$b11], [$b21], [$b31], [$b41] ]);
$ans1 = $A*$B;
$ans2 = $B*$A;

$showPartialCorrectAnswers = 1;

############################################
Context()->texStrings;
BEGIN_TEXT
$ans1
Let \(A\) and \(B\) be the following matrices.
$PAR
\[
A = $A, \qquad B = $B
\]

$PAR
Perform the following operations:
$PAR
\( A \cdot B = \) \{ $ans1->ans_array\}
$PAR
\( B \cdot A = \) \{ $ans2->ans_array\}

END_TEXT
Context()->normalStrings;
ANS($ans1->cmp);
ANS($ans2->cmp);
ENDDOCUMENT();
In reply to Jason Aubrey

Re: Multiplying matrices

by Jason Aubrey -
Hi - this might be the issue. I added this to the problem:

ANS( $ans1->cmp( checker=>sub {
  my ( $correct, $student, $ansHash ) = @_;
  @d1 = $correct->dimensions;
  @d2 = $student->dimensions;
 warn "Correct dims: @d1 vs Student dims: @d2";
} ) );

which gave me the following when I submitted an answer:
  • Correct dims: 11 vs Student dims: 1 at line 85 of (eval 3229)
Now in pg/lib/Value/AnswerChecker.pm cmp_preprocess checks for this case and does 
  $ans->{student_value} = $student->make([$student->value])
to try to fix it.  

Only it doesn't fix it: putting in warn statements after that and again in cmp_postprocess shows that the student answer is still one dimensional and the correct answer is 2 dimensional.  

I thought maybe in cmp_preprocess and cmp_postprocess that doing
$correct = $ans->{correct_value};
and then comparing
$correct->dimensions and $student->dimensions 
instead of 
$self->dimensions and $student->dimensions
would work along with setting 
$ans->{correct_value} = $correct->make([$correct->value])
in cmp_preprocess if the dimensions correct ans student dimensions come in as 2 and 1.  

Doing that, I no longer get "Matrix dimension is not correct" but I do get "incorrect" (incorrectly).

Thanks,
Jason
In reply to Jason Aubrey

Re: Multiplying matrices

by Davide Cervone -
Try changing
    $ans->{student_value} = $student->make([$student->value])
to
    $ans->{student_value} = $student->make([[$student->value]])
instead. I think that might do it for you (though I haven't actually tested it out).

Davide