Python Forum
Simplifying a short code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simplifying a short code
#1
I'm working on simplifying a part of a function. d_n actually represents a double sequence, call it d_ki, where both indices k and i begin at 0 and terminate at n. Evaluating the sequence for any n yields a "matrix" where the xth first columns in the xth row fall away (noting that the first row is row zero, second is first and so on). Every d_n thus represents the nth, ever-decreasing row in the "matrix". a is a previously defined list and x a constant. Is there a way to simplify the code so that when evaluating the function for all n, one mustn't change the code every time?

def ln(x,n):
     ... 
     d_0 =[]
     for k in range(n+1):
     d_0.append(a[k])
     d_1=[]
     for k in range (1,n+1):
     d_1.append((d_0[k]-4**(-1)*d_0[k-1])/(1-4**(-1)))
     d_2=[]
     for k in range(2,n+1):
     d_2.append((d_1[k-1]-4**(-2)*d_1[k-2])/(1-4**(-2)))
     d_3=[]
     for k in range(3,n+1):
     d_3.append((d_2[k-2]-4**(-3)*d_2[k-3])/(1-4**(-3)))
     ...
     d_n=[]
     for k in range(n,n+1):
     d_n.append((d_n-1[k-(n-1)]-4**(-n)*d_n-1[k-n])/(1-4**(-n)))
     return (x-1)/d_n[0]
Reply
#2
It's not clear to me what you mean. Can you give an example matrix for small k and i, and repost your code with proper indentation.

I expect this can be done with nested lists and a nested for loop, but I'd want a better understanding of the problem before proposing a solution.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I'm approximating the natural logarithm according to the formulas given in this paper https://www.ams.org/journals/mcom/1972-2...7438-2.pdf (see second page, under 'Algorithm for logarithm'). When evaluating dki one attains a matrix of the form
([[d_00,d_01,d_02,...,d_nn],
  [     d_11,d_12,...,d_nn],
  [               ...,d_nn],
  [          d_n-1n-1,d_nn],
  [                   d_nn]])
Here is the full code.
def ln(x,n):
    a =[(1+x)/2]
    g =[x**(1/2)]
    for i in range(n):
        a.append((a[i]+g[i])/2)
        g.append((a[i+1]*g[i])**(1/2))
    d_0 =[]
    for k in range(n+1):
        d_0.append(a[k])
    d_1=[]
    for k in range (1,n+1):
        d_1.append((d_0[k]-4**(-1)*d_0[k-1])/(1-4**(-1)))
    d_2=[]
    for k in range(2,n+1):
        d_2.append((d_1[k-1]-4**(-2)*d_1[k-2])/(1-4**(-2)))
    d_3=[]
    for k in range(3,n+1):
        d_3.append((d_2[k-2]-4**(-3)*d_2[k-3])/(1-4**(-3)))
    ...
    d_n=[]
    for k in range(n,n+1):
        d_n.append((d_n-1[k-(n-1)]-4**(-n)*d_n-1[k-n])/(1-4**(-n)))
    return (x-1)/d_n[0]
Reply
#4
As I said, nested for loops and nested lists. I think this will work:

def ln(x, n):
    a =[(1 + x) / 2]
    g =[x ** (1 / 2)]
    for i in range(n):
        a.append((a[i] + g[i]) / 2)
        g.append((a[i+1] * g[i]) ** (1 / 2))
    d = [a[:]]
    for i in range(1, n + 1):
    	d.append([])
    	for k in range(i, n + 1):
    		d.append(d[-2][k - i - 1] - 4 ** (-1) * d[-2][k - i] / (1 - 4 ** (-i)))
    return (x - 1) / d[-1][0]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Nice! I made some minor adjustments, and the code below should work. Thanks!

def ln(x, n):
    a =[(1 + x) / 2]
    g =[x ** (1 / 2)]
    for i in range(n):
        a.append((a[i] + g[i]) / 2)
        g.append((a[i+1] * g[i]) ** (1 / 2))
    d = [a[:]]
    for i in range(1, n + 1):
        d.append([])
        for k in range(i, n + 1):
            d[i].append((d[-2][k - i +1] - 4 ** (-i) * d[-2][k - i])/(1 - 4 ** (-i)))
    return (x - 1)/d[-1][0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simplifying the tie function blacklight 2 2,137 Jul-01-2020, 02:49 PM
Last Post: deanhystad
  Simplifying a rather short code schniefen 1 2,011 Apr-07-2019, 02:11 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020