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

Commit

Permalink
11111: CombinatorialFreeModule.echelon_from -> Modules.WithBasis.Pare…
Browse files Browse the repository at this point in the history
…ntMethods.echelon_form; removed duplicate echelonize; delete meaningless and dangerous argument base_ring
  • Loading branch information
nthiery committed Apr 16, 2015
1 parent 6a3a07d commit b6d8bff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
40 changes: 29 additions & 11 deletions src/sage/categories/modules_with_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,33 +567,51 @@ def _repr_(self):
name = "Free module generated by {}".format(self.basis().keys())
return name + " over {}".format(self.base_ring())

def echelonize(self, gens):
"""
A basis in echelon form of the subspace spanned by a finite set of
elements.
def echelon_form(self, elements):
r"""
Return a basis in echelon form of the subspace spanned by
a finite set of elements.
INPUT:
- ``gens`` -- a finite set of elements of ``self``
- ``elements`` -- a list or finite iterable of elements of ``self``.
OUTPUT:
A list of elements of ``self`` whose expressions as vectors
is a matrix in echelon form.
A list of elements of ``self`` whose expressions as
vectors form a matrix in echelon form. If ``base_ring`` is
specified, then the calculation is achieved in this base
ring.
EXAMPLES::
sage: X = CombinatorialFreeModule(QQ, range(3), prefix="x")
sage: x = X.basis()
sage: V = X.echelonize([x[0]-x[1], x[0]-x[2],x[1]-x[2]]); V
sage: V = X.echelon_form([x[0]-x[1], x[0]-x[2],x[1]-x[2]]); V
[x[0] - x[2], x[1] - x[2]]
sage: matrix(map(vector, V))
[ 1 0 -1]
[ 0 1 -1]
::
sage: F = CombinatorialFreeModule(ZZ, [1,2,3,4])
sage: B = F.basis()
sage: elements = [B[1]-17*B[2]+6*B[3], B[1]-17*B[2]+B[4]]
sage: F.echelon_form(elements)
[B[1] - 17*B[2] + B[4], 6*B[3] - B[4]]
::
sage: F = CombinatorialFreeModule(QQ, ['a','b','c'])
sage: a,b,c = F.basis()
sage: F.echelon_form([8*a+b+10*c, -3*a+b-c, a-b-c])
[B['a'] + B['c'], B['b'] + 2*B['c']]
"""
from sage.matrix.constructor import matrix
mat = matrix([g.to_vector() for g in gens])
return [self.from_vector(v) for v in mat.row_space().basis()]
mat = matrix([g._vector_() for g in elements])
mat.echelonize()
return [self.from_vector(vec) for vec in mat if vec]

def submodule(self, gens,
check=True, already_echelonized=False, category=None):
Expand Down Expand Up @@ -728,7 +746,7 @@ def submodule(self, gens,
sage: TestSuite(center).run()
"""
if not already_echelonized:
gens = self.echelonize(gens)
gens = self.echelon_form(gens)
from sage.modules.with_basis.subquotient import SubmoduleWithBasis
return SubmoduleWithBasis(gens, ambient=self, category=category)

Expand Down
40 changes: 0 additions & 40 deletions src/sage/combinat/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2084,46 +2084,6 @@ def _from_dict( self, d, coerce=False, remove_zeros=True ):
d = dict( (key, coeff) for key, coeff in d.iteritems() if coeff)
return self.element_class( self, d )

def echelon_form(self, elements, base_ring=None):
r"""
Compute the echelon form of the given elements.
INPUT:
- ``elements`` - a list of elements of ``self``.
- ``base_ring`` - ring (default: ``None``): compute the echelon
form over the given ring; if ``base_ring`` is ``None``, then uses
the base ring of ``self``.
OUTPUT:
- list of elements of ``self``
EXAMPLES::
sage: F = CombinatorialFreeModule(ZZ, [1,2,3,4])
sage: B = F.basis()
sage: elements = [B[1]-17*B[2]+6*B[3], B[1]-17*B[2]+B[4]]
sage: F.echelon_form(elements)
[B[1] - 17*B[2] + B[4], 6*B[3] - B[4]]
sage: F.echelon_form(elements, base_ring=QQ)
[B[1] - 17*B[2] + B[4], B[3] - 1/6*B[4]]
::
sage: F = CombinatorialFreeModule(QQ, ['a','b','c'])
sage: a,b,c = F.basis()
sage: F.echelon_form([8*a+b+10*c, -3*a+b-c, a-b-c])
[B['a'] + B['c'], B['b'] + 2*B['c']]
"""
from sage.matrix.constructor import matrix
if base_ring is None:
base_ring = self.base_ring()
mat = matrix(base_ring, map(vector,elements))
mat.echelonize()
return [self.from_vector(vec) for vec in mat if vec != 0]

class CombinatorialFreeModule_Tensor(CombinatorialFreeModule):
"""
Tensor Product of Free Modules
Expand Down

0 comments on commit b6d8bff

Please sign in to comment.