Path: blob/master/sage/structure/factorization_integer.py
4036 views
from sage.structure.factorization import Factorization12from sage.rings.integer_ring import ZZ34class IntegerFactorization(Factorization):5"""6A lightweight class for an ``IntegerFactorization`` object,7inheriting from the more general ``Factorization`` class.89In the ``Factorization`` class the user has to create a list10containing the factorization data, which is then passed to the11actual ``Factorization`` object upon initialization.1213However, for the typical use of integer factorization via14the ``Integer.factor()`` method in ``sage.rings.integer``15this is noticeably too much overhead, slowing down the16factorization of integers of up to about 40 bits by a factor17of around 10. Moreover, the initialization done in the18``Factorization`` class is typically unnecessary: the caller19can guarantee that the list contains pairs of an ``Integer``20and an ``int``, as well as that the list is sorted.2122AUTHOR:2324- Sebastian Pancratz (2010-01-10)25"""2627def __init__(self, x, unit=None, cr=False, sort=True, simplify=True,28unsafe=False):29"""30Sets ``self`` to the factorization object with list ``x``,31which must be a sorted list of pairs, where each pair contains32a factor and an exponent.3334If the flag ``unsafe`` is set to ``False`` this method delegates35the initialization to the parent class, which means that a rather36lenient and careful way of initialization is chosen. For example,37elements are coerced or converted into the right parents, multiple38occurrences of the same factor are collected (in the commutative39case), the list is sorted (unless ``sort`` is ``False``) etc.4041However, if the flag is set to ``True``, no error handling is42carried out. The list ``x`` is assumed to list of pairs. The43type of the factors is assumed to be constant across all factors:44either ``Integer`` (the generic case) or ``int`` (as supported45by the flag ``int_`` of the ``factor()`` method). The type of46the exponents is assumed to be ``int``. The list ``x`` itself47will be referenced in this factorization object and hence the48caller is responsible for not changing the list after creating49the factorization. The unit is assumed to be either ``None`` or50of type ``Integer``, taking one of the values `+1` or `-1`.5152EXAMPLES::5354sage: factor(15)553 * 556"""57if unsafe:58if unit is None:59self._Factorization__unit = sage.rings.integer.ONE60else:61self._Factorization__unit = unit6263self._Factorization__x = x64self._Factorization__universe = ZZ65self._Factorization__cr = cr6667if sort:68self.sort()69if simplify:70self.simplify()7172else:73super(IntegerFactorization, self).__init__(x,74unit=unit, cr=cr, sort=sort, simplify=simplify)7576def __sort__(self, _cmp=None):77"""78Sort the factors in this factorization.7980INPUT:8182- ``_cmp`` - (default: None) comparison function8384EXAMPLES::8586sage: F = factor(15)87sage: F.sort(_cmp = lambda x,y: -cmp(x,y))88sage: F895 * 390"""91self.__x.sort(_cmp)92939495