# Checking for equality before calculating product of a matrix with inverse of another matrix

I have an operation that is performed many times and is thus performance sensitive where I need to calculate product of a matrix and inverse of a matrix as below. Both matrices are 4x4 and consist of floats.

M3 = Inverse(M1)*M2

Now in this specific scenario M1 and M2 very often equal each other. Let's say 75% of the time the two matrices are equal. I'm wondering if it would be cost-effective to then compare the two matrices for equality (equal within a given epsilon because of floating point inaccuracy), and assign M3 identity matrix if the two are equal?

Calculating an inverse is very expensive so would it make sense to do matrix comparisons which only mean 16 comparisons and 16 subtractions?

• What is the reason that most of the time the matrices are equal (this is a pretty unusual information). Can't you know this ahead of time ?
– user16034
Jul 29, 2022 at 11:50
• Best practice is not to compute the inverse and multiply, but to compute the LU decomposition (whether pivoting is required or not is debatable), then perform multiple system resolutions.
– user16034
Jul 29, 2022 at 11:52

If the cost of performing the inversion and multiplication is $$c_\times$$ and the cost of comparing the two matrices is $$c_=$$, then the expected cost of your optimization is $$c_= + 0.25 c_\times$$, whereas the cost of always multiplying is $$c_\times$$. We can then use simple algebra to identify the condition under which your optimization is better. In particular, your optimization is better if $$c_= + 0.25 c_\times \le c_\times$$, which happens if $$c_= \le 0.75 c_\times$$. So, if you benchmark the cost of multiplication and the cost of checking equality, you will be able to determine whether this condition holds.