# Warning against a deprecated function

When programming the following code

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


and applying it

B= random_matrix(QQ, 7,7)
varelmat(B)


I end with the following warning

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/repl/ipython_kernel/__main__.py:3: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details. IPKernelApp.launch_instance(kernel_class=SageKernel'

Could some one explain me how to keep my code save ?

edit retag close merge delete

Sort by » oldest newest most voted 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())
`
more

rburing sorry for my incompetence but I do not knwo how to use the lambda function in that case.