Repository: scipy/scipy
File: scipy/linalg/src/_linalg_inv.hh (C++)

Code before:
        // detect the structure if not given
        slice_structure = structure;
        if (slice_structure == St::NONE) {
            // Get the bandwidth of the slice
            bandwidth(data, n, n, &lower_band, &upper_band);

            if ((upper_band == 0) && (lower_band == 0)) {
                slice_structure = St::DIAGONAL;
            }
            else if(lower_band == 0) {
                slice_structure = St::UPPER_TRIANGULAR;
                uplo = 'U';
            } else if (upper_band == 0) {
                slice_structure = St::LOWER_TRIANGULAR;
                uplo = 'L';
            } else {
                // Check if symmetric/hermitian
                std::tie(is_symm, is_herm) = is_sym_or_herm(data, n);

                if constexpr (!type_traits<T>::is_complex) {
                    // Real: is_symm and is_herm are always equal
                    if (is_symm) {
                        // try Cholesky first, fall back to sytrf if it fails
                        slice_structure = St::POS_DEF;
                    }
                    else {
                        slice_structure = St::GENERAL;
                    }
                }
                else {
                    // Complex
                    if (!is_symm && !is_herm) {
                        slice_structure = St::GENERAL;
                    }
                    else if (is_herm) {
                        // Hermitian (may also be symmetric if entries are real)
                        // try Cholesky first, fall back to hetrf if it fails
                        slice_structure = St::POS_DEF;
                    }
                    else {
                        // is_symm && !is_herm: complex symmetric, not hermitian
                        slice_structure = St::SYM;
                    }
                }
            }
        }

        init_status(slice_status, idx, slice_structure);

        switch(slice_structure) {
            case St::DIAGONAL:

Review comment (refactor):
Perhaps it might be worth adding a brief comment on why it is only in that case? 

It is already explicitly mentioned in the `_basic.py` file that `overwrite_a` is only enabled when the input array is `F-contiguous`, but the rest of the code is somewhat oblivious to the ordering of the input array (through the strides, which abstract this away), so mentioning why it is fine to do it like that here might make things a little easier to follow. (Just my two cents' worth, feel free to disregard entirely.)

Code after:
    if (structure == St::POS_DEF) {
        posdef_fallback = false;
    }
    else if (structure == St::SYM) {
        is_symm = true;
    }
    else if (structure == St::HER) {
        is_herm = true;
    }
    if (structure == St::LOWER_TRIANGULAR) {
        uplo = 'L';
    }
    else if (structure == St::UPPER_TRIANGULAR) {
        uplo = 'U';
    }

    /*
     * Main loop to traverse the slices.
     */
    for (npy_intp idx = 0; idx < outer_size; idx++) {
        T *slice_ptr = compute_slice_ptr(idx, Am_data, ndim, shape, strides);

        if (!overwrite_a) {
            copy_slice(scratch, slice_ptr, n, n, strides[ndim-2], strides[ndim-1]); // XXX: make it in one go
            swap_cf(scratch, data, n, n, n);
        }

        // detect the structure if not given
        slice_structure = structure;
        if (slice_structure == St::NONE) {
            // Get the bandwidth of the slice
            bandwidth(data, n, n, &lower_band, &upper_band);

            if ((upper_band == 0) && (lower_band == 0)) {
                slice_structure = St::DIAGONAL;
            }
            else if(lower_band == 0) {
                slice_structure = St::UPPER_TRIANGULAR;
                uplo = 'U';
            } else if (upper_band == 0) {
                slice_structure = St::LOWER_TRIANGULAR;
                uplo = 'L';
            } else {
                // Check if symmetric/hermitian
                std::tie(is_symm, is_herm) = is_sym_or_herm(data, n);

                if constexpr (!type_traits<T>::is_complex) {
                    // Real: is_symm and is_herm are always equal
                    if (is_symm) {
                        // try Cholesky first, fall back to sytrf if it fails
                        slice_structure = St::POS_DEF;