Subtract a polynomial, monomial, or scalar from this polynomial. Args: other: Value to subtract. Returns: The resulting Polynomial. Raises: ValueError: If other is not a valid type.
(self, other: int | float | Fraction | Monomial)
| 433 | ) |
| 434 | |
| 435 | def __sub__(self, other: int | float | Fraction | Monomial) -> Polynomial: |
| 436 | """Subtract a polynomial, monomial, or scalar from this polynomial. |
| 437 | |
| 438 | Args: |
| 439 | other: Value to subtract. |
| 440 | |
| 441 | Returns: |
| 442 | The resulting Polynomial. |
| 443 | |
| 444 | Raises: |
| 445 | ValueError: If other is not a valid type. |
| 446 | """ |
| 447 | if isinstance(other, (int, float, Fraction)): |
| 448 | return self.__sub__( |
| 449 | Monomial({}, Polynomial._rationalize_if_possible(other)) |
| 450 | ) |
| 451 | elif isinstance(other, Monomial): |
| 452 | monos = {m.clone() for m in self.all_monomials()} |
| 453 | for _own_monos in monos: |
| 454 | if _own_monos.equal_upto_scalar(other): |
| 455 | scalar = _own_monos.coeff |
| 456 | monos -= {_own_monos} |
| 457 | temp_variables = {i: other.variables[i] for i in other.variables} |
| 458 | monos |= { |
| 459 | Monomial( |
| 460 | temp_variables, |
| 461 | Polynomial._rationalize_if_possible(scalar - other.coeff), |
| 462 | ) |
| 463 | } |
| 464 | return Polynomial([z for z in monos]) |
| 465 | |
| 466 | to_insert = other.clone() |
| 467 | to_insert.coeff *= -1 |
| 468 | |
| 469 | monos |= {to_insert} |
| 470 | return Polynomial([z for z in monos]) |
| 471 | |
| 472 | elif isinstance(other, Polynomial): |
| 473 | p = Polynomial(list(z for z in {m.clone() for m in self.all_monomials()})) |
| 474 | for o in other.all_monomials(): |
| 475 | p = p.__sub__(o.clone()) |
| 476 | return p |
| 477 | |
| 478 | else: |
| 479 | raise ValueError( |
| 480 | "Can only subtract int, float, Fraction, " |
| 481 | "Monomials, or Polynomials from Polynomials." |
| 482 | ) |
| 483 | |
| 484 | def __mul__(self, other: int | float | Fraction | Monomial) -> Polynomial: |
| 485 | """Multiply this polynomial by another polynomial, monomial, or scalar. |
nothing calls this directly
no test coverage detected