Alternate Geometric Series¶
A Geometric Series is a series summing the terms of an infinite geometric sequence in which the ratio of consecutive terms is constant.
Thus, the geometric sequence is an infinite sequence that starts at an initial term and each following term is the result of multiplying the previous one by a fixed ratio. For example, if the initial term is \(1.0\) and the ratio is \(2.0\), the resulting gometric sequence is \(1.0, 2.0, 4.0, 8.0, 16.0, ...\) and the corresponding geometric series is the sum of all these terms is infinite.
When the ratio belongs to the \((-1.0, 0.0)\) interval, the terms have alternate signs and converge to \(0.0\). For example, if the initial term is \(1.0\) and the ratio is \(-0.5\), the resulting gometric sequence is \(1.0, -0.5, 0.25, -0.125, ...\)
Our purpose is to compute the sum of the positive terms and the sum of negative ones separately. This must be done up to point when the difference between the absolute values of two consecutive terms becomes neglectible according to a given tolerance value. That difference is considered neglectible if its absolute value is smaller than the given tolerance.
To that goal please implement the following Python function in the gseries_alt module (gseries_alt.py file):
- gseries_alt(t0, r, eps)¶
such that
For example:
>>> eps = 0.000001 >>> r = gseries_alt(1, -0.5, eps) >>> r (1.3333, -0.6667) >>> r = gseries_alt(3, -0.5, eps) >>> r (4.0, -2.0) >>> r = gseries_alt(-1.0, -0.33, eps) >>> r (0.3703, -1.1222) >>> r = gseries_alt(1, -0.1, eps) >>> r (1.0101, -0.101)
They are available at the gseries_alt.test file.
Note
Using the
whilestatement is recommended.The given doctests are not exhaustive.