summaryrefslogtreecommitdiffstats
path: root/src/sage/categories/finite_dimensional_modules_with_basis.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/sage/categories/finite_dimensional_modules_with_basis.py')
-rw-r--r--src/sage/categories/finite_dimensional_modules_with_basis.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/sage/categories/finite_dimensional_modules_with_basis.py b/src/sage/categories/finite_dimensional_modules_with_basis.py
index cf6c716..4f78cbb 100644
--- a/src/sage/categories/finite_dimensional_modules_with_basis.py
+++ b/src/sage/categories/finite_dimensional_modules_with_basis.py
@@ -11,6 +11,7 @@ Finite dimensional modules with basis
import operator
from sage.categories.category_with_axiom import CategoryWithAxiom_over_base_ring
+from sage.misc.cachefunc import cached_method
class FiniteDimensionalModulesWithBasis(CategoryWithAxiom_over_base_ring):
"""
@@ -271,6 +272,57 @@ class FiniteDimensionalModulesWithBasis(CategoryWithAxiom_over_base_ring):
already_echelonized=already_echelonized)
return QuotientModuleWithBasis(submodule, category=category)
+ @cached_method
+ def _dense_free_module(self, base_ring=None):
+ """
+ Return a dense free module of the same dimension as ``self``.
+
+ INPUT:
+
+ - ``base_ring`` -- a ring or ``None``
+
+ If ``base_ring`` is ``None``, then the base ring of ``self``
+ is used.
+
+ This method is mostly used by ``_vector_``
+
+ EXAMPLES::
+
+ sage: C = CombinatorialFreeModule(QQ['x'], ['a','b','c']); C
+ Free module generated by {'a', 'b', 'c'} over
+ Univariate Polynomial Ring in x over Rational Field
+ sage: C._dense_free_module()
+ Ambient free module of rank 3 over the principal ideal domain
+ Univariate Polynomial Ring in x over Rational Field
+ sage: C._dense_free_module(QQ['x,y'])
+ Ambient free module of rank 3 over the integral domain
+ Multivariate Polynomial Ring in x, y over Rational Field
+ """
+ if base_ring is None:
+ base_ring = self.base_ring()
+ from sage.modules.free_module import FreeModule
+ return FreeModule(base_ring, self.dimension())
+
+ def from_vector(self, vector, order=None):
+ """
+ Build an element of ``self`` from a vector.
+
+ EXAMPLES::
+
+ sage: p_mult = matrix([[0,0,0],[0,0,-1],[0,0,0]])
+ sage: q_mult = matrix([[0,0,1],[0,0,0],[0,0,0]])
+ sage: A = algebras.FiniteDimensional(QQ, [p_mult, q_mult, matrix(QQ,3,3)],
+ ....: 'p,q,z')
+ sage: A.from_vector(vector([1,0,2]))
+ p + 2*z
+ """
+ if order is None:
+ try:
+ order = sorted(self.basis().keys())
+ except AttributeError: # Not a family, assume it is list-like
+ order = range(self.dimension())
+ return self._from_dict({order[i]: c for i,c in vector.iteritems()})
+
class ElementMethods:
def dense_coefficient_list(self, order=None):
"""
@@ -300,6 +352,25 @@ class FiniteDimensionalModulesWithBasis(CategoryWithAxiom_over_base_ring):
order = range(self.parent().dimension())
return [self[i] for i in order]
+ def _vector_(self, order=None):
+ r"""
+ Return ``self`` as a vector.
+
+ EXAMPLES::
+
+ sage: v = vector([0, -1, -3])
+ sage: v._vector_()
+ (0, -1, -3)
+ sage: C = CombinatorialFreeModule(QQ['x'], ['a','b','c'])
+ sage: C.an_element()._vector_()
+ (2, 2, 3)
+ """
+ dense_free_module = self.parent()._dense_free_module()
+ # We slightly break encapsulation for speed reasons
+ return dense_free_module.element_class(dense_free_module,
+ self.dense_coefficient_list(order),
+ coerce=True, copy=False)
+
class MorphismMethods:
def matrix(self, base_ring=None, side="left"):
r"""