Splitting math sums with python -
this simple question has been bothering me while now.
i attempting rewrite code parallel, , in process need split sum done on multiple nodes , add small sums together. piece working this:
def pia(n, i): k = 0 lsum = 0 while k < n: p = (n-k) ld = (8.0*k+i) ln = pow(16.0, p, ld) lsum += (ln/ld) k += 1 return lsum
where n
limit , i
integer. have hints on how split , same result in end?
edit: asking, i'm not using pow()
custom version efficiently floating point:
def ssp(b, n, m): ssp = 1 while n>0: if n % 2 == 1: ssp = (b*ssp) % m b = (b**2) % m n = n // 2 return ssp
since variable that's used 1 pass next k
, , k
increments 1 each time, it's easy split calculation.
if pass k
pia
, you'll have both definable starting , ending points, , can split many pieces want, , @ end, add results together. like:
# instead of pia(20000, i), use pia(n, i, k) , run result = pia(20000, i, 10000) + pia(10000, i, 0)
also, since n
used both set limits , in calculation directly, these 2 uses need split.
from math import pow def pia(nlimit, ncalc, i, k): lsum = 0 while k < nlimit: p = ncalc-k ld = 8.0*k+i ln = ssp(16., p, ld) lsum += ln/ld k += 1 return lsum if __name__=="__main__": i, ncalc = 5, 10 print pia(10, ncalc, i, 0) print pia(5, ncalc, i, 0) + pia(10, ncalc, i, 5)
Comments
Post a Comment