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
- sugree's blog
- 6298 reads
Math Help
Post new comment