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

Every coefficient will be transformed in an instance of Fraction

>>> type(Monomial(7, a=2).coefficient)
<class 'fractions.Fraction'>

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(Fraction(1, 3))
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 that aren’t equal to zero

>>> a.gcd(3.14)
Traceback (most recent call last):
...
ValueError: Monomial coefficient must be a whole number
>>> a.gcd(Monomial(3.14))
Traceback (most recent call last):
...
ValueError: Monomial coefficient must be a whole number
>>> 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, Fraction
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 more, please check Monomial.gcd().

If you want to calculate the lcm between more monomials, you can use the lcm() shorthand.

Return type:Monomial, Fraction
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)
Fraction(30, 1)

NB: if there are no variables left, it returns only the coefficient, as instance of :class:`Fraction`.

You can also assign variables’ values with a dictionary (or any subclass)

>>> m.eval({'x': 2, 'y': 3})
Fraction(30, 1)

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

The value for a variable can be a Monomial as well

>>> m.eval(x=Monomial(3, y=1))
15y**2
Return type:int, float, Monomial
Raise:TypeError
__add__(other)

Sums two monomials

>>> Monomial(5, x=1, y=3) + Monomial(-1.5, x=1, y=3)
7/2xy**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)

Subtracts two monomials

>>> Monomial(5, x=1) - Monomial(3, x=1)
2x

If the monomials are not similar or the second operand 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 - 5/2
Return type:Monomial, Polynomial
Raise:TypeError
__mul__(other)

Multiplicates two monomials or a monomial and a number

>>> Monomial(5, x=1, y=2) * Monomial(2, a=1, b=1)
10abxy**2
>>> Monomial(3, c=2) * 5
15c**2
Return type:Polynomial, Monomial
Raise:TypeError
__truediv__(other)

Divides two monomials or a monomial and a number

>>> Monomial(6, a=3) / Monomial(3, a=1)
2a**2
>>> Monomial(18, k=3) / 6
3k**3

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

If the exponent is 0, the result will be 1

>>> Monomial(5, k=6) ** 0
1

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

It raises a TypeError if exp isn’t an istance of int.

>>> Monomial(3.14, a=3) ** 2.5
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for ** or pow(): 'Monomial' and 'float'

To calculate roots, use Monomial.root().

Return type:Monomial
Raise:ValueError, TypeError
__radd__(other)

Reverse for Monomial.__add__()

>>> 18 + Monomial(3)
21

For more informations, see Monomial.__add__() docs.

Return type:Monomial, Polynomial
Raise:TypeError
__rsub__(other)

Reverse for Monomial.__sub__()

>>> 9 - Monomial(4)
5

For more informations, see Monomial.__sub__() docs.

Return type:Polynomial, Monomial
Raise:TypeError
__rmul__(other)

Reverse for Monomial.__mul__()

>>> 5 * Monomial(2, x=2)
10x**2

For more informations, see Monomial.__mul__() docs.

Return type:Monomial, Polynomial
Raise:TypeError
__rtruediv__(other)

Reverse for Monomial.__truediv__()

>>> 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__()

Return the monomial as a string

>>> Monomial(5, x=5)
5x**5

For more informations, see :func:Monomial.__str__()`.

Return type:str
__eq__(other)

Checks if two monomials are equivalent

>>> 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 operand isn’t a monomial or a number, it will return False.

It works by comparing the hashes calculated with Monomial.__hash__().

Return type:bool
Raise:TypeError
__neg__()

Returns the opposite of the monomial

>>> - Monomial(5, x=1, y=1)
-5xy
Return type:Monomial
__abs__()

Returns the absolute value of the monomial

>>> abs(Monomial(-3, a=1, b=4))
3ab**4
Return type:Monomial
__hash__()

Returns 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)

Returns the gcd between two or more monomials. For more informations, see Monomial.gcd()

lcm()

ruffini.lcm(*args)

Returns the lcm between two or more monomials. For more informations, see Monomial.lcm()