def lorentz_ip(u, v=None):
    """
    Compute Lorentz inner product of two vectors
    
    For vectors `u` and `v`, the
    Lorentz inner product for 3-dimensional case is defined as
    
        u[0]*v[0] + u[1]*v[1] + u[2]*v[2] - u[3]*v[3]
        
    Or, for 2-dimensional case as
        
        u[0]*v[0] + u[1]*v[1] - u[2]*v[2]
    
    Args
        u: vector with shape either (3,) or (4,) 
        v: vector with same shape as x1; if None (default), sets v = u

    Returns
        float: value of Lorentz IP """
    if v is None:
        v = u

    if len(u) == 3 and len(v) == 3:
        c = [1, 1, -1]
        return sum([u[i] * v[i] * c[i] for i in range(len(u))])
    elif len(u) == 4 and len(v) == 4:
        c = [1, 1, 1, -1]
        return sum([u[i] * v[i] * c[i] for i in range(len(u))])

    return ValueError(f"length of x should be 3 or 4, was{len(u)}")