Subsequence From Madhava–Leibniz Series As Fast As Possible With Python
Madhava–Leibniz series: All I need is to create a subsequence from element k to element n: from this series to be created and returned as fast as possible. I started from 'l
Solution 1:
I slightly changed the madhava()
function by using itertools.cycle
:
def madhava(k, n): # Optimized for speed any way you like.
# you don't need to allocate the array in advance, so comment it out
# series = [0.0] * (n - k + 1)
first_divisor = 2 * k + 1
last_divisor_plus_1 = 2 * n + 2
if k & 1:
series = [what / divisor for what, divisor in zip(cycle([-1, 1]), range(first_divisor, last_divisor_plus_1, 2))]
else:
series = [what / divisor for what, divisor in zip(cycle([1, -1]), range(first_divisor, last_divisor_plus_1, 2))]
return series
Original version on my machine (AMD 2400G, Ubuntu 18.04):
('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC 8.2.0
Executing in 64bit
Time Pi 3.141592653589793 Error of calculation function
m * n = 4 * 25 = 100
0.000 3.131592903558554 0.00999975003123943 madhava
0.000 3.131592903558554 0.00999975003123943 leibniz
m * n = 4 * 250 = 1,000
0.000 3.140592653839794 0.000999999749998981 madhava
0.000 3.140592653839794 0.000999999749998981 leibniz
m * n = 4 * 2,500 = 10,000
0.001 3.141492653590034 9.99999997586265e-05 madhava
0.002 3.141492653590034 9.99999997586265e-05 leibniz
m * n = 4 * 25,000 = 100,000
0.009 3.141582653589720 1.0000000073340232e-05 madhava
0.016 3.141582653589720 1.0000000073340232e-05 leibniz
m * n = 4 * 250,000 = 1,000,000
0.091 3.141591653589774 1.0000000187915248e-06 madhava
0.150 3.141591653589774 1.0000000187915248e-06 leibniz
m * n = 4 * 2,500,000 = 10,000,000
0.890 3.141592553589792 1.0000000161269895e-07 madhava
1.546 3.141592553589792 1.0000000161269895e-07 leibniz
m * n = 4 * 25,000,000 = 100,000,000
9.002 3.141592643589326 1.0000467121074053e-08 madhava
15.699 3.141592643589326 1.0000467121074053e-08 leibniz
The version using itertools.cycle
:
('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC 8.2.0
Executing in 64bit
Time Pi 3.141592653589793 Error of calculation function
m * n = 4 * 25 = 100
0.000 3.131592903558554 0.00999975003123943 madhava
0.000 3.131592903558554 0.00999975003123943 leibniz
m * n = 4 * 250 = 1,000
0.000 3.140592653839794 0.000999999749998981 madhava
0.000 3.140592653839794 0.000999999749998981 leibniz
m * n = 4 * 2,500 = 10,000
0.001 3.141492653590034 9.99999997586265e-05 madhava
0.002 3.141492653590034 9.99999997586265e-05 leibniz
m * n = 4 * 25,000 = 100,000
0.007 3.141582653589720 1.0000000073340232e-05 madhava
0.016 3.141582653589720 1.0000000073340232e-05 leibniz
m * n = 4 * 250,000 = 1,000,000
0.061 3.141591653589774 1.0000000187915248e-06 madhava
0.152 3.141591653589774 1.0000000187915248e-06 leibniz
m * n = 4 * 2,500,000 = 10,000,000
0.608 3.141592553589792 1.0000000161269895e-07 madhava
1.530 3.141592553589792 1.0000000161269895e-07 leibniz
m * n = 4 * 25,000,000 = 100,000,000
6.167 3.141592643589326 1.0000467121074053e-08 madhava
15.617 3.141592643589326 1.0000467121074053e-08 leibniz
Another version, using itertools.starmap
and operator.truediv
:
def madhava_leibniz_starmap(k, n):
return starmap(truediv, zip(cycle([1, -1] if k & 1 else [1, -1]), range(2*k+1, 2*n+2, 2)))
On my computer this yields:
('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC 8.2.0
Executing in 64bit
chunks * elements = m * n = 4 * 25,000,000 = 100,000,000
Time Pi 3.141592653589793 Error of calculation function
15.569 3.141592643589326 1.0000467121074053e-08 leibniz
8.968 3.141592643589326 1.0000467121074053e-08 madhava
6.182 3.141592643589326 1.0000467121074053e-08 madhava_leibniz
5.238 3.141592643589326 1.0000467121074053e-08 madhava_leibniz_starmap
Which is roughly one second faster than list comprehension.
Post a Comment for "Subsequence From Madhava–Leibniz Series As Fast As Possible With Python"