Ask Your Question
2

Map a function to each element of a matrix?

asked 2012-10-23 09:19:47 -0500

daniel.e2718 gravatar image

Is there a direct way to apply a function to each entry of a matrix and return the resulting matrix?

A = matrix([[1,2],[3,4]])
B = matrix([[j^2 for j in i] for i in A])

Not terribly hard, but it's not ideal.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2012-10-23 09:34:33 -0500

achrzesz gravatar image
sage: A = matrix([[1,2],[3,4]])
sage: f(x)=x^2                 
sage: A.apply_map(f)           
[ 1  4]
[ 9 16]
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-10-23 09:19:47 -0500

Seen: 1,270 times

Last updated: Oct 23 '12