# MatrixOperations1

## Matrix Operations

Click to enlarge

This PG code shows how to assess whether a student knows whether two matrices can be multiplied and, when the matrix product exists, what the dimensions of a matrix product are.

PG problem file Explanation

Problem tagging:

```DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
```

Initialization: We use `parserPopUp.pl` for the multiple choice part of the question. Use `\$showPartialCorrectAnswers = 0;` to withhold feedback (we will use an all-or-nothing answer grader).

```Context('Matrix');

\$A = Matrix([
[non_zero_random(-5,5,1),non_zero_random(-5,5,1)],
[non_zero_random(-5,5,1),non_zero_random(-5,5,1)],
]);

\$B = Matrix([
[non_zero_random(-5,5,1),non_zero_random(-5,5,1),non_zero_random(-5,5,1)],
[non_zero_random(-5,5,1),non_zero_random(-5,5,1),non_zero_random(-5,5,1)],
]);

\$popup = PopUp(['Choose','True','False'],'False');
```

Setup: Create two 2 by 3 matrices and a true/false popup.

```Context()->texStrings;
BEGIN_TEXT
Let
\[ A = \$A,\]
\[ B = \$B.\]
If possible, compute the following.
If an answer does not exist, enter \${BBOLD}DNE\${EBOLD}.
\$BR
\$BR
\( AB = \)
\$BR
\$BR
\( BA = \)
\$BR
\$BR
\{ \$popup->menu \} True or False: For any two matrices
\( A \) and \( B \), both of the products \( AB \) and \( BA \)
are always defined.
END_TEXT
Context()->normalStrings;
```

Main Text: Use `ans_box(rows,cols)` so that students must find the matrix product dimensions themselves, if the matrix product exists.

```install_problem_grader(~~&std_problem_grader);

ANS( (\$A * \$B)->cmp() );
ANS( Compute('DNE')->cmp() );
ANS( \$popup->cmp );

COMMENT('MathObject version.');

ENDDOCUMENT();
```

Answer Evaluation: Use the standard problem grader, which awards full credit only when all answers are correct and zero credit otherwise. Although it would have been better coding practice to construct the matrix product `\$A * \$B` in the Setup section, we did it in the answer section just to show you that it was possible to break "the rules". Standard matrix operations such as `+, -, *` can be used whenever the matrix operation makes sense.