Monomials¶
Monomials¶
-
class
ruffini.
Monomial
(coefficient=1, variables={}, **kwargs)¶ Bases:
object
A Monomial is the product of a coefficient and some variables.
Monomials can be added, subtracted, multiplied and divided togheter (and with numbers). lcm and gcd between monomials (and numbers) is available, too.
You can also assign a value to the variables and calculate the value of that monomial with the value you assigned.
-
__init__
(coefficient=1, variables={}, **kwargs)¶ Creates a new Monomial. The default coefficient value is 1 (so it can be omitted); variables instead are empty for default
>>> Monomial(17, k=3) 17k**3 >>> Monomial() 1
If coefficient is an instance of float but it’s a whole number (like 18.0), it will be transformed in int (in this case, 18)
>>> Monomial(7.0, a=2) 7a**2
Monomials can also be initialized by passing a dictionary (or anything similar) where are stored the variables:
>>> Monomial(2, {'x': 2, 'y': 1}) 2x**2y
Variables will be stored in a VariableDict. For more infos, see
VariablesDict.__init__()
.)Once initialized the monomial, it calculates the monomial’s total degree (which is the sum of the variables’ degrees)
>>> Monomial(-2, a=2, b=1, c=3).degree 6
Raise: ValueError, TypeError
-
similar_to
(other)¶ Checks if two monomials are similar (if the have the same variables).
>>> m = Monomial(3, x=1, y=1) >>> m.similar_to(Monomial(3.14)) False >>> m.similar_to(Monomial(2, x=1, y=1)) True
If the second operand is not a monomial the result will always be False
>>> m.similar_to("") False
When a monomial has no variables, if compared to a number the result will be True
>>> Monomial(6).similar_to(6.28) True
Return type: bool
-
has_root
(index)¶ Checks if the monomial “has” the root: the monomial 4x**2, for example, is a square, so we can say it has root 2, because (4x**2)**(1/2) is a monomial (2x).
>>> Monomial(4, x=2).has_root(2) True
We can’t say the same thing for 16a**4b:
>>> Monomial(16, a=4, b=1).has_root(2) False
Zero has all the roots
>>> Monomial(0).has_root(700) True
Raises: TypeError Return type: bool
-
root
(index)¶ Calculates the root of given index of the monomial
>>> Monomial(4, x=2).root(2) 2x >>> Monomial(-27, a=9).root(3) -3a**3 >>> Monomial(0).root(700) 0
If a monomial hasn’t a root, it raises a ValueError
>>> Monomial(5, b=2).root(3) Traceback (most recent call last): ... ValueError: this monomial hasn't root 3
To see if a monomial has a root, use
Monomial.has_root()
.Raises: ValueError Return type: Monomial
-
gcd
(other)¶ Calculates the greatest common divisor of two monomials or numbers
>>> a = Monomial(5, x=1, y=1) >>> b = Monomial(15, x=1) >>> a.gcd(b) 5x
It works only with integer coefficient/numbers different from zero
>>> a.gcd(3.14) Traceback (most recent call last): ... TypeError: Can't calculate gcd between Monomial and float >>> a.gcd(Monomial(3.14)) Traceback (most recent call last): ... ValueError: Monomial coefficient must be int >>> b.gcd(0) Traceback (most recent call last): ... ValueError: Coefficient can't be zero
The result is always positive
>>> c = Monomial(-30, x=1, y=1) >>> b.gcd(c) 15x
If you want to calculate the gcd with more factors, you can use the shorthand
gcd()
.Return type: Monomial, int Raise: TypeError, ValueError
-
lcm
(other)¶ Calculates the least common multiple of two monomials or numbers
>>> a = Monomial(2, x=1, y=1) >>> b = Monomial(-9, y=3) >>> a.lcm(b) 18xy**3
If you want to know others informations like errors and limits, please check the documentation of Monomial().gcd().
If you want to calculate the lcm between more monomials, you can use the
lcm()
shorthand.Return type: Monomial, int, float Raise: TypeError, ValueError
-
eval
(values={}, **kwargs)¶ Evaluates the monomial, giving values for each variable
>>> m = Monomial(5, x=1, y=1) >>> m.eval(x=2, y=3) 30
NB: if there are no variables left, it returns only the coefficient, as instance of `int` or `float`
You can also assign variables’ values with a dictionary (or any subclass)
>>> m.eval({'x': 2, 'y': 3}) 30
If you omit some variables’ values, those variables will remain as they were
>>> m.eval(x=2) 10y
You can declare some variables values which aren’t in the monomial and the result won’t change
>>> m.eval(b=7) 5xy
Return type: int, float, Monomial Raise: TypeError
-
__add__
(other)¶ Sums two monomials.
>>> Monomial(5, x=1, y=3) + Monomial(-1.52, x=1, y=3) 3.48xy**3
You can also sum a monomial and a number, but the result will be an instance of Polynomial.
>>> Monomial(1, z=1) + 17 z + 17
Return type: Monomial, Polynomial Raise: TypeError
-
__sub__
(other)¶ Returns the subtraction between this monomial and other
>>> Monomial(5, x=1) - Monomial(3, x=1) 2x
If the monomials are not similar or the second operator is a number, the result will be a polynomial
>>> Monomial(5, x=1, y=3) - Monomial(3, x=1) 5xy**3 - 3x >>> Monomial(17, a=1, b=1) - 2.5 17ab - 2.5
Return type: Monomial, Polynomial Raise: TypeError
-
__mul__
(other)¶ Multiplicates this monomial by other, which can be a monomial or a number
>>> Monomial(5, x=1, y=2) * Monomial(2, a=1, b=1) 10abxy**2 >>> Monomial(3, c=2) * 5 15c**2 >>> Monomial(k=3) * Monomial(k=3) k**6
Return type: Polynomial, Monomial Raise: TypeError
-
__truediv__
(other)¶ Divide this monomial by another monomial or a number
>>> Monomial(6, a=3) / Monomial(3, a=1) 2a**2 >>> Monomial(18, k=3) / 6 3k**3 >>> Monomial(27, x=6) / Monomial(3, x=6) 9
If other’s variable’s exponents are higher than this monomial’s, it raises a ValueError
>>> Monomial(5) / Monomial(4, x=1) Traceback (most recent call last): ... ValueError: variable's exponent must be positive
Return type: Monomial Raise: ValueError, TypeError
-
__pow__
(exp)¶ Raises a monomial to a given power
>>> Monomial(5, x=1) ** 2 25x**2 >>> Monomial(4, c=6) ** 3 64c**18
If the exponent is 0, the result will be 1
>>> Monomial(5, k=6) ** 0 1
It raises a TypeError if exp is an istance of float.
>>> Monomial(3.14, a=3) ** 2.5 Traceback (most recent call last): ... TypeError: unsupported operand type(s) for ** or pow(): 'Monomial' and 'float'
It raises a ValueError if the exponent is negative
>>> Monomial(17, k=1) ** (-1) Traceback (most recent call last): ... ValueError: Exponent can't be negative
Return type: Monomial Raise: ValueError, TypeError
-
__radd__
(other)¶ This method is the reverse for
Monomial.__add__()
. With this method, you can swap the two operands of the addition:>>> 18 + Monomial(3) 21
For more informations, see
Monomial.__add__()
docs.Return type: Monomial, Polynomial Raise: TypeError
-
__rsub__
(other)¶ This method is the reverse for
Monomial.__sub__()
. With this method, you can swap the two operands of the subtraction:>>> 9 - Monomial(4) 5
For more informations, see
Monomial.__sub__()
docs.Return type: Polynomial, Monomial, int, float Raise: TypeError
-
__rmul__
(other)¶ This method is the reverse for
Monomial.__mul__()
. With this method, you can swap the two operands of the multiplication:>>> 5 * Monomial(2, x=2) 10x**2
For more informations, see
Monomial.__mul__()
docs.Return type: Monomial, Polynomial Raise: TypeError
-
__rtruediv__
(other)¶ This method is the reverse for
Monomial.__truediv__()
. With this method, you can swap the two operands of the division:>>> 8 / Monomial(4) 2
For more informations, see
Monomial.__truediv__ docs()
.Return type: Monomial Raise: ValueError, TypeError
-
__str__
()¶ Returns the monomial as a string. Normally, it will return the coefficient and the variables without spaces or *.
The power is indicated with **.
Examples: >>> str(Monomial(5, x=1, y=1)) ‘5xy’ >>> str(Monomial(a=2)) ‘a**2’ >>> str(Monomial(-1, k=3)) ‘-k**3’ >>> str(Monomial(0, s=5)) ‘0’ >>> str(Monomial()) ‘1’ >>> str(Monomial(-1)) ‘-1’
Variables are displayed in alphabetical order
>>> str(Monomial(5, k=2, b=3)) '5b**3k**2'
Return type: str
-
__repr__
()¶ Returns the monomial as a string
>>> Monomial(5, x=5) 5x**5 >>> Monomial(-1, a=2, c=3) -a**2c**3
For more informations, see :func:Monomial.__str__()`.
Return type: str
-
__eq__
(other)¶ Checks if two monomials are equivalent, comparing coefficients and variables
>>> Monomial(5, x=1) == Monomial(5, x=1) True
If there are no variables, it can be compared also to a number
>>> Monomial(4) == 4 True
If the second operator isn’t a monomial or a number, it will return False.
Return type: bool Raise: TypeError
-
__neg__
()¶ Returns the opposite of the monomial inverting the coefficient
>>> - Monomial(5, x=1, y=1) -5xy
Return type: Monomial
-
__abs__
()¶ Returns the absolute value of the monomial, calculating the absolute value of the coefficient
>>> abs(Monomial(-3, a=1, b=4)) 3ab**4
Return type: Monomial
-
__hash__
()¶ Return the hash for the Monomial
The hash for 8xy, for example, is equivalent to the hash of (8, (‘x’, 1), (‘y’, 1)).
>>> hash(Monomial(8, x=1, y=1)) == hash((8, ('x', 1), ('y', 1))) True
If the monomial has no variables, its hash will be equal to the coefficient’s hash
>>> hash(Monomial(3)) == hash(3) True
Return type: int
-
gcd()¶
-
ruffini.
gcd
(*args)¶ A shorthand to calculate the gcd between two or more monomials. For more informations, see
Monomial.gcd()
lcm()¶
-
ruffini.
lcm
(*args)¶ A shorthand to calculate the lcm between two or more monomials. For more informations, see
Monomial.lcm()