I'd like to implement a geometric progression using Python / Pandas / Numpy.
Here is what I did:
N = 10
n0 = 0
n_array = np.arange(n0, n0 + N, 1)
u = pd.Series(index = n_array)
un0 = 1
u[n0] = un0
for n in u.index[1::]:
#u[n] = u[n-1] + 1.2 # arithmetic progression
u[n] = u[n-1] * 1.2 # geometric progression
print(u)
I get:
0 1.000000
1 1.200000
2 1.440000
3 1.728000
4 2.073600
5 2.488320
6 2.985984
7 3.583181
8 4.299817
9 5.159780
dtype: float64
I wonder how I could avoid to use this for loop.
I had a look at https://fr.wikipedia.org/wiki/Suite_g%C3%A9om%C3%A9trique and found that u_n can be expressed as: u_n = u_{n_0} * q^{n-n_0}
So I did that
n0 = 0
N = 10
n_array = np.arange(n0, n0 + N, 1)
un0 = 1
q = 1.2
u = pd.Series(map(lambda n: un0 * q ** (n - n0), n_array), index = n_array)
That's ok... but I'm looking for a way to define it in a recurrent way like
u_n0 = 1
u_n = u_{n-1} * 1.2
But I don't see how to do it using Python / Pandas / Numpy... I wonder if it's possible.
un0 * 1.2 ** np.arange(N)? It is not recursive, though.u = pd.Series(un0 * q ** np.arange(N), index=n_array. I'm still looking for a recursive solution (without for loop)