Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
Something like Total[Log[data]]? –  J. M. Oct 18 '12 at 12:39
2  
and if you want to stick to your strategy Sum[x,{x,Log[data]}] –  chris Oct 18 '12 at 12:55

2 Answers 2

up vote 4 down vote accepted

For the sum it's much better to use Total

Total@data
(*23*)

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

Total@Log@data
(*Log[3] + Log[5] + Log[6] + Log[8]*)

(if you need a numerical value just use N)

share|improve this answer

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]
share|improve this answer
    
Sasha, you're back! I hope you stay around this time. :-) –  Mr.Wizard Oct 18 '12 at 12:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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