An element in an indexed free module¶
AUTHORS:
- Travis Scrimshaw (03-2017): Moved code from - sage.combinat.free_module.
- Travis Scrimshaw (29-08-2022): Implemented - copyas the identity map.
- class sage.modules.with_basis.indexed_element.IndexedFreeModuleElement[source]¶
- Bases: - ModuleElement- Element class for - CombinatorialFreeModule.- monomial_coefficients(copy=True)[source]¶
- Return the internal dictionary which has the combinatorial objects indexing the basis as keys and their corresponding coefficients as values. - INPUT: - copy– boolean (default:- True); if- selfis internally represented by a dictionary- d, then make a copy of- d. If- False, then this can cause undesired behavior by mutating- d.
 - EXAMPLES: - sage: F = CombinatorialFreeModule(QQ, ['a','b','c']) sage: B = F.basis() sage: f = B['a'] + 3*B['c'] sage: d = f.monomial_coefficients() sage: d['a'] 1 sage: d['c'] 3 - >>> from sage.all import * >>> F = CombinatorialFreeModule(QQ, ['a','b','c']) >>> B = F.basis() >>> f = B['a'] + Integer(3)*B['c'] >>> d = f.monomial_coefficients() >>> d['a'] 1 >>> d['c'] 3 - To run through the monomials of an element, it is better to use the idiom: - sage: for (t,c) in f: ....: print("{} {}".format(t,c)) a 1 c 3 - >>> from sage.all import * >>> for (t,c) in f: ... print("{} {}".format(t,c)) a 1 c 3 - sage: # needs sage.combinat sage: s = SymmetricFunctions(QQ).schur() sage: a = s([2,1])+2*s([3,2]) sage: d = a.monomial_coefficients() sage: type(d) <... 'dict'> sage: d[ Partition([2,1]) ] 1 sage: d[ Partition([3,2]) ] 2 - >>> from sage.all import * >>> # needs sage.combinat >>> s = SymmetricFunctions(QQ).schur() >>> a = s([Integer(2),Integer(1)])+Integer(2)*s([Integer(3),Integer(2)]) >>> d = a.monomial_coefficients() >>> type(d) <... 'dict'> >>> d[ Partition([Integer(2),Integer(1)]) ] 1 >>> d[ Partition([Integer(3),Integer(2)]) ] 2 
 - to_vector(new_base_ring=None, order=None, sparse=False)[source]¶
- Return - selfas a vector.- INPUT: - new_base_ring– a ring (default:- None)
- order– (optional) an ordering of the support of- self
- sparse– boolean (default:- False); whether to return a sparse vector or a dense vector
 - OUTPUT: a - FreeModule()vector- Warning - This will crash/run forever if - selfis infinite dimensional!- See also 
- CombinatorialFreeModule._dense_free_module()
 - EXAMPLES: - sage: F = CombinatorialFreeModule(QQ, ['a','b','c']) sage: B = F.basis() sage: f = B['a'] - 3*B['c'] sage: f._vector_() (1, 0, -3) - >>> from sage.all import * >>> F = CombinatorialFreeModule(QQ, ['a','b','c']) >>> B = F.basis() >>> f = B['a'] - Integer(3)*B['c'] >>> f._vector_() (1, 0, -3) - One can use equivalently: - sage: f.to_vector() (1, 0, -3) sage: vector(f) (1, 0, -3) - >>> from sage.all import * >>> f.to_vector() (1, 0, -3) >>> vector(f) (1, 0, -3) - More examples: - sage: # needs sage.combinat sage: QS3 = SymmetricGroupAlgebra(QQ, 3) sage: a = 2*QS3([1,2,3]) + 4*QS3([3,2,1]) sage: a._vector_() (2, 0, 0, 0, 0, 4) sage: a.to_vector() (2, 0, 0, 0, 0, 4) sage: vector(a) (2, 0, 0, 0, 0, 4) sage: a == QS3.from_vector(a.to_vector()) True sage: a.to_vector(sparse=True) (2, 0, 0, 0, 0, 4) - >>> from sage.all import * >>> # needs sage.combinat >>> QS3 = SymmetricGroupAlgebra(QQ, Integer(3)) >>> a = Integer(2)*QS3([Integer(1),Integer(2),Integer(3)]) + Integer(4)*QS3([Integer(3),Integer(2),Integer(1)]) >>> a._vector_() (2, 0, 0, 0, 0, 4) >>> a.to_vector() (2, 0, 0, 0, 0, 4) >>> vector(a) (2, 0, 0, 0, 0, 4) >>> a == QS3.from_vector(a.to_vector()) True >>> a.to_vector(sparse=True) (2, 0, 0, 0, 0, 4) - If - new_base_ringis specified, then a vector over- new_base_ringis returned:- sage: a._vector_(RDF) # needs sage.combinat (2.0, 0.0, 0.0, 0.0, 0.0, 4.0) - >>> from sage.all import * >>> a._vector_(RDF) # needs sage.combinat (2.0, 0.0, 0.0, 0.0, 0.0, 4.0) - Note - Issue #13406: the current implementation has been optimized, at the price of breaking the encapsulation for FreeModule elements creation, with the following use case as metric, on a 2008’ Macbook Pro: - sage: F = CombinatorialFreeModule(QQ, range(10)) sage: f = F.an_element() sage: %timeit f._vector_() # not tested 625 loops, best of 3: 17.5 micros per loop Other use cases may call for different or further optimizations.