def newshape(s, lowdim=0):
    """
    Given a shape s, calculate the shape of a new array with the
    lowest dimension replaced.  If lowdim is zero the lowest dimension
    is removed, otherwise it is replaced.
    """
    sl = list(s)
    if lowdim == 0:
        sl.pop()
    else:
        sl[-1] = lowdim
    return tuple(sl)