Advertisement
makispaiktis

Distances between spots in N-D domain

Jan 4th, 2021 (edited)
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. # In this task, I have a 10xN matrix. That symbolizes a vector of vectors. We have 10 vectors (row = vector with coordinates).
  2. # Each inner vector has N coordinates (we are in the N-d domain)
  3.  
  4. from math import sqrt
  5. from random import randrange
  6. from timeit import default_timer as timer
  7.  
  8. # Function 1 - Initialization of the vectors
  9. def initializeMatrix(numOfVectors, N, LIMITinclusive):
  10.     if numOfVectors <= 0 or N <= 0 or numOfVectors != int(numOfVectors) or N != int(N):
  11.         print("Error while using the function '" + initializeMatrix.__name__ + "'")
  12.     else:
  13.         matrix = list()
  14.         for i in range(numOfVectors):
  15.             vector = list()
  16.             for j in range(N):
  17.                 vector.append(randrange(0, LIMITinclusive + 1))
  18.             matrix.append(vector)
  19.         return matrix
  20.  
  21. # Function 2 - Find the distances of every vector with the other vectors
  22. def distance(v1, v2):
  23.     if len(v1) != len(v2):
  24.         print("Error while using the function '" + distance.__name__ + "'")
  25.     else:
  26.         N = len(v1)
  27.         normSquared = 0
  28.         for i in range(N):
  29.             normSquared += (v1[i] - v2[i]) ** 2
  30.         norm = sqrt(normSquared)
  31.         return norm
  32.  
  33.  
  34. # Function 3 - Create a numOfVector x numOfVector matrix with all the distances
  35. def distances(matrix):
  36.     N = len(matrix)
  37.     distances = [[0 for _ in range(N)] for __ in range(N)]
  38.     for i in range(N):
  39.         for j in range(N):
  40.             distances[i][j] = distance(matrix[i], matrix[j])
  41.     # The variables "distances" is a NxN matrix <----> list in list
  42.     return distances
  43.  
  44.  
  45. # Function 4 - Pretty Distances
  46. def prettyDistances(matrix):
  47.     DISTANCES = distances(matrix)
  48.     for i in range(len(DISTANCES)):
  49.         print(DISTANCES[i])
  50.  
  51.  
  52. # Function 5 - Return a matrix, where the 1st element is the closest distance of the 1st spot with the other spots,
  53. # the 2nd element is the closest distance of the 2nd spot with the other spots
  54. def minimumDistances(matrix):
  55.     N = len(matrix)
  56.     DISTANCES = distances(matrix)
  57.     mins = list()
  58.     for i in range(N):
  59.         # DISTANCES[i] = a vector with all the distances, containing the zero distance (0)
  60.         # that has been calculated between an object and itself
  61.         d = DISTANCES[i]
  62.         d.remove(0)
  63.         mins.append(min(d))
  64.     return mins
  65.  
  66.  
  67. # MAIN FUNCTION
  68. start = timer()
  69. print("******** Matrix ********")
  70. numOfvectors = 100
  71. N = 40
  72. LIMIinclusive = 1000
  73. matrix = initializeMatrix(numOfvectors, N, LIMIinclusive)
  74. print(matrix)
  75. print()
  76.  
  77. print("******** Distances ********")
  78. print(distances(matrix))
  79. print()
  80. prettyDistances(matrix)
  81. print()
  82.  
  83. print("******** Minimum Distances for every spot ********")
  84. print(minimumDistances(matrix))
  85. end = timer()
  86. print()
  87.  
  88. print("******** Execution time ********")
  89. elapsed = end - start
  90. elapsed = round(elapsed, 2)
  91. print("Execution time for", numOfvectors, "vectors in", N, "- D domain is:", elapsed, "seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement