MCPcopy
hub / github.com/keon/algorithms / test_polynomial_long_division

Method test_polynomial_long_division

tests/test_polynomial.py:150–204  ·  view source on GitHub ↗

Test polynomial long division

(self)

Source from the content-addressed store, hash-verified

148 return
149
150 def test_polynomial_long_division(self):
151 """
152 Test polynomial long division
153 """
154
155 # Dividend: 4a_1^3 + 3a_1^2 - 2a_1 + 5
156 dividend = Polynomial(
157 [
158 Monomial({1: 3}, 4), # 4(a_1)^3
159 Monomial({1: 2}, 3), # 3(a_1)^2
160 Monomial({1: 1}, -2), # -2(a_1)
161 Monomial({}, 5), # +5
162 ]
163 )
164
165 # Divisor: 2a_1 - 1
166 divisor = Polynomial(
167 [
168 Monomial({1: 1}, 2), # 2(a_1)
169 Monomial({}, -1), # -1
170 ]
171 )
172
173 # Expected Quotient: 2a_1^2 + (5/2)a_1 + 1/4
174 expected_quotient = Polynomial(
175 [
176 Monomial({1: 2}, 2), # 2(a_1)^2
177 Monomial({1: 1}, Fraction(5, 2)), # (5/2)(a_1)
178 Monomial({}, Fraction(1, 4)), # +1/4
179 ]
180 )
181
182 # Expected Remainder: 21/4
183 expected_remainder = Polynomial(
184 [
185 Monomial({}, Fraction(21, 4)) # 21/4
186 ]
187 )
188
189 quotient_long_div, remainder_long_div = dividend.poly_long_division(divisor)
190
191 quotient_truediv = (
192 dividend / divisor
193 ) # Calls __truediv__, which returns only the quotient
194
195 # Check if quotient from poly_long_division matches expected
196 self.assertEqual(quotient_long_div, expected_quotient)
197
198 # Check if remainder from poly_long_division matches expected
199 self.assertEqual(remainder_long_div, expected_remainder)
200
201 # Check if quotient from __truediv__ matches quotient from poly_long_division
202 self.assertEqual(quotient_truediv, quotient_long_div)
203
204 return

Callers

nothing calls this directly

Calls 3

poly_long_divisionMethod · 0.95
PolynomialClass · 0.90
MonomialClass · 0.90

Tested by

no test coverage detected