Variables¶
VariablesDicts¶
-
class
ruffini.
VariablesDict
(variables=None, **kwargs)¶ Bases:
dict
A VariablesDict is a dictionary with special features, created to manage in a better way the variables of a monomial.
In this case, we’ll call keys variables and values exponents.
The changes are:
- If a variable isn’t in the dictionary, its exponent is 0
- Therefore, variables with exponent 0 won’t be inserted
- Variables are made lowercase
- Variables must be letters from the latin alphabet and one-character long
- Exponents must be integer
NB VariablesDict is a sublass of dict, so all the methods of dict are inherited rom VariablesDict; many of these methods are not in this docs.
-
__init__
(variables=None, **kwargs)¶ Initialize the VariablesDict by giving it the pairs variable: exponent storing them in a dict (variables) or as keyword arguments:
>>> VariablesDict({'x': 5, 'y': 3}) {'x': 5, 'y': 3} >>> VariablesDict(x=5, y=3) {'x': 5, 'y': 3}
As said above, it asserts if variables are lowercase; if not they’ll be transformed automatically:
It also converts the exponent in integer if it’s a whole number
>>> VariablesDict(c=9.0) {'c': 9}
It can raise an error if:
- variable’s name is too long (ValueError)
>>> VariablesDict(xy=3) Traceback (most recent call last): ... ValueError: variable's name length must be one
- variable’s name is not alphabetical (ValueError)
>>> VariablesDict(x2=9) Traceback (most recent call last): ... ValueError: variable's name must be alphabetical
- exponent is not an integer (or a whole number) (TypeError)
>>> VariablesDict(k=[]) Traceback (most recent call last): ... TypeError: variable's exponent must be int
>>> VariablesDict(z=7.13) Traceback (most recent call last): ... TypeError: variable's exponent must be a whole number
- exponent is negative (ValueError)
>>> VariablesDict(f=-3) Traceback (most recent call last): ... ValueError: variable's exponent must be positive
After that, it checks if the dictionary is empty:
>>> VariablesDict(a=2, b=8, c=3).is_empty False >>> VariablesDict(x=0).is_empty True
Raise: TypeError, ValueError
-
__setitem__
(key, value)¶ Raises AttributeError: VariablesDict is immutable
Raise: AttributeError
-
__delitem__
(key)¶ Raises AttributeError: VariablesDict is immutable
Raise: AttributeError
-
pop
(key)¶ Raises AttributeError: VariablesDict is immutable
Raise: AttributeError
-
clear
()¶ Raises AttributeError: VariablesDict is immutable
Raise: AttributeError
-
__getitem__
(key)¶ Gets the exponent of a variable from the variable’s name
>>> v = VariablesDict(a=2, b=3) >>> v['a'] 2
If a variable isn’t in the dictionary, its value is 0
>>> v['k'] 0
Return type: int
-
__str__
()¶ Returns the dict as a string (as a normal dict)
>>> str(VariablesDict(x=5, y=3)) "{'x': 5, 'y': 3}" >>> str(VariablesDict(Y=5)) "{'y': 5}"
Variables are sorted alphabetically:
>>> str(VariablesDict(k=2, b=3)) "{'b': 3, 'k': 2}"
Return type: str
-
__repr__
()¶ Returns the dict as a string
>>> repr(VariablesDict(Y=5)) "{'y': 5}"
For more informations see
VariablesDict.__str__()
.Return type: str
-
__add__
(other)¶ Sums two VariablesDict, returning a VariablesDict whose exponents are the sum of the starting VariablesDicts’ ones
>>> VariablesDict(x=5, y=3) + VariablesDict(y=5) {'x': 5, 'y': 8} >>> VariablesDict(x=18) + VariablesDict(y=4) {'x': 18, 'y': 4} >>> VariablesDict(a=36) + VariablesDict() {'a': 36}
Return type: VariablesDict Raise: TypeError
-
__sub__
(other)¶ Return a VariablesDict whose values are the difference between the starting VariablesDicts’ ones
>>> VariablesDict(x=5, y=3) - VariablesDict(x=1, y=2) {'x': 4, 'y': 1} >>> VariablesDict(x=18) - VariablesDict(x=18) {}
If any exponent becomes negative, a ValueError will be raised instead:
>>> VariablesDict(c=2) - VariablesDict(c=3) Traceback (most recent call last): ... ValueError: variable's exponent must be positive
Return type: VariablesDict Raise: ValueError, TypeError
-
__mul__
(other)¶ Returns a VariablesDict whose exponents are this one’s, but multiplied by a given (integer) number
>>> VariablesDict(a=2, b= 5) * 3 {'a': 6, 'b': 15}
If the number is negative, a ValueError is raised
>>> VariablesDict() * (-15) Traceback (most recent call last): ... ValueError: can't multiply a VariablesDict by a negative number
Return type: VariablesDict Raise: TypeError, ValueError
-
__truediv__
(other)¶ Returns a VariablesDict whose values are this one’s, but divided by a given (integer) number
>>> VariablesDict(a=4, b=2) / 2 {'a': 2, 'b': 1}
If the VariableDict is not divisible by the given number, it will raise a ValueError
>>> VariablesDict(x=7) / 3 Traceback (most recent call last): ... ValueError: can't divide this VariablesDict by 3
To see if a VariablesDict is divisible by a number, you can use modulus operator (see more at
VariablesDict.__mod__()
):>>> VariablesDict(x=7) % 3 False
Return type: VariablesDict Raises: ValueError, TypeError
-
__mod__
(other)¶ Checks if the VariablesDict can be divided by a number (True => can be divided by other).
>>> VariablesDict(a=2, b=4) % 2 True >>> VariablesDict(a=2, b=4) % 3 False
It raises ValueError if other isn’t a positive integer
>>> VariablesDict(k=2) % (-7) Traceback (most recent call last): ... ValueError: can't use modulus with VariablesDict and negative numbers
Return type: bool Raise: TypeError
-
__hash__
()¶ Returns the hash of the VariablesDict. It’s equal to the tuple of its items.
>>> hash(VariablesDict(x=2)) == hash((('x', 2),)) True
Return type: int