Skip to content

Commit dcf03eb

Browse files
authored
Merge pull request #4 from AllanLRH/master
Added cumsum and cumprod functions
2 parents ee5b393 + e1bd19c commit dcf03eb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

MiniPy.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
import sublime_plugin
1010

1111

12+
def cumsum(lst):
13+
return [sum(lst[:i]) for i in range(1, len(lst)+1)]
14+
15+
16+
def cumprod(lst, use_logsum=False):
17+
def do_cumprod(lst):
18+
tmp = 1
19+
toReturn = list()
20+
for el in lst:
21+
tmp *= el
22+
toReturn.append(tmp)
23+
return toReturn
24+
25+
if use_logsum:
26+
return [exp(el_log) for el_log in cumsum([log(el) for el in lst])]
27+
else:
28+
return do_cumprod(lst)
29+
30+
1231
def dnow():
1332
return datetime.datetime.strftime(datetime.datetime.now(), '%d/%m/%Y')
1433

@@ -50,7 +69,7 @@ def set_symdiff(itr0, itr1):
5069
return s0.symmetric_difference(s1)
5170

5271

53-
def formatnum(num, digits, scientificNotation=None):
72+
def formatnum(num, digits=8, scientificNotation=None):
5473

5574
def normalFormatting(num, digits):
5675
return ('{:.%df}' % digits).format(num)

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ The functions takes two iterable arguments, which are turned into sets, and the
7676
set_symdiff('foo bar', 'foo baz') -> {'z', 'r'}
7777

7878

79+
### Computing cumulative sums and products
80+
81+
Compute the cumulative sum of an iterable:
82+
83+
cumsum([1,2,3,4,5]) -> [1, 3, 6, 10, 15]
84+
cumsum([0.02809, 0.05619, 0.08646, 0.11919, 0.15192, 0.18465, 1.31694]) -> [0.02809, 0.08428, 0.17074, 0.28993, 0.44185, 0.6265000000000001, 1.94344]
85+
86+
And a cumulative product
87+
88+
cumprod([1, 2, 3, 4, 5]) -> [1, 2, 6, 24, 120]
89+
cumprod([0.02809, 0.05619, 0.08646, 0.11919, 0.15192, 0.18465, 1.31694]) -> [0.02809, 0.0015783771, 0.000136466484066, 1.626544023582654e-05, 2.471045680626768e-06, 4.5627858492773275e-07, 6.008915196347284e-07]
90+
91+
But doing products of a lot of small numbers are prone to errors, so we can use a math trick:<br>
92+
<a href="https://www.codecogs.com/eqnedit.php?latex=\exp&space;\left[&space;\sum&space;\log&space;\left[&space;A&space;\right]&space;\right]&space;=&space;\prod&space;A" target="_blank"><img src="https://latex.codecogs.com/gif.latex?\exp&space;\left[&space;\sum&space;\log&space;\left[&space;A&space;\right]&space;\right]&space;=&space;\prod&space;A" title="\exp \left[ \sum \log \left[ A \right] \right] = \prod A" /></a>, where _A_ is the iterable. This will increase the numerical stability, as seen in this example:
93+
94+
cumprod([1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14]) -> [1e-08, 1e-17, 1e-27, 1e-38, 9.999999999999999e-51, 9.999999999999999e-64, 1e-77]
95+
cumprod([1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14], use_logsum=True) -> [9.999999999999982e-09, 9.99999999999999e-18, 1.0000000000000022e-27, 9.999999999999936e-39, 9.999999999999944e-51, 1.0000000000000049e-63, 9.999999999999967e-78]
96+
cumprod([1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14], use_logsum=True) -> # same result as above.
97+
98+
7999
### Formatting numbers
80100

81101
The fnuction `formatnum` formats numbers, and takes two mandatory and an optional argument:

0 commit comments

Comments
 (0)