## WeBWorK Main Forum

### inverse of the product of two matrices

by Nandor Sieben -
Number of replies: 4
I have some trouble with taking the inverse matrix of a product. The inverse is not calculated correctly. Could you please tell me what the problem is.

The full problem is in the OPL:
Library/NAU/setLinearAlgebra/similar3.pg

TEXT(beginproblem());
Context("Matrix");
{
$P=Matrix([[random(-3,3,1),random(-3,3,1)],[random(-3,3,1),random(-3,3,1)]]); redo if (abs($P->det) != 1);
$Pi=$P->inverse;
}
{
$Q=Matrix([[random(-3,3,1),random(-3,3,1)],[random(-3,3,1),random(-3,3,1)]]); redo if (abs($Q->det) != 1);
}

$I=Matrix([[1,0],[0,1]]); {$B=Matrix([[random(-3,3,1),random(-3,3,1)],[random(-3,3,1),random(-3,3,1)]]);
$A=$Pi*$B*$P;
redo if ($B==$A);
}

$R=$Q*$P;$Rin = $R->inverse; # This does not seem to work. Please tell me why?$Ri = $P->inverse*$Q->inverse; # This works.
...

### Re: inverse of the product of two matrices

by Alexander Basyrov -
Hi Nandor,

In the context of your example, would

$R = Matrix($Q*$P);$Rin = $R->inverse; work as expected? -- Alex Basyrov In reply to Alexander Basyrov ### Re: inverse of the product of two matrices by Nandor Sieben - Yes, this change fixed it. Thank you very much. In reply to Nandor Sieben ### Re: inverse of the product of two matrices by Davide Cervone - Alexander beat me to it. The problem is that when you do certain operations (like inverses) certain information is cached as part of the object (to make it faster to do other actions later). When objects are combined (like in the matrix multiplication), their properties are copied to the new object, so that things like test points for functions, or limits, or tolerances, or other options that you can set are maintained. The cache values should not be copied, but are, and so when the inverse is taken later, the cached values are incorrect (they were for the$P matrix rather than the matrix product), and that caused the inverse to be incorrect.

Alexander's suggestion causes a new Matrix object to be created from the data of the old one, and the cached values are lost (along with any other settings that might have been made).

In any case, this is a bug that needs to be fixed. There is a list of properties NOT to be copied, and I will have to add the cache values to that.