Computing the Kronecker product over a tensor chain I'm working on some Python code and have a few functions which do similar things, and the only way I've found of writing them is quite ugly and not very clear.
In the example below, the goal is to compute the Kronecker product over a tensor chain of length M, in which the mth tensor is R and every other tensor is J.
Is there any nice way to rewrite this?
def make_rotate_target(m, M, J, R): 
    out = J
    if M == 1:
        return R
    else:
        for i in range(M):
            if i == 0:
                out = J
            else:
                if i + 1 == m:
                    out = np.kron(out, R)
                else:
                    out = np.kron(out, J)
    return out
Assuming M is non-negative you can make it clear when J and R are returned and remove some of the nested if-else's.
Since if i+1 ==m only evaluates as true if m &lt;= M you could check that and use different for loops if it's true/false to make things slightly faster but I think that would decrease readability for not much gain
def make_rotate_target(m, M, J, R):
    if M == 0:
        return J

    if M == 1:
        return R

    out = J
    for i in range(1, M):
        if i + 1 == m:
            out = np.kron(out, R)
        else:
            out = np.kron(out, J)

    return out