#26317·scipy

MAINT: linalg: unify C++ wrappers and low-level batched implementations

Author: BasilLiekensCreated Sep 30, 2026Updated Oct 1, 2026
Labelsscipy.linalgmaintenance

Low-level batching of select linalg functions is currently being done in C/C++ for performance reasons. In order to access the required BLAS/LAPACK functions these are wrapped using BLAS_FUNC(...)(...), e.g. here.

However, #25611 and #25992 replaced the old f2py BLAS and LAPACK wrappers with a C++ implementation using the same pattern to access said BLAS/LAPACK functions. Since this covers all the BLAS and LAPACK functions that SciPy offers, this means that the functions that are being wrapped for the low-level batching are essentially wrapped twice in different namespaces.

In an ideal case there would be only 1 set of wrappers, e.g. have the low-level batching functions tap into the functions being accessed for the wrappers, which also removes the need to manually rewrap these functions in the aforementioned file. The call_xyz macros are most probably still required though.

There is, however, one slightly larger problem in doing so which is that the lower-level batching functions make use of npy_complex64/128 whereas the C++ wrappers just wrap std::complex<float/double>, so that would also have to be unified. Other than that it seems as simple as just including lapack_calls.hpp in the low-level batching functions, potentially fiddling around a bit with the fact that the C++ wrappers do not immediately make use of references whereas the low-level functions do.

Is this an approach that seems worth pursuing, and if so, which approach should be taken to deal with the std::complex<*> vs npy_complex* issue? My 2 cents would be that it is probably a better idea to rely on the standard library, although that would also require investigating what changes to some of the auxiliaries in _npymath.hpp are needed.