I'm looking to calculate the trace of an inverse-matrix product efficiently, i.e. Tr(A^-1 B). I use the inverse of A in many other places, so I have access to either the Cholesky decomposition or the explicit inverse.
The naive thing to do would be one of the following two:
np.trace(np.dot(invA, B))
np.trace(linalg.cho_solve(choA, B))
However, this seems wasteful, as it computes the full matrix A^-1 B at O(N^3), before only using its diagonal to calculate the trace.
Given the explicit inverse, the O(N^2) solution would be to do:
np.sum(invA.T * B)
Though this requires an explicit inverse, which is undesirable.
I think the ideal way to do it, would be to only calculate the diagonal elements of A^-1 B given the Cholesky decomposition and then simply sum. Is this possible using scipy? Or is there another way of calculating Tr(invA*B) in a numerically stable way, given the Cholesky decomposition?