0
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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 ? $\endgroup$
    – user16034
    Jul 29, 2022 at 11:50
  • $\begingroup$ 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. $\endgroup$
    – user16034
    Jul 29, 2022 at 11:52

1 Answer 1

1
$\begingroup$

Yes, your optimization is worthwhile.

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.

In practice, testing two matrices for inequality is far faster than inverting and multiplying two matrices, so in practice the condition will surely hold, and your optimization will be worthwhile.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.