Answer is a matrix
Download file: MatrixAnswer2.pg
DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');
Preamble
These standard macros need to be loaded.Context('Matrix');
$example1 = Matrix([ [ 1, 2, 3 ], [ 4, 5, 6 ] ]);
$example2 = $example1->column(1);
$example3 = $example1->row(1);
$example4 = $example1->copy;
Setup
Switch to the Matrix context by calling
Context('Matrix').
Then construct a 2 by 3 matrix and extract its first column and first row. These matrices will be answers in which the entire matrix will be expected to be entered into a single answer rule.
Finally, construct another 2 by 3 matrix which is a copy of the
first. This matrix will be an answer in which each entry of the matrix
will have its own answer rule. These answer rules are inserted via the
ans_array method. The answer rules are inserted into
PGML with [_]*, and the asterisk tells
PGML to use ans_array instead of
ans_rule.
Note that it is important to use another matrix for
$example4 or a copy as is done here. Do not attempt to use
$example1 for both a single answer rule question and a
answer array question (or even for multiple answers in general).
MathObjects change internal values when used for an answer, and those
changed internal values will cause conflicts if the matrix is used again
for another answer.
BEGIN_PGML
The purpose of this question is to show you the syntax needed to enter matrices
in WeBWorK when there is only one answer box for entering a matrix (which is not
obvious) or when there are multiple answer boxes for entering a matrix (which is
obvious). The examples below should be self explanatory, so you can jump to
them if you want. However, a detailed explanation follows if you want to read
more.
Matrices use square brackets to enclose items in lists. A matrix with one row,
such as a row vector, is a comma separated list enclosed by square brackets. A
matrix with more than one row or a column vector is a comma separated list of
lists, where each list uses square brackets to enclose its items. Your answers
must use square brackets to enclose each row in a matrix or in a column vector.
Also, a pair of square brackets is needed to enclose a matrix with more than one
row (e.g., a [`2 \times 3`] matrix and a [`2 \times 1`] column vector have
multiple rows so they need extra square brackets, but a [`1 \times 3`] row
vector has only one row so it does not need extra square brackets). Your
answers may have spaces and line breaks in them, such as
>> [| [ [1, 2, 3], |] <<
>> [| [4, 5, 6] ] |] <<
+ Enter the matrix [``[$example1]``] as [@ $example1->string @]*:
[_]{$example1}{20}
+ Enter the column vector [``[$example2]``] as [@ $example2->string @]*:
[_]{$example2}{10}
+ Enter the row vector [``[$example3]``] as [@ $example3->string @]*:
[_]{$example3}{10}
+ Entering a matrix using multiple answer blanks is straightforward. Just put
each matrix entry into its own answer blank.
Enter the matrix [``[$example4]``] with one matrix entry per answer box.
[______]*{$example4}{4}
END_PGML
ENDDOCUMENT();
Statement
This is the problem statement in PGML.