Create a monomial with the given variables and coefficient. Args: variables: Dictionary mapping variable indices to their powers. coeff: The coefficient (defaults to 0 if empty, 1 otherwise). Examples: >>> Monomial({1: 1}) # (a_1)^1
(
self, variables: dict[int, int], coeff: int | float | Fraction | None = None
)
| 24 | """A monomial represented by a coefficient and variable-to-power mapping.""" |
| 25 | |
| 26 | def __init__( |
| 27 | self, variables: dict[int, int], coeff: int | float | Fraction | None = None |
| 28 | ) -> None: |
| 29 | """Create a monomial with the given variables and coefficient. |
| 30 | |
| 31 | Args: |
| 32 | variables: Dictionary mapping variable indices to their powers. |
| 33 | coeff: The coefficient (defaults to 0 if empty, 1 otherwise). |
| 34 | |
| 35 | Examples: |
| 36 | >>> Monomial({1: 1}) # (a_1)^1 |
| 37 | >>> Monomial({1: 3, 2: 2}, 12) # 12(a_1)^3(a_2)^2 |
| 38 | """ |
| 39 | self.variables = dict() |
| 40 | |
| 41 | if coeff is None: |
| 42 | coeff = Fraction(0, 1) if len(variables) == 0 else Fraction(1, 1) |
| 43 | elif coeff == 0: |
| 44 | self.coeff = Fraction(0, 1) |
| 45 | return |
| 46 | |
| 47 | if len(variables) == 0: |
| 48 | self.coeff = Monomial._rationalize_if_possible(coeff) |
| 49 | return |
| 50 | |
| 51 | for i in variables: |
| 52 | if variables[i] != 0: |
| 53 | self.variables[i] = variables[i] |
| 54 | self.coeff = Monomial._rationalize_if_possible(coeff) |
| 55 | |
| 56 | @staticmethod |
| 57 | def _rationalize_if_possible( |
nothing calls this directly
no test coverage detected