1
$\begingroup$

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.

$\endgroup$
3
  • 3
    $\begingroup$ I don't think I understand what you mean. Functions like Exp automatically thread over lists element-wise. For example: Exp[Range[5]]. Is that not what you need? $\endgroup$ Commented Nov 4, 2020 at 10:05
  • 3
    $\begingroup$ Don't use E as a symbol, it's predefined! $\endgroup$ Commented Nov 4, 2020 at 10:13
  • $\begingroup$ @UlrichNeumann Thanks, I will be careful. $\endgroup$
    – Greg
    Commented Nov 4, 2020 at 16:47

2 Answers 2

3
$\begingroup$

Try

ei = RandomReal[{0, 1}, 5]
#/Total[#] &[Exp[-ei]]
$\endgroup$
1
  • $\begingroup$ Very interesting notation and it works well, too! Thanks! $\endgroup$
    – Greg
    Commented Nov 4, 2020 at 16:56
4
$\begingroup$

Maybe this ?

e = {1, 2, 3, 4};
Normalize[Exp[-e/T], Total]
$\endgroup$
1
  • $\begingroup$ works very well, thanks! $\endgroup$
    – Greg
    Commented Nov 4, 2020 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.