checking triangle inequality in a massive numpy matrix

  • Last Update :
  • Techknowledgy :

You can certainly vectorise the innermost loop easily enough with (untested):

for i in range(N):
   for j in range(i):
   assert all(D[i, j] <= D[i,: ] + D[: , j])

For double vectorisation you can loop through k (also untested):

for k in range(N):
   row = D[k,: ].reshape(1, N)
col = D[: , k].reshape(N, 1)
assert(D <= row + col).all()

Suggestion : 2

I have a symmetric NumPy matrix D of anycodings_distance non-negative floating point numbers. A anycodings_distance number in the ith row and jth column anycodings_distance represents the distance between objects i anycodings_distance and j, whatever they are. The matrix is anycodings_distance large (~10,000 rows/columns). I would like anycodings_distance to check if all the distances in the matrix anycodings_distance obey the triangle inequality, that is: anycodings_distance D[i,j]<=D[i,k]+D[k,j] for all i, j, k.,You can certainly vectorise the anycodings_python innermost loop easily enough with anycodings_python (untested):,The problem can be solved, quite anycodings_distance inefficiently, by using a triple-nested anycodings_distance loop. But is there a faster, vectorized anycodings_distance solution?,(row + col generates a square matrix the anycodings_python same size as D)

You can certainly vectorise the anycodings_python innermost loop easily enough with anycodings_python (untested):

for i in range(N):
   for j in range(i):
   assert all(D[i, j] <= D[i,: ] + D[: , j])

For double vectorisation you can loop anycodings_python through k (also untested):

for k in range(N):
   row = D[k,: ].reshape(1, N)
col = D[: , k].reshape(N, 1)
assert(D <= row + col).all()

Suggestion : 3

Last Updated : 03 Mar, 2019

The triangle inequality states that for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side. In other words, a triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met.

a + b > c
a + c > b
b + c > a

[(4, 5, 6)]

[(4, 5, 6)]

[4, 5, 6]

Suggestion : 4

If you'd like to report this error to the developers, please send an email to info@openreview.net.

The requested page could not be found.

Please check that the URL is spelled correctly and
try again.