Ask Your Question
0

Using matrix for algebraic operation with its elements

asked 7 years ago

pifko gravatar image

Hi,

I would like to know if it's possible to use the matrix for algebraic calculations with its "individual" matrix elements. For example, i have matrix Z and i would like to logarithm each element, something like log(Z). Or do another algebraic operation with all elements separetly. What is the best practice?

Thanks

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

mforets gravatar image

there is also apply_map:

sage: Z = random_matrix(ZZ, 5, x=1, y=10)
sage: Z.apply_map(lambda z : log(z))

[log(6)      0 log(6) log(2) log(8)]
[log(3) log(6) log(8) log(3) log(5)]
[log(6) log(9) log(5) log(4) log(6)]
[log(8) log(3) log(7) log(2) log(6)]
[     0      0 log(4) log(7) log(8)]
Preview: (hide)
link
0

answered 7 years ago

calc314 gravatar image

One approach is to use numpy arrays. For example...

import numpy as np
a = np.array([[1,e,3.3],[4.5,5.6,7.8]])
print log(a)
print np.log(a)
Preview: (hide)
link
0

answered 7 years ago

pifko gravatar image

Thank you both for the answer. Apply_map is a great feature. Thank you very much.

Preview: (hide)
link

Comments

If you feel like that answer solved your problem, don't forget to click the "check mark" so that future visitors to this question will know it has a correct answer!

kcrisman gravatar imagekcrisman ( 7 years ago )

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

Seen: 317 times

Last updated: Jun 19 '17