I have a problem in evaluating a sum using values from data I input.

Suppose I have data = {1, 3, 5, 6, 8}

If I want to evaluate the sum of my data (1+3+5+6+8), I can use the following command:

Sum[x, {x, data}]

And it will give me 23, which is correct.

However, I found difficulty if I want to evaluate the sum of Log of each value, such that I have: Log(1) + Log(3) + Log(5) + Log(6) + Log(8). This can be done manually if I type the function one by one, but then it is not efficient if I have 100 values to be calculated. Can anyone help me in solving this problem?

  • 1
    $\begingroup$ Something like Total[Log[data]]? $\endgroup$ – J. M. will be back soon Oct 18 '12 at 12:39
  • 2
    $\begingroup$ and if you want to stick to your strategy Sum[x,{x,Log[data]}] $\endgroup$ – chris Oct 18 '12 at 12:55

For the sum it's much better to use Total


for the Logs, you can use the fact that Log is Listable and write

(*Log[3] + Log[5] + Log[6] + Log[8]*)

(if you need a numerical value just use N)


You can still you Sum, but you are now summing $\sum_{x \in \text{data}}\log(x)$ rather than $\sum_{x \in \text{data}}x$:

In[367]:= data = {1, 3, 5, 6, 8};
Sum[Log[x], {x, data}]

Out[368]= Log[3] + Log[5] + Log[6] + Log[8]

In[369]:= Total[Log[data]]

Out[369]= Log[3] + Log[5] + Log[6] + Log[8]
  • $\begingroup$ Sasha, you're back! I hope you stay around this time. :-) $\endgroup$ – Mr.Wizard Oct 18 '12 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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