RFC:linalg: nD-array accepting functions to return partially completed work instead of discarding all and throwing an exception · Issue #22476 · scipy/scipy (original) (raw)

This is something @ev-br and I decided to go with for setting precedence of a particular linalg behavior. It can be extended to other modules but for now let's stick to the specific linalg context.

While rewriting things in linalg module, there is a design choice that I am still not 100% comfortable yet which is, what to do when dealing with a batch input (with ndim > 2), a particular slice fails to produce valid output but the rest is OK.

Example; currently solve, sqrtm, or logm (and some more) raise LinAlgError when a 2D array does not satisfy some requirements (singular matrix or negative eigenvalue or whatever). But imagine we have supplied an array of (10, 4, 4)-shape and only the seventh slice is problematic. We already had six successful results and possibly will get three more after the seventh.

  1. Option 1: Not our problem, fix it and retry.
    • Bad 2D input: Raise LinAlgError (current behavior)
    • Bad 3D input: It doesn't where it happens, if one slice fails, clean up and raise LinAlgError for the whole.
  2. Option 2: Compute as much as you can, mention the screw-ups, (possibly NaN-fill) and return the finished work.
    • Bad 2D input: Raise LinAlgError (current behavior)
    • Bad 3D input: Raise LinAlgWarning that some slice went bad and return the work. No exceptions raised.
  3. Option 3: Compute as much as you can, mention the screw-ups, (possibly NaN-fill) and return the finished work.
    • Bad 2D input: Raise only LinAlgWarning the returned array involves NaN's, Inf's. No exceptions raised.
    • Bad 3D input: Raise LinAlgWarning that some slice went bad and return the work. No exceptions raised.

One thing that Python does not allow is to raise Exception and return a result. Hence it is an exclusive choice. Option 1 is our current state. Not by design but we just batched things and still everything is treated as separate 2D inputs however an error cleans up all work and raises the exception.

Backwards-compatibility wise, if we go with option 1, then 2D arrays become our special case and we have to do something about the current exception behavior. We can also opt for, if you have an array with ndim>2, then exceptions become warnings. But if 2D then you get an exception. Does this sound inconsistent?

Which option should we take? All feedback welcome.