Last active
May 4, 2019 23:33
-
-
Save tnibert/a0da09699f001d5e56cf82d1875f5ef1 to your computer and use it in GitHub Desktop.
Summation (sigma) notation in python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# | |
# Σ i | |
# i=s | |
from functools import reduce | |
# the sequence to sum, you can use range(blah) as well | |
s = [1, 2, 3, 5] | |
# alter this lambda for the right side of the sigma | |
right_side_of_sigma = lambda a, x: a + x | |
result = reduce(right_side_of_sigma, [0] + s) | |
print(result) | |
# another example: | |
result = reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5]) | |
assert result == ((3.14*2*2) + (3.14*3*3) + (3.14*5*5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment