Ask Your Question
0

Warning against a deprecated function

asked 2020-05-27 11:33:48 +0100

Cyrille gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-05-27 11:42:53 +0100

rburing gravatar image

updated 2020-05-27 20:07:37 +0100

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())
edit flag offensive delete link more

Comments

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

Cyrille gravatar imageCyrille ( 2020-05-27 16:25:55 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-05-27 11:33:48 +0100

Seen: 740 times

Last updated: May 27 '20