-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial_sum.py
72 lines (62 loc) · 2.52 KB
/
partial_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Calculate Partial Sums
# Hezekiah Branch
# Feb 16, 2022
class PartialSum:
# Object initialization
def __init__(self, fx=None, lower=0, upper=0, auto=False):
self.fx = fx
self.lower = lower
self.upper = upper
self.test = None
self.result = None
self.tail = None
self.zero_tail = None
self.auto = auto
# Run partial sum on auto-mode if user specifies
# else, user will call each function manually
if auto:
self.driver()
# Custom define print function
def __str__(self):
return "{}".format(self.result)
# Validate function inputted correctly
def validate_function(self):
try:
str(type(self.fx)) == "<class 'function'>"
except TypeError:
print('First parameter must be a function.')
# Validate bound inputs are numberic
def validate_bounds(self):
try:
str(self.lower).isnumeric() and str(self.upper).isnumeric()
except TypeError:
print('Upper and lower bounds must be numbers.')
# Chain input validation
def validate(self):
try:
self.validate_function() and self.validate_bounds()
except TypeError:
print("Use syntax {VAR_NAME} = PartialSum(function, lower, upper, auto=True).")
# Calculate the terms of the partial sum
def initialize(self):
self.test = [self.fx(x) for x in range(self.lower, self.upper)]
# Calculate partial sum using contiguous values
def partial_sum(self):
sum = lambda test : [self.test[0]] + [self.test[i] + self.test[i-1] for i in range(1, len(self.test))]
self.result = sum(self.test)
self.tail = self.result[-1]
if abs(0 - self.tail) < 1e-7:
self.zero_tail = True
# Driver function for the class
def driver(self):
# Validate input
self.validate()
# Begin analysis
self.initialize()
# Calculate summation
self.partial_sum()
# Output location of result
print("Final result available at .result or by calling 'print()' on PartialSum object.")
print("To view the output of f(x) for each n as n approaches infinity, use .test to view the entire list.")
print("To view the nth partial sum (i.e. the limit if input is a convergent infinite series), use .tail to view output.")
print("To observe if tail approaches zero, use .zero_tail (checks margin of 1e-7).")