TransWikia.com

Vectorization of a function

Mathematica Asked on July 9, 2021

I would like to calculate the exponential values of the elements of a list / vector. For example, I would like to calculate the Boltzmann distribution over different energy levels where the normalized population of the energy level, $N_i$, is calculated using the energy of those levels, $E_i$, and temperature($T$) as:

$$N_i= frac{exp(-frac{E_i}{T})}{sum {exp(-frac{E_i}{T})}} $$

Exp[E] doesn’t work if E is a vector. I guess I can calculate element by element for each $N_i$ using a loop for all i in an E[[i]], but in most language, there is a way to vectorize functions (function acting on a vector elementwise), which are generally way faster and nicer. What is the recommended “Mathematical” way to do this?

Edit: Thank you everyone for the help. It turned out I had a stupid mistake ( 11i think a 0/0 somewhere inside the vector) that is why Exp[] didn’t work on a vector.

2 Answers

Try

ei = RandomReal[{0, 1}, 5]
#/Total[#] &[Exp[-ei]]

Correct answer by Ulrich Neumann on July 9, 2021

Maybe this ?

e = {1, 2, 3, 4};
Normalize[Exp[-e/T], Total]

Answered by cvgmt on July 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP