Skip to content

Commit 469f0fc

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 1f03223 commit 469f0fc

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

maths/series/logarithmic_series.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,49 @@
33
Reference: https://math.stackexchange.com/questions/3973429/what-is-a-logarithmic-series
44
"""
55

6+
67
def logarithmic_series(x: float, n_terms: int = 5, expand: bool = False) -> list:
78
"""
89
Returns the logarithmic series for a number x (log x) upto n terms.
9-
10+
1011
Parameters:
1112
x: a floating point number for log(x)
1213
n_terms: number of terms to be computed
1314
expand: Set this flag to get the terms as real numbers, unset for unsolved expressions
14-
15+
1516
Examples:
1617
>>> logarithmic_series(3)
1718
['(2^1)/1', '-(2^2)/2', '(2^3)/3', '-(2^4)/4', '(2^5)/5']
18-
19+
1920
>>> logarithmic_series(-3)
2021
['-(4^1)/1', '(4^2)/2', '-(4^3)/3', '(4^4)/4', '-(4^5)/5']
21-
22+
2223
>>> logarithmic_series(3, 10)
2324
['(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']
24-
25+
2526
>>> logarithmic_series(3, expand=True)
2627
[2.0, -2.0, 2.6666666666666665, -4.0, 6.4]
2728
"""
28-
n_times_x_minus_1: float = x-1
29+
n_times_x_minus_1: float = x - 1
2930
n: int = 1
3031
series: list = []
3132
for _ in range(n_terms):
32-
if (expand):
33-
series.append(((-1)**(n+1))*(n_times_x_minus_1/n))
34-
n_times_x_minus_1 *= (x-1)
33+
if expand:
34+
series.append(((-1) ** (n + 1)) * (n_times_x_minus_1 / n))
35+
n_times_x_minus_1 *= x - 1
3536
else:
36-
sign: str = '-' if (-1)**(n+1) == -1 else ''
37-
term: str = sign+'('+str(x-1)+'^'+str(n)+')'+'/'+str(n)
38-
if (term.startswith("-(-")):
39-
term = '('+term[3::]
40-
elif (term.startswith("(-")):
41-
term = "-("+term[2::]
37+
sign: str = "-" if (-1) ** (n + 1) == -1 else ""
38+
term: str = sign + "(" + str(x - 1) + "^" + str(n) + ")" + "/" + str(n)
39+
if term.startswith("-(-"):
40+
term = "(" + term[3::]
41+
elif term.startswith("(-"):
42+
term = "-(" + term[2::]
4243
series.append(term)
4344
n += 1
4445
return series
4546

46-
if (__name__ == "__main__"):
47+
48+
if __name__ == "__main__":
4749
import doctest
50+
4851
doctest.testmod()

0 commit comments

Comments
 (0)