Click to enlarge

This PG code shows how to collect matrix answers using one textbox for the entire matrix a student enters.

PG problem file Explanation

Problem tagging:

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
);

TEXT(beginproblem());


Initialization:

Context('Matrix');

$example1 = Matrix([[1,2,3],[4,5,6]]);$example2 = $example1->column(1);$example3 = $example1->row(1);  Setup 1: We construct a 2 by 3 matrix and extract its first column and first row. Context()->texStrings; BEGIN_TEXT 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.$BR
$BR 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$PAR
$BCENTER [ [1, 2, 3],$BR [4, 5, 6] ]
$ECENTER$BR
$BR Enter the matrix $$example1$$ as \{$example1->string \}
$BR \{ ans_box(3,30) \}$BR
$BR Enter the column vector $$example2$$ as \{$example2->string \}
$BR \{ ans_box(3,30) \}$BR
$BR Enter the row vector $$example3$$ as \{$example3->string \}
$BR \{ ans_box(3,30) \} END_TEXT Context()->normalStrings;  Main Text 1: Use $matrix->string to get the string representation of the matrix (i.e., the string answer the student needs to type in) and ans_box(rows,cols) to create a re-sizable text box for the student to enter their answer.

$showPartialCorrectAnswers = 1; ANS($example1->cmp() );
ANS( $example2->cmp() ); ANS($example3->cmp() );


Context('Matrix');

$example1 = Matrix([[1,2,3],[4,5,6]]);  Setup 2: Reset the context by calling Context('Matrix'); because the matrix answer checker gets confused when the ans_box and ans_array methods are co-mingled. Context()->texStrings; BEGIN_TEXT$BR
$BR Entering a matrix using multiple answer blanks is straightforward -- just put each matrix entry into its own answer blank.$BR
Enter the matrix $$example1$$ with one matrix entry per answer box.
$BR \{$example1->ans_array \}
END_TEXT
Context()->normalStrings;


Main Text 2:

ANS( \$example1->cmp() );

COMMENT('MathObject version.');

ENDDOCUMENT();