|
| 1 | +""" |
| 2 | +
|
| 3 | +This module implements a single indeterminate polynomials class |
| 4 | +with some basic operations |
| 5 | +
|
| 6 | +Reference: https://en.wikipedia.org/wiki/Polynomial |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from collections.abc import MutableSequence |
| 13 | + |
| 14 | + |
| 15 | +class Polynomial: |
| 16 | + def __init__(self, degree: int, coefficients: MutableSequence[float]) -> None: |
| 17 | + """ |
| 18 | + The coefficients should be in order of degree, from smallest to largest. |
| 19 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 20 | + >>> p = Polynomial(2, [1, 2, 3, 4]) |
| 21 | + Traceback (most recent call last): |
| 22 | + ... |
| 23 | + ValueError: The number of coefficients should be equal to the degree + 1. |
| 24 | +
|
| 25 | + """ |
| 26 | + if len(coefficients) != degree + 1: |
| 27 | + raise ValueError( |
| 28 | + "The number of coefficients should be equal to the degree + 1." |
| 29 | + ) |
| 30 | + |
| 31 | + self.coefficients: list[float] = list(coefficients) |
| 32 | + self.degree = degree |
| 33 | + |
| 34 | + def __add__(self, polynomial_2: Polynomial) -> Polynomial: |
| 35 | + """ |
| 36 | + Polynomial addition |
| 37 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 38 | + >>> q = Polynomial(2, [1, 2, 3]) |
| 39 | + >>> p + q |
| 40 | + 6x^2 + 4x + 2 |
| 41 | + """ |
| 42 | + |
| 43 | + if self.degree > polynomial_2.degree: |
| 44 | + coefficients = self.coefficients[:] |
| 45 | + for i in range(polynomial_2.degree + 1): |
| 46 | + coefficients[i] += polynomial_2.coefficients[i] |
| 47 | + return Polynomial(self.degree, coefficients) |
| 48 | + else: |
| 49 | + coefficients = polynomial_2.coefficients[:] |
| 50 | + for i in range(self.degree + 1): |
| 51 | + coefficients[i] += self.coefficients[i] |
| 52 | + return Polynomial(polynomial_2.degree, coefficients) |
| 53 | + |
| 54 | + def __sub__(self, polynomial_2: Polynomial) -> Polynomial: |
| 55 | + """ |
| 56 | + Polynomial subtraction |
| 57 | + >>> p = Polynomial(2, [1, 2, 4]) |
| 58 | + >>> q = Polynomial(2, [1, 2, 3]) |
| 59 | + >>> p - q |
| 60 | + 1x^2 |
| 61 | + """ |
| 62 | + return self + polynomial_2 * Polynomial(0, [-1]) |
| 63 | + |
| 64 | + def __neg__(self) -> Polynomial: |
| 65 | + """ |
| 66 | + Polynomial negation |
| 67 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 68 | + >>> -p |
| 69 | + - 3x^2 - 2x - 1 |
| 70 | + """ |
| 71 | + return Polynomial(self.degree, [-c for c in self.coefficients]) |
| 72 | + |
| 73 | + def __mul__(self, polynomial_2: Polynomial) -> Polynomial: |
| 74 | + """ |
| 75 | + Polynomial multiplication |
| 76 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 77 | + >>> q = Polynomial(2, [1, 2, 3]) |
| 78 | + >>> p * q |
| 79 | + 9x^4 + 12x^3 + 10x^2 + 4x + 1 |
| 80 | + """ |
| 81 | + coefficients: list[float] = [0] * (self.degree + polynomial_2.degree + 1) |
| 82 | + for i in range(self.degree + 1): |
| 83 | + for j in range(polynomial_2.degree + 1): |
| 84 | + coefficients[i + j] += ( |
| 85 | + self.coefficients[i] * polynomial_2.coefficients[j] |
| 86 | + ) |
| 87 | + |
| 88 | + return Polynomial(self.degree + polynomial_2.degree, coefficients) |
| 89 | + |
| 90 | + def evaluate(self, substitution: int | float) -> int | float: |
| 91 | + """ |
| 92 | + Evaluates the polynomial at x. |
| 93 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 94 | + >>> p.evaluate(2) |
| 95 | + 17 |
| 96 | + """ |
| 97 | + result: int | float = 0 |
| 98 | + for i in range(self.degree + 1): |
| 99 | + result += self.coefficients[i] * (substitution**i) |
| 100 | + return result |
| 101 | + |
| 102 | + def __str__(self) -> str: |
| 103 | + """ |
| 104 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 105 | + >>> print(p) |
| 106 | + 3x^2 + 2x + 1 |
| 107 | + """ |
| 108 | + polynomial = "" |
| 109 | + for i in range(self.degree, -1, -1): |
| 110 | + if self.coefficients[i] == 0: |
| 111 | + continue |
| 112 | + elif self.coefficients[i] > 0: |
| 113 | + if polynomial: |
| 114 | + polynomial += " + " |
| 115 | + else: |
| 116 | + polynomial += " - " |
| 117 | + |
| 118 | + if i == 0: |
| 119 | + polynomial += str(abs(self.coefficients[i])) |
| 120 | + elif i == 1: |
| 121 | + polynomial += str(abs(self.coefficients[i])) + "x" |
| 122 | + else: |
| 123 | + polynomial += str(abs(self.coefficients[i])) + "x^" + str(i) |
| 124 | + |
| 125 | + return polynomial |
| 126 | + |
| 127 | + def __repr__(self) -> str: |
| 128 | + """ |
| 129 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 130 | + >>> p |
| 131 | + 3x^2 + 2x + 1 |
| 132 | + """ |
| 133 | + return self.__str__() |
| 134 | + |
| 135 | + def derivative(self) -> Polynomial: |
| 136 | + """ |
| 137 | + Returns the derivative of the polynomial. |
| 138 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 139 | + >>> p.derivative() |
| 140 | + 6x + 2 |
| 141 | + """ |
| 142 | + coefficients: list[float] = [0] * self.degree |
| 143 | + for i in range(self.degree): |
| 144 | + coefficients[i] = self.coefficients[i + 1] * (i + 1) |
| 145 | + return Polynomial(self.degree - 1, coefficients) |
| 146 | + |
| 147 | + def integral(self, constant: int | float = 0) -> Polynomial: |
| 148 | + """ |
| 149 | + Returns the integral of the polynomial. |
| 150 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 151 | + >>> p.integral() |
| 152 | + 1.0x^3 + 1.0x^2 + 1.0x |
| 153 | + """ |
| 154 | + coefficients: list[float] = [0] * (self.degree + 2) |
| 155 | + coefficients[0] = constant |
| 156 | + for i in range(self.degree + 1): |
| 157 | + coefficients[i + 1] = self.coefficients[i] / (i + 1) |
| 158 | + return Polynomial(self.degree + 1, coefficients) |
| 159 | + |
| 160 | + def __eq__(self, polynomial_2: object) -> bool: |
| 161 | + """ |
| 162 | + Checks if two polynomials are equal. |
| 163 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 164 | + >>> q = Polynomial(2, [1, 2, 3]) |
| 165 | + >>> p == q |
| 166 | + True |
| 167 | + """ |
| 168 | + if not isinstance(polynomial_2, Polynomial): |
| 169 | + return False |
| 170 | + |
| 171 | + if self.degree != polynomial_2.degree: |
| 172 | + return False |
| 173 | + |
| 174 | + for i in range(self.degree + 1): |
| 175 | + if self.coefficients[i] != polynomial_2.coefficients[i]: |
| 176 | + return False |
| 177 | + |
| 178 | + return True |
| 179 | + |
| 180 | + def __ne__(self, polynomial_2: object) -> bool: |
| 181 | + """ |
| 182 | + Checks if two polynomials are not equal. |
| 183 | + >>> p = Polynomial(2, [1, 2, 3]) |
| 184 | + >>> q = Polynomial(2, [1, 2, 3]) |
| 185 | + >>> p != q |
| 186 | + False |
| 187 | + """ |
| 188 | + return not self.__eq__(polynomial_2) |
0 commit comments