简体   繁体   中英

Fast approx to ln

link to problem

I need to write a fast approx of ln in Python and use the 2.4 algorithm. I know I can get the first a_i numbers with:

 def f1(x,i): a0=(x+1)/2 g0=np.sqrt(x) a=[] for j in range(i): a0=(a0+g0)/2 g0=np.sqrt((a0)*g0) a.append(a0) return a

If you use a0 and g0 inside the for loop, you make a mistake. You need to initialise arrays with those values.

def f1(x, n):
    a = [(1 + x) / 2]
    g = [np.sqrt(x)]
    for i in range(n + 2):
        a.append((a[i] + g[i]) / 2)
        g.append(np.sqrt(a[i+1] * g[i]))
    return a[n]

In the function above I also keep the notation consistent with the paper. Let me know if this helps, and please flag my comment as an answer to the question if it does.

Yea but im thinking it could be usefull to have it in array so i can calculate the next d(n,k) in the next function. But i have no idea even what to start with. Im also going to plot how bit the error is with n number of iterations.

this is how im thinking but i know its wrong

 def fast_approx(x,n): a = [(1 + x) / 2] def f1(x, n): a = [(1 + x) / 2] g = [np.sqrt(x)] for i in range(n + 2): a.append((a[i] + g[i]) / 2) g.append(np.sqrt(a[i+1] * g[i])) return a[n] def the_big_D(n,k): dnull=a[n] d=[] for i in range(k): dnull=(a[n-1] - 4**(-k) * a[n-2]) / (1 - 4**(-k)) d.append(dnull) return d[n]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM