# Using matrix for algebraic operation with its elements

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

edit retag close merge delete

Sort by » oldest newest most voted

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)]

more

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

more

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!

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)

more