First time here? Check out the FAQ!

Ask Your Question
3

Map a function to each element of a matrix?

asked 12 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
6

answered 12 years ago

achrzesz gravatar image
sage: A = matrix([[1,2],[3,4]])
sage: f(x)=x^2                 
sage: A.apply_map(f)           
[ 1  4]
[ 9 16]
Preview: (hide)
link

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: 12 years ago

Seen: 3,688 times

Last updated: Oct 23 '12