WeBWorK Main Forum

This does not appear to be a matrix

This does not appear to be a matrix

by Joel Trussell -
Number of replies: 4
The code in Context("Matrix") appears to work and produces the right answers, but I get the error message

PG warning message------

---- main (eval 982) 774 ------
This does not appear to be a matrix

The code appears below

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

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

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context("Matrix");

$a=non_zero_random(-3,3,1);
$c=non_zero_random(-3,3,1);
$b=-$c-2+$a*$c;
$A = Matrix([$a, $b, 1], [-1, 1, -1], [1, $c, 0]);
$A_inv = Compute($A->inverse);
BEGIN_TEXT
This problem is related to Problem 5.21 in the text.

$PAR
Given the matrix $BR
A = \{ mbox( display_matrix($A), "," ) \}
$BR
(a) does the inverse of the matrix exist?
$BR
Your answer is (input Yes or No): \{ans_rule(15) \}
$BR
(b) if your answer is Yes, write the inverse here; if the answer is NO, enter all zeros here:
$BR
\( A^{-1} = \) \{ mbox( answer_matrix(3,3,10), "." ) \}
END_TEXT

$ans0 = "Yes";
ANS(str_cmp($ans0));
ANS num_cmp(ra_flatten_matrix($A_inv));

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$BBOLD SOLUTION $EBOLD
$PAR
The matrix is invertible $BR
\( A^{-1} = \) \{ mbox( display_matrix($A_inv) )\}

END_SOLUTION

ENDDOCUMENT(); # This should be the last executable line in the problem.

In reply to Joel Trussell

Re: This does not appear to be a matrix

by Danny Glin -
It looks like you are using MathObjects matrix objects to construct $A and $A_inv, and then trying to use the old matrix answer checker from PGmatrixmacros.pl to check the answer.

I believe ra_flatten_matrix expects a different type of matrix object (the one created by the macros in PGmatrixmacros.pl), which is likely why it is giving the error.

Try changing
\( A^{-1} = \) \{ mbox( answer_matrix(3,3,10), "." ) \}
to
\( A^{-1} = \) \{ mbox( $A_inv->ans_array(), "." ) \}

and
ANS num_cmp(ra_flatten_matrix($A_inv));
to
ANS( $A_inv->cmp());

This will use the MathObjects answer checker, which is the one compatible with MathObjects matrices.

The one downside to this is that it checks if the entire matrix is equal to the correct matrix, which means that there is no partial credit if some entries are correct. The ra_flatten_matrix subroutine converts a matrix into a one-dimensional array, which can then be compared against a set of independent answer blanks, such as those generated by answer_matrix, which means that in your case it would be doing nine separate checks.

If you want to preserve your code, there may be a way to convert a MathObjects matrix back to an old-style matrix which is compatible with the rest of your code, but I don't know what it is off hand.
In reply to Danny Glin

Re: This does not appear to be a matrix

by Joel Trussell -
Thanks - I inserted the changes but still get the same error. The correct answer is given as a matrix and not as a list as originally. So that works. But what causes the error message now. The code is given below.

But I'm coding this for another instructor. He strongly wants to have the answers checked element by element. The code he wants for complex number operations has a blank for the real part and one for the imaginary part. He wanted to avoid checking the complex number directly. Hey, it is his class. So I would be interested in changing the matrix into a 1-D array. I can set th e9 element as separate variables and call the answer checker 9 times.
ANS(num_cmp($ans1));
...
ANS(num_cmp($ans9));

Thanks

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

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

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context("Matrix");

$a=non_zero_random(-3,3,1);
$c=non_zero_random(-3,3,1);
$b=-$c-2+$a*$c;
$A = Matrix([$a, $b, 1], [-1, 1, -1], [1, $c, 0]);
$A_inv = Compute($A->inverse);
BEGIN_TEXT
This problem is related to Problem 5.21 in the text.

$PAR
Given the matrix $BR
A = \{ mbox( display_matrix($A), "," ) \}
$BR
(a) does the inverse of the matrix exist?
$BR
Your answer is (input Yes or No): \{ans_rule(15) \}
$BR
(b) if your answer is Yes, write the inverse here; if the answer is NO, enter all zeros here:
$BR
\( A^{-1} = \) \{ mbox( $A_inv->ans_array(), "." ) \}
END_TEXT

$ans0 = "Yes";
ANS(str_cmp($ans0));
ANS( $A_inv->cmp());

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$BBOLD SOLUTION $EBOLD
$PAR
The matrix is invertible $BR
\( A^{-1} = \) \{ mbox( display_matrix($A_inv) )\}

END_SOLUTION

ENDDOCUMENT(); # This should be the last executable line in the problem.
In reply to Joel Trussell

Re: This does not appear to be a matrix

by Danny Glin -
So it turns out I totally misdiagnosed the problem.

The old answer checkers work fine with the new MathObjects matrices. The error is being generated by "display_matrix". Since MathObjects have their own built in method for displaying matrices, you don't need display_matrix at all.

You can use your original code with the following modifications:

Immediately before BEGIN_TEXT, add:
Context()->texStrings;

Immediately after END_TEXT, add:
Context()->normalStrings;

Change the line A = \{ mbox( display_matrix($A), "," ) \} to:
\(A = $A\),

In the solution, change the line \( A^{-1} = \) \{ mbox( display_matrix($A_inv) )\} to:
\( A^{-1} = $A_inv \)


On a side note, I highly recommend coding yes/no questions as drop-downs (see http://webwork.maa.org/wiki/PopUpLists). It prevents many headaches caused by students entering weird variants of the answer (capitalization, spaces, etc).
In reply to Danny Glin

Re: This does not appear to be a matrix

by Joel Trussell -
Great. That works.I can use
ANS num_cmp(ra_flatten_matrix($A_inv));
to get the individual elements checked and still display matrices in the problem statement and the solution.

Thanks a bunch!