## WeBWorK Problems

### Re: Ans_rule with matrix

by Ryan Maccombs -
Number of replies: 0
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() ;

"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]}[0..#{a[0]}])\} \cr \{join("& ",@{a[1]}[0..#{a[1]}])\} \cr \{join("& ",@{a[2]}[0..#{a[2]}])\} \cr \end{array}\right] \quad \mbox{ and } \quad B = \left[\begin{array}{ccc} \{join("& ",@{b[0]}[0..#{b[0]}])\} \cr \{join("& ",@{b[1]}[0..#{b[1]}])\} \cr \{join("& ",@{b[2]}[0..#{b[2]}])\} \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() ;