I'm trying to create functions in Python that would update calculations for a matrix only for new entries. These functions are created within a Design class, where users would submit a design into an existing database of designs. The distance between each design should be calculated, but ideally not by brute force every time so as to optimize for larger sized matrices (e.g. if there were 100+ designs). Here's what I have so far:

def compute_matrix(self, all_design, new_design):
    M = {}
    count = 0
    #Does the design already exist?
    existing_design = None
    #Do not calculate every time
    update_distance = None
    for i in self.designs:
        for j in self.designs:
            if i not in M:
                M[i] = {}
                current_design = M[i]
                count += 1
            if i == j:
    #need to test - only update based on new entries
    for current_design in all_design:
        update_distance = self._distance(new_design, current_design)
        #Is calculation exactly the same?
        if update_distance is None or current_distance == update_distance:
    ##M[i][j] = self._distance(i, j)
    return current_distance

def _distance(self, i, j):
    p1 = self.designs[i].values()
    p2 = self.designs[j].values()
    ##Using pdist to calculate distance matrix
    dm = pdist(M, reduce(lambda a,x: (x[1]-x[0])**2 + a, zip(p1,p2), 0))
    return dm

Designs are of 3D models where displacement forces are calculated on each model. Distances would evaluate how similar or dissimilar each collection of measured forces are from one another. So in a matrix of collected information of 100+ designs, a user would ideally be able to determine the top 5 most similar or dissimilar designs from a submitted one based on the distance between stored values within each design.

M is a matrix defined within compute_matrix and is the initialized matrix where submitted designs with their values will be stored. Time would probably be the primary concern with these calculations. E.g. if a row is added into a matrix of 100*100 designs, I would not want a 101*101 calculation to be done - rather just the distance between the 101st design and the rest of the designs to be updated instead of iterating through all the designs again.

How could I think about formatting the code properly? And might something like scipy.spatial.cKDTree be usable for a scenario like this? Or would that be more appropriate for finding the nearest vector in a matrix, and not the distance between each and every vector in a matrix?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.