From 1f03223571af91424381195b5ea8618d2815b3c6 Mon Sep 17 00:00:00 2001 From: Arijit Kumar Das Date: Thu, 24 Oct 2024 01:00:15 +0530 Subject: [PATCH 1/6] Create logarithmic_series.py This is an implementation of logarithmic series in Python. Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series --- maths/series/logarithmic_series.py | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 maths/series/logarithmic_series.py diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py new file mode 100644 index 000000000000..5e0a5c47ed05 --- /dev/null +++ b/maths/series/logarithmic_series.py @@ -0,0 +1,48 @@ +""" +This is an implementation of logarithmic series in Python. +Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series +""" + +def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list: + """ + Returns the logarithmic series for a number x (log x) upto n terms. + + Parameters: + x: a floating point number for log(x) + n_terms: number of terms to be computed + expand: Set this flag to get the terms as real numbers, unset for unsolved expressions + + Examples: + >>> logarithmic_series(3) + ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5'] + + >>> logarithmic_series(-3) + ['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5'] + + >>> logarithmic_series(3, 10) + ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6', '(2^7)/7', '-(2^8)/8', '(2^9)/9', '-(2^10)/10'] + + >>> logarithmic_series(3, expand=True) + [2.0, -2.0, 2.6666666666666665, -4.0, 6.4] + """ + n_times_x_minus_1: float = x-1 + n: int = 1 + series: list = [] + for _ in range(n_terms): + if (expand): + series.append(((-1)**(n+1))*(n_times_x_minus_1/n)) + n_times_x_minus_1 *= (x-1) + else: + sign: str = '-' if (-1)**(n+1) == -1 else '' + term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n) + if (term.startswith("-(-")): + term = '('+term[3::] + elif (term.startswith("(-")): + term = "-("+term[2::] + series.append(term) + n += 1 + return series + +if (__name__ == "__main__"): + import doctest + doctest.testmod() From 469f0fc4ecc706eb0611274aeda4bd0fc2719e94 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:36:43 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/series/logarithmic_series.py | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py index 5e0a5c47ed05..5e502e4e52b5 100644 --- a/maths/series/logarithmic_series.py +++ b/maths/series/logarithmic_series.py @@ -3,46 +3,49 @@ Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series """ + def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list: """ Returns the logarithmic series for a number x (log x) upto n terms. - + Parameters: x: a floating point number for log(x) n_terms: number of terms to be computed expand: Set this flag to get the terms as real numbers, unset for unsolved expressions - + Examples: >>> logarithmic_series(3) ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5'] - + >>> logarithmic_series(-3) ['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5'] - + >>> logarithmic_series(3, 10) ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6', '(2^7)/7', '-(2^8)/8', '(2^9)/9', '-(2^10)/10'] - + >>> logarithmic_series(3, expand=True) [2.0, -2.0, 2.6666666666666665, -4.0, 6.4] """ - n_times_x_minus_1: float = x-1 + n_times_x_minus_1: float = x - 1 n: int = 1 series: list = [] for _ in range(n_terms): - if (expand): - series.append(((-1)**(n+1))*(n_times_x_minus_1/n)) - n_times_x_minus_1 *= (x-1) + if expand: + series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n)) + n_times_x_minus_1 *= x - 1 else: - sign: str = '-' if (-1)**(n+1) == -1 else '' - term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n) - if (term.startswith("-(-")): - term = '('+term[3::] - elif (term.startswith("(-")): - term = "-("+term[2::] + sign: str = "-" if (-1) ** (n + 1) == -1 else "" + term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n) + if term.startswith("-(-"): + term = "(" + term[3::] + elif term.startswith("(-"): + term = "-(" + term[2::] series.append(term) n += 1 return series -if (__name__ == "__main__"): + +if __name__ == "__main__": import doctest + doctest.testmod() From 25c5430b4725b7c6f8803acb0aa46f7437cddff4 Mon Sep 17 00:00:00 2001 From: Arijit Kumar Das Date: Thu, 24 Oct 2024 01:13:40 +0530 Subject: [PATCH 3/6] Update logarithmic_series.py Fixed long line issue --- maths/series/logarithmic_series.py | 42 ++++++++++++++---------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py index 5e502e4e52b5..b6d164da228c 100644 --- a/maths/series/logarithmic_series.py +++ b/maths/series/logarithmic_series.py @@ -3,49 +3,47 @@ Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series """ - def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list: """ Returns the logarithmic series for a number x (log x) upto n terms. - + Parameters: x: a floating point number for log(x) n_terms: number of terms to be computed - expand: Set this flag to get the terms as real numbers, unset for unsolved expressions - + expand: Set this flag to get the terms as real numbers, + unset for unsolved expressions + Examples: >>> logarithmic_series(3) ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5'] - + >>> logarithmic_series(-3) ['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5'] - - >>> logarithmic_series(3, 10) - ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6', '(2^7)/7', '-(2^8)/8', '(2^9)/9', '-(2^10)/10'] - + + >>> logarithmic_series(3, 6) + ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6'] + >>> logarithmic_series(3, expand=True) [2.0, -2.0, 2.6666666666666665, -4.0, 6.4] """ - n_times_x_minus_1: float = x - 1 + n_times_x_minus_1: float = x-1 n: int = 1 series: list = [] for _ in range(n_terms): - if expand: - series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n)) - n_times_x_minus_1 *= x - 1 + if (expand): + series.append(((-1)**(n+1))*(n_times_x_minus_1/n)) + n_times_x_minus_1 *= (x-1) else: - sign: str = "-" if (-1) ** (n + 1) == -1 else "" - term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n) - if term.startswith("-(-"): - term = "(" + term[3::] - elif term.startswith("(-"): - term = "-(" + term[2::] + sign: str = '-' if (-1)**(n+1) == -1 else '' + term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n) + if (term.startswith("-(-")): + term = '('+term[3::] + elif (term.startswith("(-")): + term = "-("+term[2::] series.append(term) n += 1 return series - -if __name__ == "__main__": +if (__name__ == "__main__"): import doctest - doctest.testmod() From 273ee447bf9ee6c9c0d3eb050c53ca29386ddfed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:44:15 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/series/logarithmic_series.py | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py index b6d164da228c..ee93b45b8daf 100644 --- a/maths/series/logarithmic_series.py +++ b/maths/series/logarithmic_series.py @@ -3,47 +3,50 @@ Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series """ + def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list: """ Returns the logarithmic series for a number x (log x) upto n terms. - + Parameters: x: a floating point number for log(x) n_terms: number of terms to be computed expand: Set this flag to get the terms as real numbers, unset for unsolved expressions - + Examples: >>> logarithmic_series(3) ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5'] - + >>> logarithmic_series(-3) ['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5'] - + >>> logarithmic_series(3, 6) ['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6'] - + >>> logarithmic_series(3, expand=True) [2.0, -2.0, 2.6666666666666665, -4.0, 6.4] """ - n_times_x_minus_1: float = x-1 + n_times_x_minus_1: float = x - 1 n: int = 1 series: list = [] for _ in range(n_terms): - if (expand): - series.append(((-1)**(n+1))*(n_times_x_minus_1/n)) - n_times_x_minus_1 *= (x-1) + if expand: + series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n)) + n_times_x_minus_1 *= x - 1 else: - sign: str = '-' if (-1)**(n+1) == -1 else '' - term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n) - if (term.startswith("-(-")): - term = '('+term[3::] - elif (term.startswith("(-")): - term = "-("+term[2::] + sign: str = "-" if (-1) ** (n + 1) == -1 else "" + term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n) + if term.startswith("-(-"): + term = "(" + term[3::] + elif term.startswith("(-"): + term = "-(" + term[2::] series.append(term) n += 1 return series -if (__name__ == "__main__"): + +if __name__ == "__main__": import doctest + doctest.testmod() From 3f47a1f5b9e83b6506b56d5bf82097464fcb816e Mon Sep 17 00:00:00 2001 From: Arijit Kumar Das Date: Fri, 1 Nov 2024 19:49:51 +0530 Subject: [PATCH 5/6] Update logarithmic_series.py --- maths/series/logarithmic_series.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py index ee93b45b8daf..1c541aec469a 100644 --- a/maths/series/logarithmic_series.py +++ b/maths/series/logarithmic_series.py @@ -4,12 +4,12 @@ """ -def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list: +def logarithmic_series(x_value: float, n_terms: int = 5, expand: bool = False) -> list: """ Returns the logarithmic series for a number x (log x) upto n terms. Parameters: - x: a floating point number for log(x) + x_value: a floating point number for log(x) n_terms: number of terms to be computed expand: Set this flag to get the terms as real numbers, unset for unsolved expressions @@ -27,16 +27,16 @@ def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list >>> logarithmic_series(3, expand=True) [2.0, -2.0, 2.6666666666666665, -4.0, 6.4] """ - n_times_x_minus_1: float = x - 1 + n_times_x_minus_1: float = x_value - 1 n: int = 1 series: list = [] for _ in range(n_terms): if expand: series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n)) - n_times_x_minus_1 *= x - 1 + n_times_x_minus_1 *= x_value - 1 else: sign: str = "-" if (-1) ** (n + 1) == -1 else "" - term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n) + term: str = sign + "(" + str(x_value - 1) + "^" + str(n) + ")" + "/" + str(n) if term.startswith("-(-"): term = "(" + term[3::] elif term.startswith("(-"): From 6777f1ebd3b997f190e27705b4d2d67f89a34dcc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:38:04 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/series/logarithmic_series.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/series/logarithmic_series.py b/maths/series/logarithmic_series.py index 1c541aec469a..c1bf7a265892 100644 --- a/maths/series/logarithmic_series.py +++ b/maths/series/logarithmic_series.py @@ -36,7 +36,9 @@ def logarithmic_series(x_value: float, n_terms: int = 5, expand: bool = False) - n_times_x_minus_1 *= x_value - 1 else: sign: str = "-" if (-1) ** (n + 1) == -1 else "" - term: str = sign + "(" + str(x_value - 1) + "^" + str(n) + ")" + "/" + str(n) + term: str = ( + sign + "(" + str(x_value - 1) + "^" + str(n) + ")" + "/" + str(n) + ) if term.startswith("-(-"): term = "(" + term[3::] elif term.startswith("(-"):