In your last line, you use h
to define h
itself.
EDIT (given the explanation provided in comments): it is not possible to do such a thing. Indeed, when you write h=[...]
, the Python name h
is created after the object [...]
is created.
So, you have to define a recursive function as follows:
sage: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
That said, i guess (i might be wrong in your motivation), that you wanted to use a list so that when you compute h[4]
, you do not have to do the whole recursion by computing h[3]
, h[2]
, h[1]
, h[0]
again, which is what hh
does. The trick is to use the @cached_function
decorator, that computes h[i]
only once and memoizes the result for further calls. So, you can do:
sage: @cached_function
....: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
As written, the definitions of
f(x)
,g(x)
,c
, ands
are not used in the final result. The definition ofd
can be simplified tod = range(9)
. The lineg = [d[i]-d[i-1] for i in range(1,len(d))]
is just taking differences of consecutive integers (the entries ofrange(9)
), so every entry of the listg
is 1. So the code could be simplified to two lines:g = [1] * 9
and (the broken line)h = [...]
. Max Alekseyev's answer is a quick way to use recursion on the entries of the listg
.@john-palmieri i guess that the provided example is only a minimal example to show the issue, otherwise, one could simply put
h=range(8)
(orlist(range(9))
) from scratch. In particular, is it not clear to me whetherh
is supposed to be more complex than just accumulating sums fromg
.@tmonteil. Right, I understand, although for a minimal example, it's far from minimal.