Repository: sympy/sympy
File: sympy/polys/matrices/sdm.py (Python)

Code before:
            C[i] = Ci

    # Find all infinities in B
    for k, Bk in B.items():
        for j, Bkj in Bk.items():
            if zero * Bkj != zero:
                for i in range(m):
                    Aik = A.get(i, {}).get(k, zero)
                    # If Aik is not zero then this was handled above
                    if Aik == zero:
                        Ci = C.get(i, {})
                        Cij = Ci.get(j, zero) + Aik * Bkj
                        if Cij != zero:
                            Ci[j] = Cij
                            C[i] = Ci
                        else:
                            Ci.pop(j, None)
                            if Ci:
                                C[i] = Ci
                            else:
                                C.pop(i, None)

    return C


def sdm_scalar_mul(A, b, op, domain):
    """
    Handles special cases like 0 * oo -> nan by creating a dense result
    when necessary. For all other cases, it uses the fast sparse approach.
    """

    zero = domain.zero
    if domain.is_EXRAW and op(zero, b) != zero:
        Csdm = sdm_scalar_mul_exraw(A, b, op, domain)
    else:
        Csdm = unop_dict(A, lambda aij: op(aij, b))
    return Csdm


def sdm_scalar_mul_exraw(A, b, op, domain):
    zero = domain.zero
    zero_prod = op(zero, b)
    m, n = A.shape
    Csdm = {i: dict.fromkeys(range(n), zero_prod) for i in range(m)}
    for i, Ai in A.items():
        Ci = Csdm[i]
        for j, Aij in Ai.items():
            Cij = op(Aij, b)
            if Cij == zero:
                del Ci[j]
            else:

Review comment (suggestion):
It would be better to be consistent with the surrounding functions and call the domain parameter `K`.

Code after:
            C[i] = Ci

    # Find all infinities in B
    for k, Bk in B.items():
        for j, Bkj in Bk.items():
            if zero * Bkj != zero:
                for i in range(m):
                    Aik = A.get(i, {}).get(k, zero)
                    # If Aik is not zero then this was handled above
                    if Aik == zero:
                        Ci = C.get(i, {})
                        Cij = Ci.get(j, zero) + Aik * Bkj
                        if Cij != zero:
                            Ci[j] = Cij
                            C[i] = Ci
                        else:
                            Ci.pop(j, None)
                            if Ci:
                                C[i] = Ci
                            else:
                                C.pop(i, None)

    return C


def sdm_scalar_mul(A, b, op, K):
    """
    Handles special cases like 0 * oo -> nan by creating a dense result
    when necessary. For all other cases, it uses the fast sparse approach.
    """

    zero = K.zero
    if K.is_EXRAW and op(zero, b) != zero:
        Csdm = sdm_scalar_mul_exraw(A, b, op, K)
    else:
        Csdm = unop_dict(A, lambda aij: op(aij, b))
    return Csdm


def sdm_scalar_mul_exraw(A, b, op, K):
    zero = K.zero
    zero_prod = op(zero, b)
    m, n = A.shape
    Csdm = {i: dict.fromkeys(range(n), zero_prod) for i in range(m)}
    for i, Ai in A.items():
        Ci = Csdm[i]
        for j, Aij in Ai.items():
            Cij = op(Aij, b)
            if Cij == zero:
                del Ci[j]
            else: