WeBWorK Problems

Problems with problems with optional matrices

Problems with problems with optional matrices

by Christopher Heckman -
Number of replies: 1
I want to write a WeBWorK problem about diagonalization

(a) Is the matrix \(A\) diagonalizable? [Enter ${LQ}Y${RQ} or ${LQ}N${RQ},
without the quotation marks.] \{ $mp -> ans_rule (3) \}
$PAR
(b) If your answer to part (a) was Y, find matrices \(P\) and \(D\) such that
\(D\) is a diagonal matrix and \(A = PDP^{-1}\); enter them below.$BR
\(P =~\)\{ $mp -> ans_array \} $BR and
\(D =~\)\{ $mp -> ans_array \}
$PAR
(c) If your answer to part (a) was N, the reason is because the dimension
of the eigenspace of \(\lambda =~\)\{ $mp -> ans_rule (5) \} is not equal
to its multiplicity.


As you can see, this is best done using a multiple answer object. Now, if the answer is N, I want the matrices to be optional. However ... and this is what I'm having problems with ... if the answer is N, and the student enters only N and the correct value of lambda, then the answer is counted wrong. (If the student enters an N, the value of lambda, and at least one entry into the spots for D and P, then the problem works.) My checker looks like:

Context()->parens->set("[" => {formMatrix => 1});

$mp = MultiAnswer ($YN, $P, $D, $L0) -> with (
singleResult => 1,
checkTypes => 0,
allowBlankAnswers => 1,
checker => sub {
my ($correct, $student, $self) = @_; # get the parameters
my ($corrYN, $corrP, $corrD, $corrL0) = @{$correct};
my ($stuYN, $stuP, $stuD, $stuL0) = @{$student};
if (! Value::classMatch ($stuYN, 'String')) { return 0; }
if ($corrYN ne $stuYN) { return 0; }
if (! Value::classMatch ($stuP, 'Matrix'))
{ $stuL0 = $stuP; $stuP = Matrix ([[0]]); $stuD = Matrix ([[0]]); }
my $i, $j;

if ($corrYN eq 'Y') {
if (! Value::classMatch ($stuP, 'Matrix')) { return 0; }
if (! Value::classMatch ($stuD, 'Matrix')) { return 0; }
for ($i = 1; $i <= 4; $i++) { for ($j = 1; $j <= 4; $j++) {
if (($i != $j) && ($stuD -> element ($i, $j) != 0)) { return 0; }
}}
if ($stuP -> det == 0) { return 0; }
my $corrA = Matrix ($corrP * $corrD * $corrP -> inverse);
my $stuA = Matrix ($stuP * $stuD * $stuP -> inverse);
return (Matrix ($corrA - $stuA) -> isZero);
}
else { return ($stuL0 == $corrL0); }
}
);


It seems that my error is coming from the fact that, if you try to create a matrix with no entries, an error is produced, which causes the answer to be counted as incorrect. Is there a way to bypass that error, short of recoding Matrix.pm in WeBWorK?

[Sorry about the formatting. WWforums's fault.]
In reply to Christopher Heckman

Re: Problems with problems with optional matrices

by Christopher Heckman -
I've written up a short, complete problem to illustrate the problem I'm having. It asks for a matrix and a number; the matrix is optional, and the problem is supposed to say "correct" in all situations.
----------------------------------

DOCUMENT();

loadMacros(
"PGstandard.pl", 
"MathObjects.pl", 
"PGunion.pl",
"parserMultiAnswer.pl",
"PGcourse.pl",
);

Context ("Matrix");
Context()->parens->set("[" => {formMatrix => 1});

$showPartialCorrectAnswers = 1;
$A = Matrix ([[1,0],[0,1]]);
$B = 42;
$ma = MultiAnswer ($A, $B) -> with (
   singleResult => 1,
   checkTypes => 0,
   allowBlankAnswers => 1,
   checker => sub { 1; }  
   );

TEXT(beginproblem());

BEGIN_TEXT
Enter a matrix and a number. Or don't enter the matrix, if you don't want to.
$PAR
Matrix: \{ $ma -> ans_array \}
$PAR
Number: \{ $ma -> ans_rule \}
END_TEXT

Context()->normalStrings;

ANS ($ma -> cmp);

COMMENT('MathObject version.');

ENDDOCUMENT();

----------------------------------
Oddly enough, if I change the ans_array to ans_rule, WeBWorK does what it is supposed to: Leaving the matrix blank and entering a number is a "correct" answer.