Python Forum
semantic error log function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
semantic error log function
#21
Thank you buran.
I can see that you marked your previous code as 'not correct',
but when I try it for different n, I obtain almost the same result
as your last code, is that perhaps a coincidence?
I also wanted to ask you questions about a few lines on your last code:

On line 5, I am understanding that you are creating a list 'd', but I don't understand
why isn't it an empty list? And why the double brackets?

On line 9 I can see that you are trying to turn 'a' into a list, but why the '-1' ?

on line 11, why appending the empty list?

on line 13, I can't understand the d[k].append.


I appreciate your time and help.
Thank you.
Reply
#22
It's too late here. I will write a detail answer tomorrow.

The previous code will yield similar, close result, but it's not the algorithm from the paper
You can print cc and d respectively in every iteration of the loops and compare the results. Then you can compare the values used in the calculation of cc/d
at the moment my code reproduce verbatim the algorithm from the paper, i.e. it calculates all d(k,n) values and it uses d(n, n) to calculate the log.
you were right that d has to be list of lists. Actually that is the short answer of all your remaining question. but I will write detail explanation tomorrow
if you understand the algorithm from the paper you should not have problem with my code. I intentionally used the same variable names. the only difference, because you already had n, we use i.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#23
Thanks a lot.
I would love that.
I do understand the math algorithm, but having it translated
into coding can be quite confusing at times.

now your last comment got me a bit lost, lol !
Reply
#24
(Apr-24-2019, 08:17 PM)mcgrim Wrote: now your last comment got me a bit lost, lol !
sorry, I've deleted it - as I said it's late here and I was confused - it was not correct. sorry
I will write again with fresh head tomorrow :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#25
no prob
Reply
#26
I will repost the code here, so we can refer to same code/line numbers

import math
def fastln(n, x):
    a =(1 + x) / 2
    g = x ** (1 / 2)
    d = [[a]] 
    for i in range(1, n + 1):
        a = (a + g) / 2
        g = ((a * g) ** (1 / 2))
        d[-1].append(a)
    for k in range(1, n + 1):
        d.append([])
        for i in range(n + 1):
            d[k].append((d[k - 1][i] - 2 ** (-2 * k) * d[k - 1][i - 1])/(1 - 2 ** (-2 * k)))
    return (x - 1)/d[-1][-1]
   
print(fastln(4,4))
print(math.log(4, math.e))
print(fastln(5,4))
As I said you need to understand that d is list of lists (n lists with n elements - float numbers each), so that it can hold/store calculated values for d(k, n) for all combinations of k and n. I.e. you can consider/call it n X n matrix, 2-dimensional array, whatever term you are familiar with...

(Apr-24-2019, 07:53 PM)mcgrim Wrote: On line 5, I am understanding that you are creating a list 'd', but I don't understand why isn't it an empty list? And why the double brackets?
on line 5 I initialize d with one element - empty list and that element/list has one float value that is a calculated on previous lines. i.e. that is a0 that we store in d[0][0] or d(0, 0) = a0 if we look at the paper, e.g.

>>> a = 2.5 # that's just example, assume a0 is calculated to be 2.5
>>> d = [[a]]
>>> type(d)
<class 'list'>
>>> type(d[0])
<class 'list'>
>>> d[0][0]
2.5
>>>
in slow-motion d = [[a]] is the same as
d = [] # create empty list
d.append([]) # append empty list. now d is a list with one element which is empty list
d[0].append(a) # now we append to the first element of d, which is a list, so now d == [[a]]
(Apr-24-2019, 07:53 PM)mcgrim Wrote: On line 9 I can see that you are trying to turn 'a' into a list, but why the '-1' ?
I don't try to turn a into a list. I append current calculated value of a to last element element of d (remember python allows negative indexes of list and index -1 is the last element). At that time d has only one element - one list. Before line 9 this list has only i number of values, i.e. len(d[0]) == i. d has obe element - one list and that list has n float values in it.
Now we need to calculate

If you prefer line 9 could look
d[0].append(a)
From point of view of the paper that is d(0, i) = ai

(Apr-24-2019, 07:53 PM)mcgrim Wrote: on line 11, why appending the empty list?
after first loop (lines 6-9) is complete we have calculated n values, i.e. all d(0, i) values where i is from 0 to n. (i.e. for i in range(0, n+1)
Now we use 2 nested loops to calculate rest of the values. we need to add another list to d, so that we can store all d(1, i) values where i is from 0 to n. (i.e. for i in range(0, n+1). After line 11 d will have k+1 elements - i.e. k+1 lists

(Apr-24-2019, 07:53 PM)mcgrim Wrote: on line 13, I can't understand the d[k].append.
As we need to calculate all d(k, n) values, now we calculate the value for respective value of k and i and store it to last element (it has index -1 or k) of d. If you prefer d[k].append(... here is the big recursive formula...) you can have d[-1].append(... here is the big recursive formula...)


here is the code with some extra comments and in smaller steps
def fastln(n, x):
    a =(1 + x) / 2
    g = x ** (1 / 2)
    d = []
    d.append([])
    d[0].append(a) # you can write also it like this: d[-1].append(a) 
    for i in range(1, n + 1):
        a = (a + g) / 2
        g = ((a * g) ** (1 / 2))
        d[0].append(a) # you can write also it like this: d[-1].append(a)
    
# at this point we have calculated all d(0, n) values
# so len(d) == 1
# and len(d[0]) == n    
# now start calculating rest d(k, n) values
  
    for k in range(1, n + 1):
        d.append([]) # now d has k+1 elements-lists. the last element has index k or -1
        for i in range(n + 1):
            calculated_value = (d[k - 1][i] - 2 ** (-2 * k) * d[k - 1][i - 1])/(1 - 2 ** (-2 * k))
            d[k].append(calculated_value) # you can write also it like this: d[-1].append(calculated_value) 
    return (x - 1)/d[-1][-1]
In addition, we can write it a bit different if it is easier for you to understand:
def fastln(n, x):
    a =(1 + x) / 2
    g = x ** (1 / 2)
    d = [[] for _ in range(n)]

# at this point d already has n elements, all of them - empty lists, so len(d) == n
    d.append([])
    d[0].append(a)
    for i in range(1, n + 1):
        a = (a + g) / 2
        g = ((a * g) ** (1 / 2))
        d[0].append(a)
    
# at this point we have calculated all d(0, n) values
# so len(d) == n
# and len(d[0]) == n    
# now start calculating rest d(k, n) values
  
    for k in range(1, n + 1):
        for i in range(n + 1):
            calculated_value = (d[k - 1][i] - 2 ** (-2 * k) * d[k - 1][i - 1])/(1 - 2 ** (-2 * k))
            d[k].append(calculated_value)
    return (x - 1)/d[-1][-1]
note that in this case we cannot use d[-1].append(), because now d has n elements from the start, so we need to use the exact index when we append. Also we don't need to append empty list in each iteration of the outer loop (for each value of k).

(Apr-24-2019, 07:53 PM)mcgrim Wrote: I can see that you marked your previous code as 'not correct', but when I try it for different n, I obtain almost the same result as your last code, is that perhaps a coincidence?

yes, you may consider it coincidence. i.e. it calculates something, some approximation, but when/how close it will be is unclear.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#27
1000 thanks !
Reply


Forum Jump:

User Panel Messages

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