comch.free_module: Free Modules

comch.free_module.FreeModuleElement([data, ...])

Element in a free \(\mathbb{Z}/n \mathbb{Z}\)-module.

class comch.free_module.FreeModuleElement(data=None, torsion=None)[source]

Element in a free \(\mathbb{Z}/n \mathbb{Z}\)-module.

Let \(R\) be a ring and \(B\) a set. The free \(R\)-module generated by \(B\) consists of all \(R\)-linear combination of elements in \(B\)

\[R[B] = \Big\{ \sum_i r_ib_i\ |\ r_i \in R, b_i \in B \Big\}.\]

This class models elements in free \(\mathbb Z/n \mathbb Z\)-modules with \(\mathbb Z/0 \mathbb Z = \mathbb Z\). The ring is specified via the class attribute torsion corresponding to the non-negative integer \(n\).

torsion

The non-neg int \(n\) of the ring \(\mathbb Z/n \mathbb Z\).

Type:

int

default_torsion = 0

Used if torsion is None during initialization.

Type:

Class attribute

__init__(data=None, torsion=None)[source]

Initializes self.

Parameters:
  • data (dict) – Dictionary representing a linear combination of basis elements. Items in the dict correspond to pairs (basis_element: coefficient).

  • torsion (int) – The non-neg int \(n\) of the ring \(\mathbb Z/n \mathbb Z\).

Example

>>> print(FreeModuleElement())
0
>>> print(FreeModuleElement({'a': 1, 'b': -1, 'c': 0}))
a - b
__add__(other)[source]

Addition: self + other.

Parameters:

other (comch.free_module.FreeModuleElement object) – The element to add to self.

Returns:

The sum of self and other.

Return type:

comch.free_module.FreeModuleElement object

Example

>>> FreeModuleElement({'a': 1, 'b': 2}) + FreeModuleElement({'a': 1})
FreeModuleElement({'a': 2, 'b': 2})
__sub__(other)[source]

Diference: self - other.

Parameters:

other (comch.free_module.FreeModuleElement object) – The element to subtract from self.

Returns:

The difference of self and other.

Return type:

comch.free_module.FreeModuleElement object

Example

>>> FreeModuleElement({'a': 1, 'b': 2}) - FreeModuleElement({'a': 1})
FreeModuleElement({'b': 2})
__rmul__(c)[source]

Left action: c * self.

Parameters:

c (int) – The element to act self with.

Returns:

The action of c on self.

Return type:

comch.free_module.FreeModuleElement object

Example

>>> 3 * FreeModuleElement({'a':1, 'b':2})
FreeModuleElement({'b': 6, 'a': 3})
__truediv__(c)[source]

Division of self by c.

When c has a multiplicative inverse in the ground ring, divides self by c.

Parameters:

c (int) – The element to divide self by.

Returns:

The action of 1/c on self.

Return type:

comch.free_module.FreeModuleElement object

Example

>>> FreeModuleElement({'a': 1, 'b': 2}, torsion=5) / 3
FreeModuleElement({'b': 4, 'a': 2})
__neg__()[source]

Additive inverse: - self.

Returns:

The additive inverse of self.

Return type:

comch.free_module.FreeModuleElement object

Example

>>> - FreeModuleElement({'a': 1, 'b': 2})
FreeModuleElement({'a': -1, 'b': -2})
__iadd__(other)[source]

In place addition: self = self + other.

Parameters:

other (comch.free_module.FreeModuleElement object) – The element to add to self.

Example

>>> x = FreeModuleElement({'a': 1, 'b': 2})
>>> x += FreeModuleElement({'a': 3, 'b': 6})
>>> x
FreeModuleElement({'b': 8, 'a': 4})
__isub__(other)[source]

In place difference: self = self - other.

Parameters:

other (comch.free_module.FreeModuleElement object) – The element to subtract from self.

Example

>>> x = FreeModuleElement({'a': 1, 'b': 2})
>>> x -= FreeModuleElement({'a': 3, 'b': 6})
>>> x
FreeModuleElement({'a': -2, 'b': -4})
preferred_rep()[source]

The preferred representative of self.

Consisting of pairs basis_element: coefficient with coefficient different from 0 and in the set \(\{1, \dots, r-1\}\) if torsion is an int denoted \(r\).

Example

>>> FreeModuleElement({'a': 1, 'b': 2, 'c': 0})
FreeModuleElement({'b': 2, 'a': 1})
set_torsion(torsion)[source]

Sets the torsion of self.

Parameters:

torsion (int) – The new torsion of self

Example

>>> FreeModuleElement({'a': 1, 'b': 2}).set_torsion(2)
FreeModuleElement({'a': 1})
create(other=None)[source]

Initializes with the same type and attribute values as self.

Parameters:

other (dict or None, default: None) – Data to be initialized.

Returns:

The initialized object with the given data

Return type:

type(self) object

Example

>>> x =  FreeModuleElement({'a': 1})
>>> x + x.create({'b': 1})
FreeModuleElement({'a': 1, 'b': 1})
zero()[source]

Initializes 0 with same type and attribute values as self.

Returns:

The initialized empty object

Return type:

type(self) object

Example

>>> x = FreeModuleElement({'a': 1})
>>> x + x.zero() == x
True