Ask Your Question
1

How do I do map/element-wise operations?

asked 2021-12-18 15:15:14 +0200

apromixately gravatar image

I am looking for a way to apply a function to every element of a vector or matrix.

v = vector([2, 0, -3])
print(v)
w = sign(v)
print(w) # I want (1, 0, -1)

m = matrix([[1, 2], [3, 4]])
m = m**2
print(m) # I want [1, 4] [9, 16]

How does this work?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-12-18 22:32:41 +0200

tmonteil gravatar image

You can use the apply_map method :

sage: v.apply_map(sign)
(1, 0, -1)

sage: m.apply_map(lambda x : x^2)
[ 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

1 follower

Stats

Asked: 2021-12-18 15:15:14 +0200

Seen: 186 times

Last updated: Dec 18 '21