$B2
matrix does not have the variable k. Moreover, the context that is associated with the $kVar
(and so $b3
) variable is actually the original Numeric context, so your are mixing contexts in a way that will cause problems.
It appears that you are resetting and adjusting the contexts in order to set the opening and closing delimiters for the matrices, but you are working much harder than necessary for that. You could set the Matrix content once, and not modify its lists or other values (other than adding the variable k), and modify the delimiters on the matrices themselves, as in:
$A1 = Matrix([[$a11,$a12,$a13],[$a21,$a22,$a23],[$a31,$a32,$a33]])->with(close=>"|"); $B1 = Matrix([[$b1], [$b2], [$b3number]])->with(open=>".");(we use an open delimiter of
.
so that in TeX it is `\left. ... \right]`, which produces no left delimiter).
Of course, this requires you to do this for each matrix produced, but that is probably easier than the complicated and opaque resetting of the Matrix context for each set.
One of the consequences of your approach is that the A and B matrices will each have their own line in the results table when a student enters an answer. You can improve that by using the MultiAnswer
object to bind the two matrices together in one line of the results table. In fact, you can automate most of the process using something like the following:
loadMacros( "PGstandard.pl", "parserMultiAnswer.pl", "PGcourse.pl" ); TEXT(beginproblem()); sub AugmentedMatrix { my ($A, $B) = @_; my ($a, $b, $c) = @$B; $A = Matrix($A)->with(close => '|'); $B = Matrix([[$a], [$b], [$c]])->with(open => '.'); MultiAnswer($A, $B)->with( singleResult => 1, separator => "", tex_separator => "", checker => sub { my ($c, $s, $self, $ans) = @_; my ($cA, $cB) = @$c; my ($sA, $sB) = @$s; $self->{ans}[0]{preview_latex_string} =~ s/~~]$/|/; $self->{ans}[1]{preview_latex_string} =~ s/~~[/./; return $cA == $sA && $cB == $sB; } ); } $showPartialCorrectAnswers = 1; Context("Matrix"); Context()->variables->add(k => 'Real', x => 'Real', y => 'Real'); Context()->noreduce('(-x)-y'); Context()->noreduce('(-x)+y'); $k = Compute("k"); $a11 = 1; $a12 = random(-4,-1,1); $a13 = random(- $a12 +2, 10,1); $d = random(-4,4,1); $e = random(-4,4,1); $g = 2*random(-3,3,1)+1; $h = non_zero_random(-3,3,1); $i = non_zero_random(-20,20,1); $a21 = non_zero_random(-5,5,2); $a22 = $a21*$a12+ $g; $a23 = $a21*$a13+$g*$d; $a31 = $a21+$h; $a32 = $a21*$a12+$g+$h*$a12; #$a33 = ($a21+$h)*$a13 + $g*$d; $a33 = $k; $b1 = random(-4,4,1); $b2 = $a21*$c+ $g*$e; $b3 = $k; $M1 = AugmentedMatrix( [[$a11, $a12, $a13], [$a21, $a22, $a23], [$a31, $a32, $a33]], [$b1, $b2, $b3] ); $Eq1 = Formula("$a11 x + $a12 y + $a13 z")->reduce; $Eq2 = Formula("$a21 x + $a22 y + $a23 z")->reduce; $Eq3 = Formula("$a31 x + $a32 y + $a33 z")->reduce; ########################################### Context()->texStrings; BEGIN_TEXT From the system of equations \[\begin{aligned} $Eq1 &= $b1\\ $Eq2 &= $b2\\ $Eq3 &= $b3 \end{aligned}\] we have $BR $BR \( \quad \) \{ $M1->ans_array(2) \}\{ $M1->ans_array(2) \} END_TEXT Context()->normalStrings; ANS($M1->cmp());Here, we define
AugmentedMatrix()
to encapsulate most of the work. It creates the MultiAnswer
object by taking your A matrix and B vector and creating separate Matrix objects for them, with the proper delimiters. Its answer checker checks that both matrices match the correct ones, and also fixes the delimiters in the output for the student-entered matrices (since these get the usual delimiters by default). It also marks the MultiAnswer
matrix so that it is a single result in the table, and formats the results so that the two matrices are adjacent (so they look like the augmented matrix).
Once the augmented matrix is created, you can generate its input arrays using the $M1
variable just as you did the A
and B
variables (though I recommend not putting a space between them for better formatting). The output in the results table will be formatted like an augmented matrix except for the situation where the student leaves the A
part entirely blank. One could work harder to make even that case display properly, but that would take creating your own MathObject class, and is beyond the scope of this answer.
You also seem to be working too hard to format your system of equations, which doesn't require the use of BeginTable/EndTable
. Indeed, using that will make your question less accessible to students using screen readers, for example. So my example uses the aligned
environment to format the math.
Using PGML would make the problem even easier to write:
loadMacros( "PGstandard.pl", "parserMultiAnswer.pl", "PGML.pl", "PGcourse.pl", ); sub AugmentedMatrix { my ($A, $B) = @_; my ($a, $b, $c) = @$B; $A = Matrix($A)->with(close => '|'); $B = Matrix([[$a], [$b], [$c]])->with(open => '.'); MultiAnswer($A, $B)->with( singleResult => 1, separator => "", tex_separator => "", checker => sub { my ($c, $s, $self, $ans) = @_; my ($cA, $cB) = @$c; my ($sA, $sB) = @$s; $self->{ans}[0]{preview_latex_string} =~ s/~~]$/|/; $self->{ans}[1]{preview_latex_string} =~ s/~~[/./; return $cA == $sA && $cB == $sB; } ); } $showPartialCorrectAnswers = 1; Context("Matrix"); Context()->variables->add(k => 'Real', x => 'Real', y => 'Real'); Context()->noreduce('(-x)-y'); Context()->noreduce('(-x)+y'); $k = Compute("k"); $a11 = 1; $a12 = random(-4,-1,1); $a13 = random(- $a12 +2, 10,1); $d = random(-4,4,1); $e = random(-4,4,1); $g = 2*random(-3,3,1)+1; $h = non_zero_random(-3,3,1); $i = non_zero_random(-20,20,1); $a21 = non_zero_random(-5,5,2); $a22 = $a21*$a12+ $g; $a23 = $a21*$a13+$g*$d; $a31 = $a21+$h; $a32 = $a21*$a12+$g+$h*$a12; #$a33 = ($a21+$h)*$a13 + $g*$d; $a33 = $k; $b1 = random(-4,4,1); $b2 = $a21*$c+ $g*$e; $b3 = $k; $M1 = AugmentedMatrix( [[$a11, $a12, $a13], [$a21, $a22, $a23], [$a31, $a32, $a33]], [$b1, $b2, $b3] ); $Eq1 = Formula("$a11 x + $a12 y + $a13 z")->reduce; $Eq2 = Formula("$a21 x + $a22 y + $a23 z")->reduce; $Eq3 = Formula("$a31 x + $a32 y + $a33 z")->reduce; ########################################### BEGIN_PGML From the system of equations >> [``\begin{aligned} [$Eq1] &= [$b1]\\ [$Eq2] &= [$b2]\\ [$Eq3] &= [$b3] \end{aligned}``] << we have the augmented matrix [__]*{$M1}[__]*{$M1} END_PGMLFinally, note that you are are including lots of macro files that you don't use. This just causes unnecessary load on the server as it has to load and compile those file for no reason every time one of your students views or submits an answer to your problem. So it is best to use only the macro files that you actually need.