Posts: 8,090
Threads: 154
Joined: Sep 2016
1. If you don't get NameError for xx then you have defined it somewhere outside the code you show to us.
2. You assign xx to a0 and then append xx to cc
3. In the code shown you never change xx - as a result you append same value of xx over and ove again.
4. On line 10 you reset cc in every iteration of the loop.
5. As a result of 3 and 4 cc has only one value always - original/initial value of xx that we know nothing about. So why cc is a list at all?
6. On line 12 you refer to cc[1][1], but as we see from 4 it has only 1 value so only c[0], i.e. 1 is invalid index for cc - that is why you get your error
7. given that you use cc[1][1], c[0][1], c[-1][-1] and the one and only element is cc is actually xx I would guess xx is a list with unknown number of elements (as we said multiple times in your code it is defined somewhere else, so we don't know about it)
Posts: 203
Threads: 41
Joined: Mar 2019
I guarantee you that the first time xx shows is when I set it to a0.
However I am trying to make a0 a list, so if you are aware of a way to do that
without using xx, please share, as I have tried before but I got a float object error
(see previous comments).
In order for you to have a better Idea of what I am trying to do,
I will write again the iteration formulas I am trying to accomplish
with this code:
the first 6 lines of my code are totally correct, and the code right after is based on it.
Now we have cc_sub(0,i)=a0
cc_sub(k,i)=( cc_sub(k-1,i)-4^(-k)*cc_sub(k-1,i-1) )/(1 - 4^(-k))
I want cc to be dependent on line 5-6 and not 3-4.
I transformed a0 into a list because the new a0 (cc) has two iteration characters (k,i)
as you can see from the formula.
we can forget about xx and not use it at all if is easier for you to explain without
this variable, but please let me know how my code should change in order to accomplish the
iteration above written.
(again, the first 6 lines should be left untouched, because they are correct)
Let me please know if there is any further clarification you want to request.
Posts: 8,090
Threads: 154
Joined: Sep 2016
(Apr-24-2019, 09:07 AM)mcgrim Wrote: I guarantee you that the first time xx shows is when I set it to a0. And I guarantee you that if this is true you will get NameError :-)
https://repl.it/repls/HeartyAltruisticPr...nglanguage
Posts: 203
Threads: 41
Joined: Mar 2019
I'm really not sure why I don't get this error.
Anyway, as I said before, I am willing do achieve what I described above, without xx.
Do you perhaps know how to change my code in order to accomplish the Iteration I posted above?
Posts: 203
Threads: 41
Joined: Mar 2019
Apr-24-2019, 11:26 AM
(This post was last modified: Apr-24-2019, 11:29 AM by mcgrim.)
here is my new code:
def fastln(n, x):
a0 =(1 + x) / 2
b0 =x ** (1 / 2)
for i in range(n):
a0=(a0 + b0) / 2
b0=((a0 * b0) ** (1 / 2))
cc=[]
cc.append(a0)
for i in range(0,n+1):
for k in range(1,n+1):
cc[k][i]=((cc[k-1][i] - 4 ** (-k) * cc[k-1][i-1])/(1 - 4 ** (-k)))
return (x - 1)/cc[-1][-1]
print(fastln(4,4)) and here is the new error:
runfile('C:/Users/Desktop/python ode/untitled3.py', wdir='C:/Users/Desktop/python ode')
Traceback (most recent call last):
File "<ipython-input-311-24ba76ba66c4>", line 1, in <module>
runfile('C:/Users/Desktop/python ode/untitled3.py', wdir='C:/Users/Desktop/python ode')
File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Desktop/python ode/untitled3.py", line 86, in <module>
print(fastln(4,4))
File "C:/Users/Desktop/python ode/untitled3.py", line 83, in fastln
cc[k][i]=((cc[k-1][i] - 4 ** (-k) * cc[k-1][i-1])/(1 - 4 ** (-k)))
TypeError: 'float' object is not subscriptable
can anyone help me?
Posts: 8,090
Threads: 154
Joined: Sep 2016
Apr-24-2019, 11:35 AM
(This post was last modified: Apr-24-2019, 12:46 PM by buran.)
why 2 indexes and double loop
I think you need just
def fastln(n, x):
a0 =(1 + x) / 2
b0 =x ** (1 / 2)
for i in range(n):
a0=(a0 + b0) / 2
b0=((a0 * b0) ** (1 / 2))
cc=[]
cc.append(a0)
for i in range(1,n+1):
cc.append((cc[i-1] - 4 ** (-i) * cc[i-1])/(1 - 4 ** (-i)))
return (x - 1)/cc[-1]
EDIT: This code is NOT correct implementation of the algorithm. Check this post for correct implementation.
Posts: 203
Threads: 41
Joined: Mar 2019
Apr-24-2019, 11:39 AM
(This post was last modified: Apr-24-2019, 11:40 AM by mcgrim.)
Thank you buran, this is actually running.
However, I need to ask you a question for my understanding.
I used a double loop because as you can see from the recursive formula posted above,
there are two iterands (i,k)
So now I am not really understanding how does your code work, even though you only
used 'i' ?
for emphasis I will write the recursive formula again in python format
a0=cc
cc[k][i]=((cc[k-1][i] - 4 ** (-k) * cc[k-1][i-1])/(1 - 4 ** (-k)))
Posts: 8,090
Threads: 154
Joined: Sep 2016
Apr-24-2019, 11:43 AM
(This post was last modified: Apr-24-2019, 11:43 AM by buran.)
can you provide some source where you take your formula from (i.e. math background)? I see just your code and it's you who put double loop and double index without knowing why
Posts: 203
Threads: 41
Joined: Mar 2019
Posts: 8,090
Threads: 154
Joined: Sep 2016
Apr-24-2019, 06:34 PM
(This post was last modified: Apr-24-2019, 06:34 PM by buran.)
OK, now that makes more sense
Here is the code you wanted to implement
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)) Output: 1.3862943611192118
1.3862943611198906
1.3862943611198906
>>>
As you can see with n=4 there is still some difference with what math module would calculate. With n=5 calculations are the same.
Maybe it's possible to change something here and there - at the moment I just reproduce the algorithm from the paper.
|