SEPAlgebra¶
- Author
Marcel Simader
- Date
11.04.2021
New in version v0.1.1.dev0.
- SEPModules.maths.SEPAlgebra.Element¶
Generic type
Elementfor use in statically typingAlgebraicStructure.alias of TypeVar(‘Element’)
- SEPModules.maths.SEPAlgebra.Operator¶
Type alias
Operatorfor use in typingAlgebraicStructure. Represents aCallabletaking two arguments of typeElementand returning an object of typeElement.alias of
Callable[[SEPModules.maths.SEPAlgebra.Element,SEPModules.maths.SEPAlgebra.Element],SEPModules.maths.SEPAlgebra.Element]
- SEPModules.maths.SEPAlgebra.NoElement: Final = <NoElement>¶
Singleton used to indicate that an algebraic structure does not have an element under a method or operator.
- SEPModules.maths.SEPAlgebra.NoElementType: Final = <class 'SEPModules.maths.SEPAlgebra._NoElement'>¶
A final and private class used to generate the singleton
NoElement.
Algebraic Structures¶
- class SEPModules.maths.SEPAlgebra.AlgebraicStructure(elements, *binary_operators, test_for_closure=False)¶
AlgebraicStructureis a class designed to represent a structure \((G, \circ_1, \circ_2, \ldots, \circ_n)\) with one collection containing any number of elements of typeElementand another collection of callable objects which represent the binary operators applied to the elements of \(G\).The usual relational operators can be performed on this class:
\(\leq\)
\(<\)
\(>\)
\(\geq\)
\(=\)
\(\neq\)
__ne__()Performing a boolean test using
bool(algebraic_struct)(see__bool__()) will intenrally call the functionis_valid()of the respective class instance.An example usage of
AlgebraicStructurecould look as follows, where we define an algebraic structure over the set \(\mathbb{Z}_3 = \{ 0, 1, 2 \}\) along with the binary operator \(f\colon \mathbb{Z}_3 \times \mathbb{Z}_3 \to \mathbb{Z}_3\), \(f(a, b) = a + b \mod 3\).>>> struct = AlgebraicStructure({0, 1, 2}, lambda a, b: (a + b) % 3) >>> assert bool(struct) == struct.is_valid() == True # since struct is valid >>> print(struct.neutral_elements()) # only one neutral el for our one operator [0] >>> print(struct.find_inverses_per_operator(0, 1)) # finds one inverse of element 1 for operator 0 2
- Parameters
elements (Iterable[Element]) – a collection containing n elements of any but the same type
Element, where the type given must be honored in all other uses of this instancebinary_operators (Operator) – a collection of callable objects taking two arguments of type
Elementand returning one single object of typeElementrepresenting the application of the given operator onto the objects given as argumentstest_for_closure (bool) – a keyword-only argument that defaults to False, see
test_for_closurefor details
Note
Parameter
elementswill internally be stored as asetobject and parameterbinary_operatorswill internally be stored as atupleobject.This class implements most methods by exhaustively iterating over all elements over the set, at times even in up to three nested loops, leading to very bad time complexity. More efficient and tailored solutions for these problems may be implemented as subclasses that are less general than this one.
- property elements: Set[Element]¶
A collection representing set \(G\) in this algebraic structure.
- property binary_operators: Tuple[Operator, ...]¶
A collection of binary operators representing the operators \(\circ_1, \circ_2, \ldots, \circ_n\) of this algebraic structure.
- property test_for_closure: bool¶
Determines whether this algebraic structure will test for closure or not. Turning this value off can be useful for testing infinite sets using a finite selection of elements from it. Because of this property,
test_for_closureis set toFalseby default. It may be set toTrueif an exact mathematical definition of an algebraic structure is desired.
- is_valid()¶
Test whether or not this algebraic structure is considered as valid. In this case this means that \((G, \circ_1, \circ_2, \ldots, \circ_n)\) must be closed under all operators \(\circ_n\) if the property
test_for_closureis set toTrue. Otherwise, this function will always return True.- Returns
a boolean value determining the validity of this algebraic structure
- Return type
bool
- is_associative()¶
Test whether this algebraic structure is associative for every operator \(\circ_n\) in
binary_operatorsover set \(G\).- Returns
an iterator of booleans describing for every operator whether it is associative with set \(G\) or not in order
- Return type
Iterator[bool]
- neutral_elements()¶
Try to find the neutral element for every operator \(\circ_n\) in
binary_operatorsover set \(G\). This function matches an arbitrary amount of neutral elements per operator.- Returns
an iterator of neutral elements or a list of lists of neutral elements of type
Elementfor every operator in order, if no such neutral element is found the literalNoElementis returned- Return type
Iterator[Union[List[Element], Element, NoElementType]]
- find_inverses_per_operator(operator_num, element)¶
Finds the inverses of an element
elementunder operator \(\circ_{operator\_num}\) stored in this instance at positionoperator_num. This function will return a list of possible inverses for each operator and each neutral element of said operator. Note that any neutral element may match an inverse and there is no distinction made between these cases.- Parameters
operator_num (int) – the position of operator \(\circ_{\text{operator\_num}}\) in this structure \(G\)
element (Element) – the element to find inverses under operator \(\circ_{operator\_num}\) of
- Raises
ValueError – if
operator_num < 0or no operator exists in this instance at positionoperator_num- Returns
either a list of objects of type
Element, anElementobject or theNoElementliteral if no inverses exists- Return type
Union[List[Element], Element, NoElementType]
- has_inverses()¶
Test every element of \(G\) on every operator \(\circ_n\) to see if inverses exist for each element for any neutral element of said operator. This function calls
neutral_elements()to test for inverses and aborts if the substructure trivially has no inverses.- Returns
an iterator of boolean values for each operator in order, corresponding to whether all objects have an inverse under said operator or not
- Return type
Iterator[bool]
- is_commutative()¶
Test every element in \(G\) on every operator \(\circ_n\) to see if it is commutative or not. This function keeps track of any tuple \((a, b)\) it has seen to speed up computation. Since for commutativity we need to test \(\forall a, b: a \circ_n b = b \circ_n a\), once we have seen \((a, b)\) we do not need to also test \((b, a)\).
- Returns
an iterator of boolean values corresponding to whether each operator is commutative or not in order
- Return type
Iterator[bool]
- is_closed()¶
Test whether or not set \(G\) is closed under \(\circ_n\).
- Returns
an iterator of boolean values corresponding to whether or not every operator is closed or not in order
- Return type
Iterator[bool]
- __eq__(other)¶
Returns
Trueif all of the following criteria are met:clsandotherare an instance ofAlgebraicStructure\(G_{cls} \triangle G_{other} = \emptyset\), ie. the sets are equal in elements
- \(\forall i \in \{0,\ldots,|G|-1\} \;\forall a, b \in G: a \circ^i_{cls} b = a \circ^i_{other} b\), ie.
all operators of
clsandotherreturn the same value if they are passed the same arguments
- Returns
a boolean value describing whether
clsandotherare algebraically equal or not
- __lt__(other)¶
Test whether or not this algebraic structure is a true substructure of algebraic structure
otheror not.- Returns
a boolean value describing whether or not
clsis a true substructure ofotheror not- Return type
bool
- __le__(other)¶
Test whether or not this algebraic structure is a substructure of algebraic structure
otheror not.- Returns
a boolean value describing whether or not
clsis a substructure ofotheror not- Return type
bool
- __gt__(other)¶
Test whether or not algebraic structure
otheris a true substructure of this algebraic structure or not.- Returns
a boolean value describing whether or not
otheris a true substructure ofclsor not- Return type
bool
- __ge__(other)¶
Test whether or not algebraic structure
otheris a substructure of this algebraic structure or not.- Returns
a boolean value describing whether or not
otheris a substructure ofclsor not- Return type
bool
- __bool__()¶
Returns whether this algebraic structure is valid or not (see
is_valid()).- Returns
boolean value describing whether this algebraic structure is valid or not
- Return type
bool
Semigroup¶
- class SEPModules.maths.SEPAlgebra.Semigroup(elements, binary_operator, *, test_for_closure=False)¶
Semigroupis a subclass ofAlgebraicStructureand represents an algebraic structure of form \((S, \circ)\). To test whether or not this instance is a valid monoid in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).Unlike
AlgebraicStructure, this class is can only be instantiated with one operator (e.g.s = Semigroup({...}, add)is allowed buts = Semigroup({...}, add, subtract)is not since it can never be a valid semigroup anyway).- property binary_operator: Operator¶
The single operator \(\circ\) of this algebraic structure.
- is_valid()¶
Test whether or not this
Semigroupinstance is a valid mathematical semigroup or not. For this to be true, two conditions must be met:clsis a validAlgebraicStructure(seeAlgebraicStructure.is_valid())clsis associative with \(\circ\) over \(G\)
- Returns
a boolean representing whether this instance is a valid semigroup or not
- Return type
bool
- is_associative()¶
Test whether this algebraic structure is associative for operator \(\circ\) over set \(G\).
- Returns
a boolean value describing whether this
Semigroupinstance is associative or not- Return type
bool
- neutral_elements()¶
Try to find the neutral element for operator \(\circ\) over set \(G\). This function matches an arbitrary amount of neutral elements but will almost always simply return one object of type
Element.- Returns
a list of neutral elements or a single neutral elements of type
Element, if no such neutral element is found the literalNoElementis returned- Return type
Union[List[Element], Element, NoElementType]
- find_inverses(element)¶
Finds the inverses of an element
elementunder operator \(\circ\) stored in this instance. This function will return a list of possible inverses for each neutral element of said operator. Note that any neutral element may match an inverse and there is no distinction made between these cases.- Parameters
element (Element) – the element to find inverses under operator \(\circ\) of
- Returns
either a list of objects of type
Element, anElementobject or theNoElementliteral if no inverses exists- Return type
Union[List[Element], Element, NoElementType]
- has_inverses()¶
Test every element of \(G\) on operator \(\circ\) to see if inverses exist for each element for any neutral element of said operator. This function calls
neutral_elements()to test for inverses and aborts if the substructure trivially has no inverses.- Returns
a boolean value corresponding to whether every element has an inverse or not
- Return type
bool
- is_commutative()¶
Test every element in \(G\) on operator \(\circ\) to see if it is commutative or not. See
AlgebraicStructure.is_commutative()for implementation details.- Returns
a boolean value corresponding to whether this structure is commutative or not
- Return type
bool
- is_closed()¶
Test whether or not set \(G\) is closed under \(\circ\).
- Returns
a boolean value corresponding to whether or not this structure is closed or not
- Return type
bool
Monoid¶
- class SEPModules.maths.SEPAlgebra.Monoid(elements, binary_operator, *, test_for_closure=False)¶
Monoidis a subclass ofSemigroupand represents an algebraic structure of form \((M, \circ)\). To test whether or not this instance is a valid semigroup in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).- is_valid()¶
Test whether or not this
Monoidinstance is a valid mathematical monoid or not. For this to be true, two conditions must be met:clsis a validSemigroup(ie. is associative, seeSemigroup.is_valid())clshas a neutral element \(e\) for ever element in set \(G\) over operator \(\circ\)
- Returns
a boolean representing whether this instance is a valid monoid or not
- Return type
bool
Group¶
- class SEPModules.maths.SEPAlgebra.Group(elements, binary_operator, *, test_for_closure=False)¶
Groupis a subclass ofMonoidand represents an algebraic structure of form \((G, \circ)\). To test whether or not this instance is a valid group in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).- is_valid()¶
Test whether or not this
Groupinstance is a valid mathematical group or not. For this to be true, two conditions must be met:clsis a validMonoid(ie. is associative and has a neutral element, seeMonoid.is_valid())clshas an inverse element \(a^{-1}\) for ever element in set \(G\) over operator \(\circ\)
- Returns
a boolean representing whether this instance is a valid group or not
- Return type
bool
Abelian Group¶
- class SEPModules.maths.SEPAlgebra.AbelianGroup(elements, binary_operator, *, test_for_closure=False)¶
AbelianGroupis a subclass ofGroupand represents an algebraic structure of form \((G, \circ)\). To test whether or not this instance is a valid Abelian group in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).- is_valid()¶
Test whether or not this
AbelianGroupinstance is a valid mathematical Abelian group or not. For this to be true, two conditions must be met:clsis a validGroup(ie. is associative, has a neutral element and inverses, seeGroup.is_valid())clsis commutative under operator \(\circ\)
- Returns
a boolean representing whether this instance is a valid Abelian group or not
- Return type
bool
Ring¶
- class SEPModules.maths.SEPAlgebra.Ring(elements, binary_operator_one, binary_operator_two, *, test_for_closure=False)¶
Ringis a subclass ofAlgebraicStructureand represents an algebraic structure of form \((G, +, \cdot)\). To test whether or not this instance is a valid ring in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).Unlike
AlgebraicStructure, this class is can only be instantiated with exactly two operators (e.g.ring = Ring({...}, add, mul)is allowed butring = Ring({...}, add)is not since it can never be a valid ring anyway).- property elements_without_zero: Set[Element]¶
The same set as
elementsbut without the zero element (according to the neutral element of operator \(+\)).
- is_valid()¶
Test whether or not this
Ringinstance is a valid mathematical ring or not. For this to be true, four conditions must be met:clsis a validAlgebraicStructure(seeAlgebraicStructure.is_valid())clsforms a valid Abelian group with \((G, +)\) (seeAbelianGroup.is_valid())clsforms a valid semigroup with \((G, \cdot)\) (seeSemigroup.is_valid())clsis distributive over \(+\) and \(\cdot\)
- Returns
a boolean representing whether this instance is a valid ring or not
- Return type
bool
- is_associative()¶
Test whether this algebraic structure is associative for operator \(+\) and \(\cdot\) over set \(G\).
- Returns
a tuple of two boolean values describing the associativity of either operator
- Return type
Tuple[bool, bool]
- neutral_elements()¶
Try to find the neutral element for operator \(+\) and \(\cdot\) over set \(G\). This function matches an arbitrary amount of neutral elements but will almost always simply return one object of type
Element.- Returns
a tuple of a list of neutral elements or a single neutral elements of type
Element, if no such neutral element is found the literalNoElementis returned- Return type
Tuple[Union[List[Element], Element, NoElementType], Union[List[Element], Element, NoElementType]]
- find_inverses(operator_num, element)¶
Finds the inverses of an element
elementunder operator \(+\) or \(\cdot\) stored in this instance. This function will return a list of possible inverses for each neutral element of said operator. Note that any neutral element may match an inverse and there is no distinction made between these cases.- Parameters
operator_num (Literal[0, 1]) – which operator to find an inverse for, must be either
0or1for \(+\) and \(\cdot\) respectivelyelement (Element) – the element to find inverses under operator \(+\) or \(\cdot\) of
- Returns
either a list of objects of type
Element, anElementobject or theNoElementliteral if no inverses exists- Return type
Union[List[Element], Element, NoElementType]
- has_inverses()¶
Test every element of \(G\) on operator \(\circ\) to see if inverses exist for each element for any neutral element of said operator. This function calls
neutral_elements()to test for inverses and aborts if the substructure trivially has no inverses.- Returns
a tuple of two boolean values corresponding to whether every element has an inverse under operators \(+\) and \(\cdot\) or not
- Return type
Tuple[bool, bool]
- is_commutative()¶
Test every element in \(G\) on operator \(\circ\) to see if it is commutative or not. See
AlgebraicStructure.is_commutative()for implementation details.- Returns
a tuple of boolean values corresponding to whether this structure is commutative under operators \(+\) and \(\cdot\) or not
- Return type
Tuple[bool, bool]
- is_closed()¶
Test whether or not set \(G\) is closed under \(\circ\).
- Returns
a tuple of boolean values corresponding to whether or not this structure is closed under operators \(+\) and \(\cdot\) or not
- Return type
Tuple[bool, bool]
- is_distributive()¶
Determine whether this algebraic structure is distributive for operators \(+\) and \(\cdot\) for every element in \(G\).
- Returns
whether or not this algebraic structure is distributive
- Return type
bool
Field¶
- class SEPModules.maths.SEPAlgebra.Field(elements, binary_operator_one, binary_operator_two, *, test_for_closure=False)¶
Fieldis a subclass ofRingand represents an algebraic structure of form \((G, +, \cdot)\). To test whether or not this instance is a valid field in the mathematical sense this class implementsis_valid()and__bool__()(seeAlgebraicStructure).- is_valid()¶
Test whether or not this
Fieldinstance is a valid mathematical field or not. For this to be true, four conditions must be met:clsis a validAlgebraicStructure(seeAlgebraicStructure.is_valid())clsforms a valid Abelian group with \((G, +)\) (seeAbelianGroup.is_valid())clsforms a valid Abelian group with \((G\backslash\{0\}, \cdot)\) , where \(0\) is the zero element of operator \(+\) (seeSemigroup.is_valid())clsis distributive over \(+\) and \(\cdot\)
- Returns
a boolean representing whether this instance is a valid field or not
- Return type
bool