Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
src/sage/tensor/modules/free_module_basis.py: Make Basis_abstract a s…
Browse files Browse the repository at this point in the history
…ubclass of AbstractFamily
  • Loading branch information
Matthias Koeppe committed Aug 23, 2022
1 parent d2e4466 commit d15bd7d
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/sage/tensor/modules/free_module_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
# http://www.gnu.org/licenses/
#******************************************************************************

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.rings.integer_ring import ZZ
from sage.sets.family import AbstractFamily
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.sage_object import SageObject

class Basis_abstract(UniqueRepresentation, SageObject):
class Basis_abstract(UniqueRepresentation, AbstractFamily):
"""
Abstract base class for (dual) bases of free modules.
"""
Expand All @@ -54,10 +56,59 @@ def __init__(self, fmodule, symbol, latex_symbol, indices, latex_indices):
self._latex_symbol = latex_symbol
self._indices = indices
self._latex_indices = latex_indices
super().__init__(category=FiniteEnumeratedSets(), facade=fmodule)

def keys(self):
"""
Return the keys (indices) of the family.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: e = M.basis('e')
sage: list(e.keys())
[0, 1, 2]
"""
return self._fmodule.irange()

def values(self):
"""
Return the basis elements of ``self``.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: e = M.basis('e')
sage: list(e.values())
[Element e_0 of the Rank-3 free module M over the Integer Ring,
Element e_1 of the Rank-3 free module M over the Integer Ring,
Element e_2 of the Rank-3 free module M over the Integer Ring]
"""
return self._vec

def _element_constructor_(self, x):
"""
Test whether ``x`` is an element of ``self``.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: e = M.basis('e')
sage: e(e[1])
Element e_1 of the Rank-3 free module M over the Integer Ring
sage: f = M.basis('f')
sage: e(f[1])
Traceback (most recent call last):
...
ValueError: no common basis for the comparison
"""
if x in self.values():
return x
raise ValueError(f'{x} is not in {self}')

def __iter__(self):
r"""
Return the list of basis elements of ``self``.
Return an iterator for the basis elements of ``self``.
EXAMPLES::
Expand All @@ -83,8 +134,7 @@ def __iter__(self):
Element e_2 of the Rank-3 free module M1 over the Integer Ring,
Element e_3 of the Rank-3 free module M1 over the Integer Ring]
"""
for i in self._fmodule.irange():
yield self[i]
yield from self.values()

def _test_iter_len(self, **options):
r"""
Expand Down Expand Up @@ -125,6 +175,19 @@ def __len__(self):
"""
return self._fmodule._rank

def cardinality(self):
r"""
Return the basis length, i.e. the rank of the free module.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: e = M.basis('e')
sage: e.cardinality()
3
"""
return ZZ(self._fmodule._rank)

def __getitem__(self, index):
r"""
Return the basis element corresponding to a given index.
Expand Down

0 comments on commit d15bd7d

Please sign in to comment.