## PREP 2015 Question Authoring - Archived

### Variables in matrices ### Variables in matrices

by Paul Pearson -
Number of replies: 4
Hi everybody,

During the session today, someone asked about how to use algebraic expressions with variables in them in MathObject matrices.  Below is an example of how to include a variable h and a formula h^2+2 as entries in a matrix.

Best regards,

Paul Pearson

#################  BEGIN PG CODE #################

DOCUMENT();

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

TEXT(beginproblem());
$showPartialCorrectAnswers = 1; Context('Matrix')->variables->are(h=>'Real');$y = Matrix([
[non_zero_random(-4,4,1),non_zero_random(-4,4,1)],
[non_zero_random(-4,4,1),non_zero_random(-4,4,1)],
[non_zero_random(-4,4,1),'h']
]);

$f = Formula("h^2+2");$z = Matrix([
[non_zero_random(-4,4,1),non_zero_random(-4,4,1)],
[non_zero_random(-4,4,1),non_zero_random(-4,4,1)],
[non_zero_random(-4,4,1),$f] ]); BEGIN_PGML Let [\vec{y} = [$y] ] and [ \vec{z} = [$z] ]. [ \vec{y} = ] [_____]*{$y}

[ \vec{z} = ] [_____]*{$z} END_PGML COMMENT('MathObject version. Uses PGML.'); ENDDOCUMENT(); ################## END PG CODE #################### In reply to Paul Pearson ### Re: Variables in matrices by Alice McLeod - Is it possible to do matrix operations on the matrices which contain algebraic expressions? I tried to modify the question to include matrix addition, in a naive way, and it didn't seem to work. When I try my version (see below), the answer blank for a + b is a matrix with four entries, as expected, but the answer blank for y + z is a single box. ################# BEGIN PG CODE ################# DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "PGML.pl", "PGcourse.pl", ); TEXT(beginproblem());$showPartialCorrectAnswers = 1;

Context('Matrix')->variables->are(h=>'Real');

$y = Matrix([ [non_zero_random(-4,4,1),non_zero_random(-4,4,1)], [non_zero_random(-4,4,1),non_zero_random(-4,4,1)], [non_zero_random(-4,4,1),'h'] ]);$f = Formula("h^2+2");

$z = Matrix([ [non_zero_random(-4,4,1),non_zero_random(-4,4,1)], [non_zero_random(-4,4,1),non_zero_random(-4,4,1)], [non_zero_random(-4,4,1),$f]
]);

$a = Matrix([ [1,2], [3,4] ]);$b = Matrix([
[4,3],
[2,1]
]);

BEGIN_PGML
Let [\vec{y} = [$y] ], [ \vec{z} = [$z] ], [ \vec{a} = [$a] ], and [ \vec{b} = [$b] ].

[ \vec{y} + \vec{z} =] [_____]*{Compute("$y+$z")}

[ \vec{a} + \vec{b} =] [_____]*{Compute("$a+$b")}

END_PGML

COMMENT('MathObject version. Uses PGML.');

ENDDOCUMENT();

################## END PG CODE #################### ### Re: Variables in matrices

by Gavin LaRose -
Hi Alice,

The problem is that your MathObjects $y and$z are, because they contain formulas, Formula objects that return a Matrix, rather than Matrix objects. Matrices have an ans_array method that generates a multi-blank answer, as happens for your sum $a +$b, but Formulas don't.

The follow-up question is whether there's a good or easy way to get an answer array for a Formula that returns a Matrix. My initial guess is that there isn't an easy way to do this, but that's probably a question for Davide. There are some possible work-arounds, however.

One of these is to use a constant: we might avoid creating a Formula by declaring h to be a named constant, and then having the student answer in terms of that constant. The following changes to your code would accomplish that.

Context('Matrix')->constants->add(h=>3.124356);
Context()->flags->set(
);

$y11 = non_zero_random(-4,4,1);$y12 = non_zero_random(-4,4,1);
$y21 = non_zero_random(-4,4,1);$y22 = non_zero_random(-4,4,1);
$y13 = non_zero_random(-4,4,1);$y = Matrix([[$y11,$y12],[$y21,$y22],[$y13,'h']]);$z11 = non_zero_random(-4,4,1); $z12 = non_zero_random(-4,4,1);$z21 = non_zero_random(-4,4,1); $z22 = non_zero_random(-4,4,1);$z13 = non_zero_random(-4,4,1);
$z = Matrix([[$z11, $z12],[$z21,$z22],[$z13,'h^2 + 2']]);

BEGIN_PGML
Let [Y = \begin{bmatrix} [$y11] & [$y12]\\ [$y21] & [$y22]\\ [$y13] & h\end{bmatrix} ], [ Z = \begin{bmatrix} [$z11] & [$z12]\\ [$z21] & [$z22]\\ [$z13] & h^2 + 2\end{bmatrix} ].

[ Y + Z =] [_____]*{$y+$z}
END_PGML


In this case I've not displayed the matrices Y and Z using their MathObjects, because this reveals the value of the constant h. There may be a way to suppress that, but I'm not sure what it is. The Context flag formatStudentAnswer=>'parsed' means that when the student's answer is displayed it will show h instead of a numerical value. The choice of the numerical value for h is arbitrary, being picked just to avoid anything obvious.

The other change I made was to name matrices with capital letters, which is more consistent with what I've seen elsewhere.

Gavin ### Re: Variables in matrices

by Davide Cervone -
Matrices have an ans_array method that generates a multi-blank answer ... but Formulas don't.

This is the right idea, but not quite accurate. Formula's do allow ans_array(), but only if the Matrix (or vector or point) is given explicitly as a matrix, not as a matrix formula. For example, each of the original matrices $y and $z could have been used with ans_array(), but $y+$z can not.

The main reason for this is that the MathObjects library is not a Computer Algebra System. When you create a matrix $M that contains a formula as an entry, what you get for $M is actually a formula that returns a matrix (not a matrix itself). When you add another matrix to that, you get a formula that performs a sum of two matrices. In particular, it is not a matrix that has entries that are the sums of the two matrices, with some entries being formulas.

To be clearer, here is an example:

Context("Matrix"); $M1 = Matrix([1,2],["x+1","x+2"]);$M2 = Matrix([0,"x"],[3,4]); $M3 =$M1 + $M2; BEGIN_PGML [[$M3]] END_PGML
Both $M1 and $M2 are formulas that return matrices (not matrices themselves). But since they are explicitly given as matrices (not computations on matrices), you can use ans_array() with them.

On the other hand, $M3 is a formula that is the some of two matrices. The display in the PGML section will show two matrices with a plus sign between them. In particular, it will not show the matrix Matrix([1, "2+x"], ["x+4", "x+6"]), because MathObject will not have added the two matrices, it will have created a formula that says to add the two. Since MathObject is not a CAS, it doesn't do that addition itself. Because $M3 is not the explicit matrix that is the sum (but rather the formula that says to take the sum), you can't use ans_array() with it. Why not? Because $M3 does not have a way to provide the student with the correct answer when they ask for correct answers after the due date is passed. The correct answer for $M3 is a formula that is the sum of two matrices, and that is not something the student can answer. So you are not allowed to ask the student for that type of an answer as an array.

Of course, with this simple sum, you might say that MathObjects should do the sum and get the explicit matrix. But this is a slippery slope: remember, MathObjects is not a CAS. Suppose $M were a 3x3 matrix with formulas like x+1 (linear polynomials) in every entry. Then you might ask the student for $M**2. If you say "MathObjets should compute the square and use the resulting matrix as the correct answer", then each entry would look like (x+1)(3x-2)+(1-x)(2x+5)+(4x-1)(x+4). Displaying a 3x3 matrix with these entries would be horrible to look at. And suppose what you really wanted was det(\$M**2). How horrible would that be?

Of course, the products form quadratics, and they could all be added but into a single quadratic, but asking MathObjects to do that for you is asking it to be a CAS system, which it isn't. It would need to be able to expand and simplify expressions, and so on. And this was just with simple linear polynomials. What if your matrix included sines and cosines or other complicated stuff.

This is beyond the scope of what MathObjects does, or was intended to do.

Since the answer array asks students to enter the matrix in a very specific format (each entry separately), you need to provide your answer in the same format, so that a correct answer can be displayed for the student. If it is not in that format, you can't ask the student to enter it that way.

That is why the answer array approach is not available for the sum of two matrix formulas.

(Edited by Michael Gage - original submission Wednesday, 17 June 2015, 11:21 AM) 