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

Method inverse

algorithms/math/polynomial.py:169–183  ·  view source on GitHub ↗

Compute the multiplicative inverse of this monomial. Returns: The inverse Monomial. Raises: ValueError: If the coefficient is zero.

(self)

Source from the content-addressed store, hash-verified

167 ).clean()
168
169 def inverse(self) -> Monomial:
170 """Compute the multiplicative inverse of this monomial.
171
172 Returns:
173 The inverse Monomial.
174
175 Raises:
176 ValueError: If the coefficient is zero.
177 """
178 mono = {i: self.variables[i] for i in self.variables if self.variables[i] != 0}
179 for i in mono:
180 mono[i] *= -1
181 if self.coeff == 0:
182 raise ValueError("Coefficient must not be 0.")
183 return Monomial(mono, Monomial._rationalize_if_possible(1 / self.coeff)).clean()
184
185 def __truediv__(self, other: int | float | Fraction) -> Monomial:
186 """Divide this monomial by another monomial or scalar.

Callers 2

test_monomial_inverseMethod · 0.80
__truediv__Method · 0.80

Calls 3

MonomialClass · 0.85
cleanMethod · 0.80

Tested by 1

test_monomial_inverseMethod · 0.64