Multiply two monomials or a monomial with a scalar. Args: other: A Monomial, int, float, or Fraction to multiply. Returns: The resulting Monomial. Raises: ValueError: If other is not a valid type.
(self, other: int | float | Fraction)
| 130 | return self.equal_upto_scalar(other) and self.coeff == other.coeff |
| 131 | |
| 132 | def __mul__(self, other: int | float | Fraction) -> Monomial: |
| 133 | """Multiply two monomials or a monomial with a scalar. |
| 134 | |
| 135 | Args: |
| 136 | other: A Monomial, int, float, or Fraction to multiply. |
| 137 | |
| 138 | Returns: |
| 139 | The resulting Monomial. |
| 140 | |
| 141 | Raises: |
| 142 | ValueError: If other is not a valid type. |
| 143 | """ |
| 144 | if isinstance(other, (float, int, Fraction)): |
| 145 | mono = {i: self.variables[i] for i in self.variables} |
| 146 | return Monomial( |
| 147 | mono, Monomial._rationalize_if_possible(self.coeff * other) |
| 148 | ).clean() |
| 149 | |
| 150 | if not isinstance(other, Monomial): |
| 151 | raise ValueError("Can only multiply monomials, ints, floats, or Fractions.") |
| 152 | else: |
| 153 | mono = {i: self.variables[i] for i in self.variables} |
| 154 | for i in other.variables: |
| 155 | if i in mono: |
| 156 | mono[i] += other.variables[i] |
| 157 | else: |
| 158 | mono[i] = other.variables[i] |
| 159 | |
| 160 | temp = dict() |
| 161 | for k in mono: |
| 162 | if mono[k] != 0: |
| 163 | temp[k] = mono[k] |
| 164 | |
| 165 | return Monomial( |
| 166 | temp, Monomial._rationalize_if_possible(self.coeff * other.coeff) |
| 167 | ).clean() |
| 168 | |
| 169 | def inverse(self) -> Monomial: |
| 170 | """Compute the multiplicative inverse of this monomial. |
no test coverage detected