Abstract base class for modules#
AUTHORS:
William Stein: initial version
Julian Rueth (2014-05-10): category parameter for Module, doc cleanup
EXAMPLES:
A minimal example of a module:
sage: from sage.structure.richcmp import richcmp
sage: class MyElement(sage.structure.element.ModuleElement):
....: def __init__(self, parent, x):
....: self.x = x
....: sage.structure.element.ModuleElement.__init__(self, parent=parent)
....: def _lmul_(self, c):
....: return self.parent()(c*self.x)
....: def _add_(self, other):
....: return self.parent()(self.x + other.x)
....: def _richcmp_(self, other, op):
....: return richcmp(self.x, other.x, op)
....: def __hash__(self):
....: return hash(self.x)
....: def _repr_(self):
....: return repr(self.x)
sage: from sage.modules.module import Module
sage: class MyModule(Module):
....: Element = MyElement
....: def _element_constructor_(self, x):
....: if isinstance(x, MyElement): x = x.x
....: return self.element_class(self, self.base_ring()(x))
....: def __eq__(self, other):
....: if not isinstance(other, MyModule): return False
....: return self.base_ring() == other.base_ring()
....: def __hash__(self):
....: return hash(self.base_ring())
sage: M = MyModule(QQ)
sage: M(1)
1
sage: import __main__
sage: __main__.MyModule = MyModule
sage: __main__.MyElement = MyElement
sage: TestSuite(M).run()
>>> from sage.all import *
>>> from sage.structure.richcmp import richcmp
>>> class MyElement(sage.structure.element.ModuleElement):
... def __init__(self, parent, x):
... self.x = x
... sage.structure.element.ModuleElement.__init__(self, parent=parent)
... def _lmul_(self, c):
... return self.parent()(c*self.x)
... def _add_(self, other):
... return self.parent()(self.x + other.x)
... def _richcmp_(self, other, op):
... return richcmp(self.x, other.x, op)
... def __hash__(self):
... return hash(self.x)
... def _repr_(self):
... return repr(self.x)
>>> from sage.modules.module import Module
>>> class MyModule(Module):
... Element = MyElement
... def _element_constructor_(self, x):
... if isinstance(x, MyElement): x = x.x
... return self.element_class(self, self.base_ring()(x))
... def __eq__(self, other):
... if not isinstance(other, MyModule): return False
... return self.base_ring() == other.base_ring()
... def __hash__(self):
... return hash(self.base_ring())
>>> M = MyModule(QQ)
>>> M(Integer(1))
1
>>> import __main__
>>> __main__.MyModule = MyModule
>>> __main__.MyElement = MyElement
>>> TestSuite(M).run()
- class sage.modules.module.Module[source]#
Bases:
Parent
Generic module class.
INPUT:
base
– a ring. The base ring of the module.category
– a category (default:None
), the category for this module. IfNone
, then this is set to the category of modules/vector spaces overbase
.names
– names of generators
EXAMPLES:
sage: from sage.modules.module import Module sage: M = Module(ZZ) sage: M.base_ring() Integer Ring sage: M.category() Category of modules over Integer Ring
>>> from sage.all import * >>> from sage.modules.module import Module >>> M = Module(ZZ) >>> M.base_ring() Integer Ring >>> M.category() Category of modules over Integer Ring
Normally the category is set to the category of modules over
base
. Ifbase
is a field, then the category is the category of vector spaces overbase
:sage: M_QQ = Module(QQ) sage: M_QQ.category() Category of vector spaces over Rational Field
>>> from sage.all import * >>> M_QQ = Module(QQ) >>> M_QQ.category() Category of vector spaces over Rational Field
The
category
parameter can be used to set a more specific category:sage: N = Module(ZZ, category=FiniteDimensionalModulesWithBasis(ZZ)) sage: N.category() Category of finite dimensional modules with basis over Integer Ring
>>> from sage.all import * >>> N = Module(ZZ, category=FiniteDimensionalModulesWithBasis(ZZ)) >>> N.category() Category of finite dimensional modules with basis over Integer Ring
- base_extend(R)[source]#
Return the base extension of
self
to \(R\).This is the same as
self.change_ring(R)
except that aTypeError
is raised if there is no canonical coerce map from the base ring ofself
to \(R\).INPUT:
R
– ring
EXAMPLES:
sage: V = ZZ^7 # needs sage.modules sage: V.base_extend(QQ) # needs sage.modules Vector space of dimension 7 over Rational Field
>>> from sage.all import * >>> V = ZZ**Integer(7) # needs sage.modules >>> V.base_extend(QQ) # needs sage.modules Vector space of dimension 7 over Rational Field
- change_ring(R)[source]#
Return the base change of
self
to \(R\).EXAMPLES:
sage: from sage.modular.modform.space import ModularFormsSpace # needs sage.modular sage: ModularFormsSpace(Gamma0(11), 2, # needs sage.modular sage.rings.finite_rings ....: DirichletGroup(1)[0], QQ).change_ring(GF(7)) Traceback (most recent call last): ... NotImplementedError: the method change_ring() has not yet been implemented
>>> from sage.all import * >>> from sage.modular.modform.space import ModularFormsSpace # needs sage.modular >>> ModularFormsSpace(Gamma0(Integer(11)), Integer(2), # needs sage.modular sage.rings.finite_rings ... DirichletGroup(Integer(1))[Integer(0)], QQ).change_ring(GF(Integer(7))) Traceback (most recent call last): ... NotImplementedError: the method change_ring() has not yet been implemented
- endomorphism_ring()[source]#
Return the endomorphism ring of this module in its category.
EXAMPLES:
sage: from sage.modules.module import Module sage: M = Module(ZZ) sage: M.endomorphism_ring() Set of Morphisms from <sage.modules.module.Module object at ...> to <sage.modules.module.Module object at ...> in Category of modules over Integer Ring
>>> from sage.all import * >>> from sage.modules.module import Module >>> M = Module(ZZ) >>> M.endomorphism_ring() Set of Morphisms from <sage.modules.module.Module object at ...> to <sage.modules.module.Module object at ...> in Category of modules over Integer Ring
- sage.modules.module.is_Module(x)[source]#
Return
True
ifx
is a module,False
otherwise.INPUT:
x
– anything.
EXAMPLES:
sage: from sage.modules.module import is_Module sage: M = FreeModule(RationalField(),30) # needs sage.modules sage: is_Module(M) # needs sage.modules doctest:warning... DeprecationWarning: the function is_Module is deprecated; use 'isinstance(..., Module)' instead See https://github.com/sagemath/sage/issues/37924 for details. True sage: is_Module(10) False
>>> from sage.all import * >>> from sage.modules.module import is_Module >>> M = FreeModule(RationalField(),Integer(30)) # needs sage.modules >>> is_Module(M) # needs sage.modules doctest:warning... DeprecationWarning: the function is_Module is deprecated; use 'isinstance(..., Module)' instead See https://github.com/sagemath/sage/issues/37924 for details. True >>> is_Module(Integer(10)) False
- sage.modules.module.is_VectorSpace(x)[source]#
Return
True
ifx
is a vector space,False
otherwise.INPUT:
x
– anything.
EXAMPLES:
sage: # needs sage.modules sage: from sage.modules.module import is_Module, is_VectorSpace sage: M = FreeModule(RationalField(),30) sage: is_VectorSpace(M) doctest:warning... DeprecationWarning: the function is_VectorSpace is deprecated; use 'isinstance(..., Module)' and check the base ring instead See https://github.com/sagemath/sage/issues/37924 for details. True sage: M = FreeModule(IntegerRing(),30) sage: is_Module(M) doctest:warning... DeprecationWarning: the function is_Module is deprecated; use 'isinstance(..., Module)' instead See https://github.com/sagemath/sage/issues/37924 for details. True sage: is_VectorSpace(M) False
>>> from sage.all import * >>> # needs sage.modules >>> from sage.modules.module import is_Module, is_VectorSpace >>> M = FreeModule(RationalField(),Integer(30)) >>> is_VectorSpace(M) doctest:warning... DeprecationWarning: the function is_VectorSpace is deprecated; use 'isinstance(..., Module)' and check the base ring instead See https://github.com/sagemath/sage/issues/37924 for details. True >>> M = FreeModule(IntegerRing(),Integer(30)) >>> is_Module(M) doctest:warning... DeprecationWarning: the function is_Module is deprecated; use 'isinstance(..., Module)' instead See https://github.com/sagemath/sage/issues/37924 for details. True >>> is_VectorSpace(M) False