def contained_segments_matrix(segments):
    """
    givens a n*n matrix m, n=len(segments), in which m[i,j] means
    segments[i] is contained inside segments[j]
    """
    x1, y1 = segments[:, 0], segments[:, 1]
    x2, y2 = x1 + segments[:, 2], y1 + segments[:, 3]
    n = len(segments)

    x1so, x2so, y1so, y2so = list(map(numpy.argsort, (x1, x2, y1, y2)))
    x1soi, x2soi, y1soi, y2soi = list(map(numpy.argsort, (x1so, x2so, y1so, y2so)))  # inverse transformations
    # let rows be x1 and collumns be x2. this array represents where x1<x2
    o1 = numpy.triu(numpy.ones((n, n)), k=1).astype(bool)
    # let rows be x1 and collumns be x2. this array represents where x1>x2
    o2 = numpy.tril(numpy.ones((n, n)), k=0).astype(bool)
    a_inside_b_x = o2[x1soi][:, x1soi] * o1[x2soi][:, x2soi]  # (x1[a]>x1[b] and x2[a]<x2[b])
    a_inside_b_y = o2[y1soi][:, y1soi] * o1[y2soi][:, y2soi]  # (y1[a]>y1[b] and y2[a]<y2[b])
    a_inside_b = a_inside_b_x * a_inside_b_y
    return a_inside_b