How does one perform the exp() operation element-wise in Juila?

China☆狼群 提交于 2020-01-11 09:41:49

问题


I'm new to Julia and this seems like a straight-forward operation but for some reason I am not finding the answer anywhere.

I have been going through some tutorials online and they simply use exp(A) where A is a nxm matrix but this gives me a DimensionMismatch error.

I looked through the documentation on the official website in the elementary functions as well as the linear algebra section and googled it multiple times but can't find it for the life of me.


回答1:


In julia, operations on matrices treat the matrix as an object rather than a collection of numbers. As such exp(A) tries to perform the matrix exponential which is only defined for square matrices. To get element-wise operations on matrices, you use broadcasting which is done with the dot operator. Thus here, you want exp.(A).

This design is used because it allows any scalar operation to be done on arrays rather than just the ones built in to the language.




回答2:


The broadcasting operator . always changes a function to "element-wise". Therefore the answer is exp.(A), just like sin.(A), cos.(A), or f.(A) for any user-defined f.




回答3:


In addition to the above answer, one might also wish to consider the broadcast operator with function piping:

A = rand(-10:10, 3, 3)

A .|> sin .|> inv


来源:https://stackoverflow.com/questions/59232420/how-does-one-perform-the-exp-operation-element-wise-in-juila

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!