## WeBWorK Problems

### Ans_rule with matrix ### Ans_rule with matrix

by Thomas Wong -
Number of replies: 1
Hi everyone,

I would like to embed some answer boxes as part of a matrix and am wondering if this can be done?

For example, if I want students to enter the full matrix as their solutions, I've managed to get the following to work:

\{"$$P =$$".$PP->ans_array(2)\} For a second part, the answer is a diagonal matrix. Instead of entering a large number of zeros, I've created a multianswer checker (called DD say) and attempted to create a partially completed matrix with the zeros filled in using a similar logic to the previous part. So students only have to enter the diagonal entries \{"$$D = \begin{bmatrix} ".DD->ans_rule(2)".&0&0&0&0&0\\ 0&".DD->ans_rule(2)."&0&0&0&0\\ 0&0&".DD->ans_rule(2)."&0&0&0\\ 0&0&0,".DD->ans_rule(2)."&0&0\\ 0&0&0&0&".DD->ans_rule(2)."&0\\ 0&0&0&0&0&".DD->ans_rule(2)."\\ \end{bmatrix}$$"\} However, this resulted in errors and I have been unable to resolve them. Suggestions on how to resolve this or alternative ways to approach this problem are welcomed. Thank you very much for your help. Regards, Thomas In reply to Thomas Wong ### Re: Ans_rule with matrix by Ryan Maccombs - The open problem Library has lots of examples of this. Here is one of the first problems I saw ## DESCRIPTION ## Matrix Algebra ## ENDDESCRIPTION ## Tagged by tda2d ## DBsubject(Linear algebra) ## DBchapter(Matrices) ## DBsection(Matrix algebra) ## Institution(ASU) ## MLT(matrix_mult) ## Level(2) ## KEYWORDS('Algebra' 'Matrix' 'Matrices') DOCUMENT() ; loadMacros( "PGstandard.pl", "PGasu.pl", "PGchoicemacros.pl", "PGdiffeqmacros.pl", "PGmatrixmacros.pl", "PGcourse.pl" ); ############ TEXT(beginproblem()); foreach$i (0..2) {

foreach $j (0..2) {$a[$i][$j] = random(-4,4,1);
$b[$i][$j] = random(-4,4,1); } }$showPartialCorrectAnswers = 1 ;

BEGIN_TEXT

If

$A = \left[\begin{array}{ccc} \{join("& ",@{a}[0..#{a}])\} \cr \{join("& ",@{a}[0..#{a}])\} \cr \{join("& ",@{a}[0..#{a}])\} \cr \end{array}\right] \quad \mbox{ and } \quad B = \left[\begin{array}{ccc} \{join("& ",@{b}[0..#{b}])\} \cr \{join("& ",@{b}[0..#{b}])\} \cr \{join("& ",@{b}[0..#{b}])\} \cr \end{array}\right]$

then

BCENTER \{ mbox( '$$A B =$$', display_matrix([[ans_rule(5),ans_rule(5),ans_rule(5)], [ans_rule(5),ans_rule(5),ans_rule(5)], [ans_rule(5),ans_rule(5),ans_rule(5)]], 'align'=>"ccc")), \}ECENTER

and

BCENTER \{ mbox( '$$BA =$$', display_matrix([[ans_rule(5),ans_rule(5),ans_rule(5)], [ans_rule(5),ans_rule(5),ans_rule(5)], [ans_rule(5),ans_rule(5),ans_rule(5)]], 'align'=>"ccc")), \}ECENTER

END_TEXT

foreach $i (0..2) { foreach$j (0..2) {
$sum = 0; foreach$k (0..2) {
$sum =$sum + $a[$i][$k]*$b[$k][$j];
}
ANS(num_cmp($sum) ); } } foreach$i (0..2) {

foreach $j (0..2) {$sum = 0;
foreach $k (0..2) {$sum = $sum +$b[$i][$k]*$a[$k][$j]; } ANS(num_cmp($sum) );
}
}
ENDDOCUMENT() ;