def cross(u, v):
    """
    Return the cross product of two 2 or 3 dimensional vectors.
    """
    uDim = len(u)
    vDim = len(v)
    uxv = []
    if uDim == vDim == 2:
        uxv = [u[0] * v[1] - u[1] * v[0]]
    elif uDim == vDim == 3:
        for i in range(uDim):
            uxv = [u[1] * v[2] - u[2] * v[1], -(u[0] * v[2] - u[2] * v[0]),
                u[0] * v[1] - u[1] * v[0]]
    else:
        raise IndexError('Vector has invalid dimensions')
    return uxv
