def calc_S(Psi0, Psi1, Psi2, Psi3, dz):
    """Calculates S, a quantity used to calculate the effective thickness of an
    intersubband transition of a quantum well.
    Psi0 - Psi3 are arrays describing the wavefunctions.
    dz is the step-size (metres) for the arrays (assumed to be constant).
    """
    i1 = 0.0
    i2 = 0.0
    i3 = 0.0
    for p0, p1, p2, p3 in zip(Psi0, Psi1, Psi2, Psi3):
        delta = p0 * p1
        i1 += delta
        i2 += i1
        i3 += p2 * p3 * i2
    i3 *= dz ** 3
    return -i3
