- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathsingle_indeterminate_operations.py
188 lines (161 loc) · 5.7 KB
/
single_indeterminate_operations.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
This module implements a single indeterminate polynomials class
with some basic operations
Reference: https://en.wikipedia.org/wiki/Polynomial
"""
from __future__ importannotations
fromcollections.abcimportMutableSequence
classPolynomial:
def__init__(self, degree: int, coefficients: MutableSequence[float]) ->None:
"""
The coefficients should be in order of degree, from smallest to largest.
>>> p = Polynomial(2, [1, 2, 3])
>>> p = Polynomial(2, [1, 2, 3, 4])
Traceback (most recent call last):
...
ValueError: The number of coefficients should be equal to the degree + 1.
"""
iflen(coefficients) !=degree+1:
raiseValueError(
"The number of coefficients should be equal to the degree + 1."
)
self.coefficients: list[float] =list(coefficients)
self.degree=degree
def__add__(self, polynomial_2: Polynomial) ->Polynomial:
"""
Polynomial addition
>>> p = Polynomial(2, [1, 2, 3])
>>> q = Polynomial(2, [1, 2, 3])
>>> p + q
6x^2 + 4x + 2
"""
ifself.degree>polynomial_2.degree:
coefficients=self.coefficients[:]
foriinrange(polynomial_2.degree+1):
coefficients[i] +=polynomial_2.coefficients[i]
returnPolynomial(self.degree, coefficients)
else:
coefficients=polynomial_2.coefficients[:]
foriinrange(self.degree+1):
coefficients[i] +=self.coefficients[i]
returnPolynomial(polynomial_2.degree, coefficients)
def__sub__(self, polynomial_2: Polynomial) ->Polynomial:
"""
Polynomial subtraction
>>> p = Polynomial(2, [1, 2, 4])
>>> q = Polynomial(2, [1, 2, 3])
>>> p - q
1x^2
"""
returnself+polynomial_2*Polynomial(0, [-1])
def__neg__(self) ->Polynomial:
"""
Polynomial negation
>>> p = Polynomial(2, [1, 2, 3])
>>> -p
- 3x^2 - 2x - 1
"""
returnPolynomial(self.degree, [-cforcinself.coefficients])
def__mul__(self, polynomial_2: Polynomial) ->Polynomial:
"""
Polynomial multiplication
>>> p = Polynomial(2, [1, 2, 3])
>>> q = Polynomial(2, [1, 2, 3])
>>> p * q
9x^4 + 12x^3 + 10x^2 + 4x + 1
"""
coefficients: list[float] = [0] * (self.degree+polynomial_2.degree+1)
foriinrange(self.degree+1):
forjinrange(polynomial_2.degree+1):
coefficients[i+j] += (
self.coefficients[i] *polynomial_2.coefficients[j]
)
returnPolynomial(self.degree+polynomial_2.degree, coefficients)
defevaluate(self, substitution: float) ->float:
"""
Evaluates the polynomial at x.
>>> p = Polynomial(2, [1, 2, 3])
>>> p.evaluate(2)
17
"""
result: int|float=0
foriinrange(self.degree+1):
result+=self.coefficients[i] * (substitution**i)
returnresult
def__str__(self) ->str:
"""
>>> p = Polynomial(2, [1, 2, 3])
>>> print(p)
3x^2 + 2x + 1
"""
polynomial=""
foriinrange(self.degree, -1, -1):
ifself.coefficients[i] ==0:
continue
elifself.coefficients[i] >0:
ifpolynomial:
polynomial+=" + "
else:
polynomial+=" - "
ifi==0:
polynomial+=str(abs(self.coefficients[i]))
elifi==1:
polynomial+=str(abs(self.coefficients[i])) +"x"
else:
polynomial+=str(abs(self.coefficients[i])) +"x^"+str(i)
returnpolynomial
def__repr__(self) ->str:
"""
>>> p = Polynomial(2, [1, 2, 3])
>>> p
3x^2 + 2x + 1
"""
returnself.__str__()
defderivative(self) ->Polynomial:
"""
Returns the derivative of the polynomial.
>>> p = Polynomial(2, [1, 2, 3])
>>> p.derivative()
6x + 2
"""
coefficients: list[float] = [0] *self.degree
foriinrange(self.degree):
coefficients[i] =self.coefficients[i+1] * (i+1)
returnPolynomial(self.degree-1, coefficients)
defintegral(self, constant: float=0) ->Polynomial:
"""
Returns the integral of the polynomial.
>>> p = Polynomial(2, [1, 2, 3])
>>> p.integral()
1.0x^3 + 1.0x^2 + 1.0x
"""
coefficients: list[float] = [0] * (self.degree+2)
coefficients[0] =constant
foriinrange(self.degree+1):
coefficients[i+1] =self.coefficients[i] / (i+1)
returnPolynomial(self.degree+1, coefficients)
def__eq__(self, polynomial_2: object) ->bool:
"""
Checks if two polynomials are equal.
>>> p = Polynomial(2, [1, 2, 3])
>>> q = Polynomial(2, [1, 2, 3])
>>> p == q
True
"""
ifnotisinstance(polynomial_2, Polynomial):
returnFalse
ifself.degree!=polynomial_2.degree:
returnFalse
foriinrange(self.degree+1):
ifself.coefficients[i] !=polynomial_2.coefficients[i]:
returnFalse
returnTrue
def__ne__(self, polynomial_2: object) ->bool:
"""
Checks if two polynomials are not equal.
>>> p = Polynomial(2, [1, 2, 3])
>>> q = Polynomial(2, [1, 2, 3])
>>> p != q
False
"""
returnnotself.__eq__(polynomial_2)