Skip to content

Commit 301d111

Browse files
ArijitKDpre-commit-ci[bot]cclauss
authored
Create logarithmic_series.py (#12344)
* 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 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update logarithmic_series.py Fixed long line issue * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update logarithmic_series.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 321cff5 commit 301d111

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@
835835
* [Harmonic](maths/series/harmonic.py)
836836
* [Harmonic Series](maths/series/harmonic_series.py)
837837
* [Hexagonal Numbers](maths/series/hexagonal_numbers.py)
838+
* [Logarithmic Series](maths/series/logarithmic_series.py)
838839
* [P Series](maths/series/p_series.py)
839840
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
840841
* [Sigmoid](maths/sigmoid.py)

maths/series/logarithmic_series.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
This is an implementation of logarithmic series in Python.
3+
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
4+
"""
5+
6+
7+
def logarithmic_series(x_value: float, n_terms: int = 5, expand: bool = False) -> list:
8+
"""
9+
Returns the logarithmic series for a number x (log x) upto n terms.
10+
11+
Parameters:
12+
x_value: a floating point number for log(x)
13+
n_terms: number of terms to be computed
14+
expand: Set this flag to get the terms as real numbers,
15+
unset for unsolved expressions
16+
17+
Examples:
18+
>>> logarithmic_series(3)
19+
['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5']
20+
21+
>>> logarithmic_series(-3)
22+
['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5']
23+
24+
>>> logarithmic_series(3, 6)
25+
['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5', '-(2^6)/6']
26+
27+
>>> logarithmic_series(3, expand=True)
28+
[2.0, -2.0, 2.6666666666666665, -4.0, 6.4]
29+
"""
30+
n_times_x_minus_1: float = x_value - 1
31+
n: int = 1
32+
series: list = []
33+
for _ in range(n_terms):
34+
if expand:
35+
series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n))
36+
n_times_x_minus_1 *= x_value - 1
37+
else:
38+
sign: str = "-" if (-1) ** (n + 1) == -1 else ""
39+
term: str = (
40+
sign + "(" + str(x_value - 1) + "^" + str(n) + ")" + "/" + str(n)
41+
)
42+
if term.startswith("-(-"):
43+
term = "(" + term[3::]
44+
elif term.startswith("(-"):
45+
term = "-(" + term[2::]
46+
series.append(term)
47+
n += 1
48+
return series
49+
50+
51+
if __name__ == "__main__":
52+
import doctest
53+
54+
doctest.testmod()

0 commit comments

Comments
 (0)