# Numerical stability in the product of many matrices

I have to calculate in numpy the matrix-product of many matrices (~400). Are there common practices to increase numerical stability?

If this is relevant, the matrices are $$300\times 300$$ orthogonal projection matrices.

• Is it possible to elaborate a bit more about what do you mean "to increase numerical stability"? – Alone Programmer Dec 10 '19 at 17:44
• The latter option. – user1767774 Dec 11 '19 at 7:27
• In that case, your matrices aren't orthogonal matrices and the answer by @whpowell96 isn't relevant. – Brian Borchers Dec 11 '19 at 13:58
• You're starting with a 300 dimensional space, and each projection would reduce this dimension by at least one. Unless there's significant repition in this sequence of projections, you'd be left with only the 0 vector after 400 projections. Could you explain where this problem is coming from? – Brian Borchers Dec 11 '19 at 16:08
• Yes, in practice I work with matrices of different dimensions, and the number of products changes accordingly; 300x300 matrices are multiplied about 100 times. The problem comes from a research in ML, and each projection matrix in the product should remove a different kind of information from the input vectors. – user1767774 Dec 11 '19 at 19:29

Orthogonal matrices are about as well-conditioned as you can get, but numerical errors still occur. One common error is loss of orthogonality. A fix for this could be to re-orthogonalize your columns after some number of multiplications. You can do this by just taking the QR decomposition of your matrix after some number of products and taking the orthogonal part. Since your matrices are square, this will cost $$O(n^3)$$, so comparable to the matrix multiplications.