-
Notifications
You must be signed in to change notification settings - Fork 0
/
isj_proj6_xturyt00.py
167 lines (137 loc) · 4.97 KB
/
isj_proj6_xturyt00.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
class Polynomial():
def __init__(self,*args,**kwargs):
"""
Constructor for Polynomial class
a,b,c and d is a number
Arguments as a list: [a,b, ...]
Arguments as a sequence: a,b, ...
Arguments as a dictionary : x1 = a, x2 = b ...
"""
# if the argument is an array
if len(args) > 0:
if isinstance(args[0],list):
self.args = args[0]
else:
self.args = list(args)
elif len(kwargs.values()) > 0: # if the argument is a dictionary
keys = list(kwargs.keys())
#finds max key
max_key = max([int(key[1:]) for key in keys])
# array init with max index + 1
self.args = [0]*(max_key+1)
# assign values to the list
for key in keys:
self.args[int(key[1:])] = kwargs[key]
else:
self.args = [0]
#removes all the 0 at the end
while len(self.args) > 0:
if self.args[-1] == 0:
self.args.pop()
else:
break
def __str__(self):
"""
Converts list into polynom string
return value is a string
Example: [1,2] => "2x + 1"
"""
res = ''
# returns 0 if polynom is empty
if len(self.args) == 0:
return '0'
for index in range(len(self.args)):
# skips 0
if self.args[index] == 0:
continue
temp_val=''
# removes "1x"
if abs(self.args[index]) == 1:
temp_val = '' if index > 0 else '1'
else:
temp_val = str(abs(self.args[index])) if abs(self.args[index]) != 1 else '1'
# adds X to a coefficient
if index == 0:
res = temp_val
elif index == 1:
res = temp_val + 'x' + res
else:
res = temp_val + f'x^{index}' + res
# places signs
if len(self.args) - 1 != index:
res = (' + ' if self.args[index] > 0 else ' - ') + res
else:
if self.args[index] < 0:
res = '- ' + res
return res
# compares polynoms
def __eq__(self, target):
"""
Compares 2 polynoms
return value is a bollean True or False
Example: 2x + 1 == 2x + 1 => True
"""
return self.__str__() == target.__str__()
def __add__(self, target):
"""
Adds up 2 polynoms
return value is new Polynomial object with the result
Example: (2x + 1) + (x - 1)
"""
new = []
# fins smaller polynom
min_len = min(len(self.args),len(target.args))
# adds up elements of the polynoms [small first]
new = [self.args[index] + target.args[index] for index in range(min_len)]
# copies the rest
new = [*new, *self.args[min_len:]] if len(self.args) > len(target.args) else [*new, *target.args[min_len:]]
return Polynomial(new)
def __pow__(self,pow_size):
"""
Powers polynom by n
return value is a Polynomial object with the result
Example: (2x + 1)^2 => 4x^2 + 4x + 1
"""
def to_pow(args,pow_size):
new_args = [0]* len(args) * 2
old_args = [*self.args,*[0]*(len(new_args)-len(self.args))]
if pow_size == 1:
return args
for index_mul in range(len(args)):
for index in range(len(args)):
new_args[index+index_mul] += old_args[index] * args[index_mul]
return to_pow(new_args,pow_size-1)
return Polynomial(to_pow(self.args,pow_size))
def derivative(self):
"""
Finds the derivative of a polynomial
return value is a string Polynom
Example: x^2 + 2x + 1 => "2x + 2"
"""
# return object
new = []
# shifts array to the left by 1 to remove first item
for index in range(len(self.args)):
if index > 0:
new.append(self.args[index]*index) # derivates
return Polynomial(new)
def at_value(self, x_1, x_2 = None):
"""
Calculates the result of polynom with x = x1,
or calculates delta X = f(x2) - f(x1)
Arguments:
x1 - number
x2 - number (optional)
return value is a number
"""
def solve_polynom(polynom,x_var):
"""
Calculates the result of polynom,
Arguments:
x - number
return value is a number
Example: x^2 + 2x + 1 and x = 1 => 4
"""
return sum(polynom[index] * (x_var**index) for index in range(len(polynom)))
return solve_polynom(self.args,x_1) if x_2 is None else solve_polynom(self.args,x_2) - solve_polynom(self.args,x_1)