SEPAlgebra

Author

Marcel Simader

Date

11.04.2021

New in version v0.1.1.dev0.

SEPModules.maths.SEPAlgebra.Element

Generic type Element for use in statically typing AlgebraicStructure.

alias of TypeVar(‘Element’)

SEPModules.maths.SEPAlgebra.Operator

Type alias Operator for use in typing AlgebraicStructure. Represents a Callable taking two arguments of type Element and returning an object of type Element.

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)

AlgebraicStructure is a class designed to represent a structure \((G, \circ_1, \circ_2, \ldots, \circ_n)\) with one collection containing any number of elements of type Element and 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\)

__le__()

__lt__()

__gt__()

__ge__()

__eq__()

__ne__()

Performing a boolean test using bool(algebraic_struct) (see __bool__()) will intenrally call the function is_valid() of the respective class instance.

An example usage of AlgebraicStructure could 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 instance

  • binary_operators (Operator) – a collection of callable objects taking two arguments of type Element and returning one single object of type Element representing the application of the given operator onto the objects given as arguments

  • test_for_closure (bool) – a keyword-only argument that defaults to False, see test_for_closure for details

Note

Parameter elements will internally be stored as a set object and parameter binary_operators will internally be stored as a tuple object.

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_closure is set to False by default. It may be set to True if 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_closure is set to True. 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_operators over 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_operators over 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 Element for every operator in order, if no such neutral element is found the literal NoElement is returned

Return type

Iterator[Union[List[Element], Element, NoElementType]]

find_inverses_per_operator(operator_num, element)

Finds the inverses of an element element under operator \(\circ_{operator\_num}\) stored in this instance at position operator_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 < 0 or no operator exists in this instance at position operator_num

Returns

either a list of objects of type Element, an Element object or the NoElement literal 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 True if all of the following criteria are met:

  1. cls and other are an instance of AlgebraicStructure

  2. \(G_{cls} \triangle G_{other} = \emptyset\), ie. the sets are equal in elements

  3. \(\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 cls and other return the same value if they are passed the same arguments

Returns

a boolean value describing whether cls and other are algebraically equal or not

__lt__(other)

Test whether or not this algebraic structure is a true substructure of algebraic structure other or not.

Returns

a boolean value describing whether or not cls is a true substructure of other or not

Return type

bool

__le__(other)

Test whether or not this algebraic structure is a substructure of algebraic structure other or not.

Returns

a boolean value describing whether or not cls is a substructure of other or not

Return type

bool

__gt__(other)

Test whether or not algebraic structure other is a true substructure of this algebraic structure or not.

Returns

a boolean value describing whether or not other is a true substructure of cls or not

Return type

bool

__ge__(other)

Test whether or not algebraic structure other is a substructure of this algebraic structure or not.

Returns

a boolean value describing whether or not other is a substructure of cls or 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)

Semigroup is a subclass of AlgebraicStructure and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

Unlike AlgebraicStructure, this class is can only be instantiated with one operator (e.g. s = Semigroup({...}, add) is allowed but s = 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 Semigroup instance is a valid mathematical semigroup or not. For this to be true, two conditions must be met:

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 Semigroup instance 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 literal NoElement is returned

Return type

Union[List[Element], Element, NoElementType]

find_inverses(element)

Finds the inverses of an element element under 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, an Element object or the NoElement literal 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)

Monoid is a subclass of Semigroup and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

is_valid()

Test whether or not this Monoid instance is a valid mathematical monoid or not. For this to be true, two conditions must be met:

  • cls is a valid Semigroup (ie. is associative, see Semigroup.is_valid())

  • cls has 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)

Group is a subclass of Monoid and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

is_valid()

Test whether or not this Group instance is a valid mathematical group or not. For this to be true, two conditions must be met:

  • cls is a valid Monoid (ie. is associative and has a neutral element, see Monoid.is_valid())

  • cls has 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)

AbelianGroup is a subclass of Group and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

is_valid()

Test whether or not this AbelianGroup instance is a valid mathematical Abelian group or not. For this to be true, two conditions must be met:

  • cls is a valid Group (ie. is associative, has a neutral element and inverses, see Group.is_valid())

  • cls is 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)

Ring is a subclass of AlgebraicStructure and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

Unlike AlgebraicStructure, this class is can only be instantiated with exactly two operators (e.g. ring = Ring({...}, add, mul) is allowed but ring = Ring({...}, add) is not since it can never be a valid ring anyway).

property elements_without_zero: Set[Element]

The same set as elements but without the zero element (according to the neutral element of operator \(+\)).

is_valid()

Test whether or not this Ring instance is a valid mathematical ring or not. For this to be true, four conditions must be met:

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 literal NoElement is 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 element under 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 0 or 1 for \(+\) and \(\cdot\) respectively

  • element (Element) – the element to find inverses under operator \(+\) or \(\cdot\) of

Returns

either a list of objects of type Element, an Element object or the NoElement literal 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)

Field is a subclass of Ring and 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 implements is_valid() and __bool__() (see AlgebraicStructure).

is_valid()

Test whether or not this Field instance is a valid mathematical field or not. For this to be true, four conditions must be met:

Returns

a boolean representing whether this instance is a valid field or not

Return type

bool