Ticket #15193: 15193-Factorization-pari-chain.patch

File 15193-Factorization-pari-chain.patch, 1.6 KB (added by pbruin, 8 years ago)

same but using itertools.chain

  • sage/structure/factorization.py

    # HG changeset patch
    # User Peter Bruin <P.Bruin@warwick.ac.uk>
    # Date 1380294535 -3600
    # Node ID 36f7c2d9d76f3982c4ccb69a06a70cf33808de64
    # Parent  4275212b576c16066ba6d5e9574e057c7f558bc8
    Trac 15193: add method Factorization._pari_(), using itertools.chain
    
    diff --git a/sage/structure/factorization.py b/sage/structure/factorization.py
    a b  
    186186from sage.structure.sequence import Sequence
    187187from sage.rings.integer import Integer
    188188from sage.misc.all import prod
     189from sage.misc.cachefunc import cached_method
    189190
    190191class Factorization(SageObject):
    191192    """
     
    885886            s =  u + ' \\cdot ' + s
    886887        return s
    887888
     889    @cached_method
     890    def _pari_(self):
     891        """
     892        Return the PARI factorization matrix corresponding to ``self``.
     893
     894        EXAMPLES::
     895
     896            sage: f = factor(-24)
     897            sage: pari(f)
     898            [-1, 1; 2, 3; 3, 1]
     899
     900            sage: R.<x> = QQ[]
     901            sage: g = factor(x^10 - 1)
     902            sage: pari(g)
     903            [x - 1, 1; x + 1, 1; x^4 - x^3 + x^2 - x + 1, 1; x^4 + x^3 + x^2 + x + 1, 1]
     904
     905        """
     906        from sage.libs.pari.all import pari
     907        from itertools import chain
     908
     909        n = len(self)
     910        if self.__unit == 1:
     911            init = ()
     912        else:
     913            init = (self.__unit, 1)
     914            n += 1
     915        # concatenate (p, e) tuples
     916        entries = init + tuple(chain.from_iterable(self))
     917        return pari.matrix(n, 2, entries)
     918
    888919    def __add__(self, other):
    889920        """
    890921        Return the (unfactored) sum of self and other.