algebraical module
Subclass of the built-in function type for representing algebraic operators (that are typically associated with algebraic structures and algebraic circuits) as immutable, hashable, sortable, and callable objects.
Instances of the class exported by this library can be used as gate operations within circuits as they are implemented within the circuit library. This library is intended to complement the logical library for logical operations.
- class algebraical.algebraical.algebraical(function: operator, name: str)[source]
Class for representing algebraic operators. This class is derived from the type of the built-in functions found in the
operator
library. Thus, it is possible to invoke these operators on values of numeric types and on objects that define the special methods associated with these built-in operators.>>> algebraical.add_(1, 2) 3
The name and arity of an instance can be retrieved.
>>> algebraical.mul_.name() 'mul' >>> algebraical.mul_.arity() 2
Instances can be compared according to their precedence.
>>> pow_ > mul_ True >>> pow_ < add_ False >>> sorted([pow_, mul_, add_]) # From lowest to highest precedence. [add_, mul_, pow_]
Instances are also hashable and can be used as members of
set
instances and as keys withindict
instances.>>> from algebraical import * >>> {add_, add_, add_} {add_} >>> sorted({add_: 0, mul_: 1}.items()) [(add_, 0), (mul_, 1)]
- names: dict = {pos_: 'pos', neg_: 'neg', add_: 'add', sub_: 'sub', mul_: 'mul', matmul_: 'matmul', truediv_: 'truediv', floordiv_: 'floordiv', mod_: 'mod', pow_: 'pow', abs_: 'abs'}
Typical concise names for operators.
- arities: dict = {pos_: 1, neg_: 1, add_: 2, sub_: 2, mul_: 2, matmul_: 2, truediv_: 2, floordiv_: 2, mod_: 2, pow_: 2, abs_: 1}
Arities of operators.
- __call__(*arguments: Tuple[Any, ...]) Any [source]
Apply the function represented by this instance to zero or more arguments.
>>> algebraical.add_(1, 2) 3
- name() str [source]
Return the canonical concise name for this operator.
>>> algebraical.mul_.name() 'mul'
- arity() int [source]
Return the arity of this operator.
>>> algebraical.mul_.arity() 2 >>> algebraical.neg_.arity() 1
- __lt__(other: algebraical.algebraical.algebraical) bool [source]
Compare two operators according to their precedence, where an operator with lower precedence is less than an operator with higher precedence.
>>> add_ < mul_ True >>> add_ < pow_ True >>> pow_ < mul_ False >>> add_ > abs_ False
Operators that have the same precedence are not less than one another.
>>> mul_ < mul_ False >>> sub_ > add_ False
- __le__(other: algebraical.algebraical.algebraical) bool [source]
Compare two operators according to their precedence, where an operator with lower precedence is less than or equal to an operator with higher precedence.
>>> add_ <= mul_ True >>> add_ <= pow_ True >>> mul_ >= pow_ False >>> mul_ >= mul_ True
- pos_: algebraical.algebraical.algebraical = pos_
Identity operator.
>>> pos_(2) 2
- neg_: algebraical.algebraical.algebraical = neg_
Negation operator.
>>> neg_(2) -2
- abs_: algebraical.algebraical.algebraical = abs_
Absolute value operator.
>>> abs(-2) 2
- add_: algebraical.algebraical.algebraical = add_
Addition operator.
>>> add_(1, 2) 3
- sub_: algebraical.algebraical.algebraical = sub_
Subtraction operator.
>>> sub_(3, 2) 1
- mul_: algebraical.algebraical.algebraical = mul_
Multiplication operator.
>>> mul_(2, 3) 6
- matmul_: algebraical.algebraical.algebraical = matmul_
Alternative multiplication operator.
>>> class free(tuple): ... def __matmul(self, other): ... return free((self, other)) >>> isinstance(matmul_(free(), free()), free) True
- truediv_: algebraical.algebraical.algebraical = truediv_
Division operator.
>>> truediv_(4, 2) 2
- floordiv_: algebraical.algebraical.algebraical = floordiv_
Integer division operator.
>>> floordiv_(3, 2) 1
- mod_: algebraical.algebraical.algebraical = mod_
Modulus operator.
>>> mod_(7, 4) 3
- pow_: algebraical.algebraical.algebraical = pow_
Exponentiation operator.
>>> pow_(2, 3) 8