Skip to content

Commit

Permalink
Trac #30179: ModulesWithBasis.linear_combination should be a method o…
Browse files Browse the repository at this point in the history
…f Modules

This method does not need a basis.

Moving it to `Modules` will make it available in `FiniteRankFreeModule`.

{{{
        def linear_combination(self, iter_of_elements_coeff,
factor_on_left=True):
            r"""
            Return the linear combination `\lambda_1 v_1 + \cdots +
            \lambda_k v_k` (resp.  the linear combination `v_1 \lambda_1
+
            \cdots + v_k \lambda_k`) where ``iter_of_elements_coeff``
iterates
            through the sequence `((\lambda_1, v_1), ..., (\lambda_k,
v_k))`.

            INPUT:

            - ``iter_of_elements_coeff`` -- iterator of pairs
              ``(element, coeff)`` with ``element`` in ``self`` and
              ``coeff`` in ``self.base_ring()``

            - ``factor_on_left`` -- (optional) if ``True``, the
coefficients
              are multiplied from the left; if ``False``, the
coefficients
              are multiplied from the right

            EXAMPLES::

                sage: m = matrix([[0,1],[1,1]])
                sage: J.<a,b,c> = JordanAlgebra(m)
                sage: J.linear_combination(((a+b, 1), (-2*b + c, -1)))
                1 + (3, -1)
            """
            if factor_on_left:
                return self.sum(coeff * element
                                for element, coeff in
iter_of_elements_coeff)
            else:
                return self.sum(element * coeff
                                for element, coeff in
iter_of_elements_coeff)
}}}

URL: https://trac.sagemath.org/30179
Reported by: mkoeppe
Ticket author(s): Matthias Koeppe
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jul 28, 2020
2 parents d8b44dd + f34d2a5 commit abd54ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
32 changes: 32 additions & 0 deletions src/sage/categories/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,38 @@ def extra_super_categories(self):
at_startup=True)

class ParentMethods:

def linear_combination(self, iter_of_elements_coeff, factor_on_left=True):
r"""
Return the linear combination `\lambda_1 v_1 + \cdots +
\lambda_k v_k` (resp. the linear combination `v_1 \lambda_1 +
\cdots + v_k \lambda_k`) where ``iter_of_elements_coeff`` iterates
through the sequence `((\lambda_1, v_1), ..., (\lambda_k, v_k))`.
INPUT:
- ``iter_of_elements_coeff`` -- iterator of pairs
``(element, coeff)`` with ``element`` in ``self`` and
``coeff`` in ``self.base_ring()``
- ``factor_on_left`` -- (optional) if ``True``, the coefficients
are multiplied from the left; if ``False``, the coefficients
are multiplied from the right
EXAMPLES::
sage: m = matrix([[0,1],[1,1]])
sage: J.<a,b,c> = JordanAlgebra(m)
sage: J.linear_combination(((a+b, 1), (-2*b + c, -1)))
1 + (3, -1)
"""
if factor_on_left:
return self.sum(coeff * element
for element, coeff in iter_of_elements_coeff)
else:
return self.sum(element * coeff
for element, coeff in iter_of_elements_coeff)

@cached_method
def tensor_square(self):
"""
Expand Down
31 changes: 0 additions & 31 deletions src/sage/categories/modules_with_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,37 +1113,6 @@ def sum_of_terms(self, terms):
"""
return self.sum(self.term(index, coeff) for (index, coeff) in terms)

def linear_combination(self, iter_of_elements_coeff, factor_on_left=True):
r"""
Return the linear combination `\lambda_1 v_1 + \cdots +
\lambda_k v_k` (resp. the linear combination `v_1 \lambda_1 +
\cdots + v_k \lambda_k`) where ``iter_of_elements_coeff`` iterates
through the sequence `((\lambda_1, v_1), ..., (\lambda_k, v_k))`.
INPUT:
- ``iter_of_elements_coeff`` -- iterator of pairs
``(element, coeff)`` with ``element`` in ``self`` and
``coeff`` in ``self.base_ring()``
- ``factor_on_left`` -- (optional) if ``True``, the coefficients
are multiplied from the left; if ``False``, the coefficients
are multiplied from the right
EXAMPLES::
sage: m = matrix([[0,1],[1,1]])
sage: J.<a,b,c> = JordanAlgebra(m)
sage: J.linear_combination(((a+b, 1), (-2*b + c, -1)))
1 + (3, -1)
"""
if factor_on_left:
return self.sum(coeff * element
for element, coeff in iter_of_elements_coeff)
else:
return self.sum(element * coeff
for element, coeff in iter_of_elements_coeff)

def _apply_module_morphism(self, x, on_basis, codomain=False):
"""
Return the image of ``x`` under the module morphism defined by
Expand Down

0 comments on commit abd54ac

Please sign in to comment.