How to calculate equation with summation in Python

Most modern mathematics might have summation in some parts of equation. Implementing this kind of equations in Python is very easy and straightforward. Python already provides built-in functions to help us, e.g., sum() and map().

For example, below is the expected information.

l(S) = -sum(Si/S * log2(Si/S))

map() helps us to compute each element one by one.

import math
 
def l(*s):
    S = float(sum(s))
    return -sum(map(lambda i: i/S*math.log(i/S,2),s))
 
print l(84,42)

As a result, we will get as follow.

0.918295834054

Tags: , ,

Math Help

I am working on a site. I am aiming to create over 1000 video tutorias by the end of 2007. Take a look: Math Online Video Tutorials

Post new comment