AD Series

A mathematical sucsession \(t_1, t_2, ... , t_i, ...\) is an infinite sequence of numbers (usually real) such that each \(t_i\) is computed by a formula parameterized to the previous term (or a number of them). The Mathematical Series induced by a given math succession is the succession of the incremental sums of the succession’s terms. Hence, the series will be \(s_1, s_2, ... , s_n, ...\) such that

\(s_1 = t_1\)

\(s_2 = t_1 + t_2\)

\(s_3 = t_1 + t_2 + t_3\)

Let’s consider the recursive definition of a mathematical succession parameterized by the real number constants \(k\), \(a\), and the succession index \(i\) such that:

\(t_{1} = a\)

\(t_{i} = {-1}^i * \pi * \frac {k + \sqrt{|t_{i-1}|}} {2*|t_{i-1}|}\)

Implement the following two Python functions in the module ad_series (file ad_series.py).


The first function is:

ad_term(k, x, i)

such that

given

  • k, float, is the constant of the above succession

  • x, float, is the value of the \(x_{i-1}\) term of the above succession

  • i, int, \(i > 1\), the index of the term to be calculated

returns a float with the value of the \(x_i\) term of the succession calculated according to the above definition.

For example:


>>> x = ad_term(2, 1, 2)
>>> round(x, 4) 
4.7124

>>> x = ad_term(2, x, 3)
>>> round(x, 4) 
-1.3903

They are available at the ad_term-test.txt file.


The second function is:

ad_sum5\((k, a)\)

such that

given

  • k is a float with the constant of the succesion

  • a is a float value of the first term of the succession

returns a float with s_5 of the series induced by the succession above (in either words, the addition of the first 5 terms of the succession) rounded to 4 decimals.

Note

Notice that a is the first term in the definition and therefore it is part of the 5 terms to be added.

For example:


>>> ad_sum5(0, 1)
1.3945

>>> ad_sum5(2, 1)
6.2106

>>> ad_sum5(1, 2)
1.8902

Warning

This function implementation must call the previous function as many times as needed.

Doctests for validation are available at the ad_sum5-test.txt file.