def distance_diagonal_law(matrix, positions=None):
n = min(matrix.shape)<EOL>if positions is None:<EOL><INDENT>return np.array([np.average(np.diagonal(matrix, j)) for j in range(n)])<EOL><DEDENT>else:<EOL><INDENT>contigs = positions_to_contigs(positions)<EOL><DEDENT>def is_intra(i, j):<EOL><INDENT>return contigs[i] == contigs[j]<EOL><DEDENT>max_intra_distance = max((len(contigs == u) for u in set(contigs)))<EOL>intra_contacts = []<EOL>inter_contacts = [np.average(np.diagonal(matrix, j))<EOL>for j in range(max_intra_distance, n)]<EOL>for j in range(max_intra_distance):<EOL><INDENT>D = np.diagonal(matrix, j)<EOL>for i in range(len(D)):<EOL><INDENT>diagonal_intra = []<EOL>if is_intra(i, j):<EOL><INDENT>diagonal_intra.append(D[i])<EOL>
Compute a distance law trend using the contact averages of equal distances.
    Specific positions can be supplied if needed.