Ask Your Question

Revision history [back]

You are implicitly treating the expression (x-(sum(vector(M)))/len(vector(M)))^2 as a function.

You should be more explicit about it, e.g. replacing it by lambda x: (x-(sum(vector(M)))/len(vector(M)))^2.

Note that it would be more efficient to compute (sum(vector(M)))/len(vector(M)) once instead of doing it anew every time.

You are implicitly treating the expression (x-(sum(vector(M)))/len(vector(M)))^2 as a function.

You should be more explicit about it, e.g. replacing it by lambda x: (x-(sum(vector(M)))/len(vector(M)))^2.

Note that it would be more efficient to compute (sum(vector(M)))/len(vector(M)) once instead of doing it anew every time.


To be explicit:

def varelmat(M) :
    return sum((vector(M)).apply_map(lambda x: (x-(sum(vector(M)))/len(vector(M)))^2))/len(vector(M))

or, alternatively e.g.

def varelmat2(M):
    avg = sum(M.list())/len(M.list())
    return vector(M - avg*ones_matrix(M.base_ring(),M.nrows(), M.ncols())).norm(2)^2 / len(M.list())