Ask Your Question
3

Map a function to each element of a matrix?

asked 2012-10-23 16:19:47 +0200

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
6

answered 2012-10-23 16:34:33 +0200

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 16:19:47 +0200

Seen: 3,306 times

Last updated: Oct 23 '12