forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_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__ import annotations
from collections.abc import MutableSequence
class Polynomial:
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.
"""
if len(coefficients) != degree + 1:
raise ValueError(
"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
"""
if self.degree > polynomial_2.degree:
coefficients = self.coefficients[:]
for i in range(polynomial_2.degree + 1):
coefficients[i] += polynomial_2.coefficients[i]
return Polynomial(self.degree, coefficients)
else:
coefficients = polynomial_2.coefficients[:]
for i in range(self.degree + 1):
coefficients[i] += self.coefficients[i]
return Polynomial(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
"""
return self + polynomial_2 * Polynomial(0, [-1])
def __neg__(self) -> Polynomial:
"""
Polynomial negation
>>> p = Polynomial(2, [1, 2, 3])
>>> -p
- 3x^2 - 2x - 1
"""
return Polynomial(self.degree, [-c for c in self.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)
for i in range(self.degree + 1):
for j in range(polynomial_2.degree + 1):
coefficients[i + j] += (
self.coefficients[i] * polynomial_2.coefficients[j]
)
return Polynomial(self.degree + polynomial_2.degree, coefficients)
def evaluate(self, substitution: float) -> float:
"""
Evaluates the polynomial at x.
>>> p = Polynomial(2, [1, 2, 3])
>>> p.evaluate(2)
17
"""
result: int | float = 0
for i in range(self.degree + 1):
result += self.coefficients[i] * (substitution**i)
return result
def __str__(self) -> str:
"""
>>> p = Polynomial(2, [1, 2, 3])
>>> print(p)
3x^2 + 2x + 1
"""
polynomial = ""
for i in range(self.degree, -1, -1):
if self.coefficients[i] == 0:
continue
elif self.coefficients[i] > 0:
if polynomial:
polynomial += " + "
else:
polynomial += " - "
if i == 0:
polynomial += str(abs(self.coefficients[i]))
elif i == 1:
polynomial += str(abs(self.coefficients[i])) + "x"
else:
polynomial += str(abs(self.coefficients[i])) + "x^" + str(i)
return polynomial
def __repr__(self) -> str:
"""
>>> p = Polynomial(2, [1, 2, 3])
>>> p
3x^2 + 2x + 1
"""
return self.__str__()
def derivative(self) -> Polynomial:
"""
Returns the derivative of the polynomial.
>>> p = Polynomial(2, [1, 2, 3])
>>> p.derivative()
6x + 2
"""
coefficients: list[float] = [0] * self.degree
for i in range(self.degree):
coefficients[i] = self.coefficients[i + 1] * (i + 1)
return Polynomial(self.degree - 1, coefficients)
def integral(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
for i in range(self.degree + 1):
coefficients[i + 1] = self.coefficients[i] / (i + 1)
return Polynomial(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
"""
if not isinstance(polynomial_2, Polynomial):
return False
if self.degree != polynomial_2.degree:
return False
for i in range(self.degree + 1):
if self.coefficients[i] != polynomial_2.coefficients[i]:
return False
return True
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
"""
return not self.__eq__(polynomial_2)