0

for example, when solving a linear system in scipy, if the A matrix is ill conditioned when solving Ax = b, the following warning is raised :

LinAlgWarning: Ill-conditioned matrix (rcond=7.10124e-19): result may not be accurate. U = scipy.linalg.solve(self.K, self.F)

I thought about computing the rcond value from the A matrix but it would be less efficient than letting it be done by scipy and in most cases the A matrix is well conditioned.

I would like to do something like :

U = scipy.linalg.solve(K, F)

if LinalgWarning:
    K += np.identity(K.shape[0])

U = scipy.linalg.solve(K, F)

I tried to do this :

try:
    U = scipy.linalg.solve(A, b)
except LinAlgWarning:
    A += np.identity(A.shape[0])
    U = scipy.linalg.solve(A, b)

But it did not worked (the warning still showed and the except statement was not run)

olivecha
  • 21
  • 4
  • Try the `warnings.filterwarnings` as discussed here https://stackoverflow.com/questions/63979540/python-how-to-filter-specific-warning, but change the action to `error` – hpaulj Jul 01 '21 at 03:37

0 Answers0