Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/factorization_integer.py
4036 views
1
from sage.structure.factorization import Factorization
2
3
from sage.rings.integer_ring import ZZ
4
5
class IntegerFactorization(Factorization):
6
"""
7
A lightweight class for an ``IntegerFactorization`` object,
8
inheriting from the more general ``Factorization`` class.
9
10
In the ``Factorization`` class the user has to create a list
11
containing the factorization data, which is then passed to the
12
actual ``Factorization`` object upon initialization.
13
14
However, for the typical use of integer factorization via
15
the ``Integer.factor()`` method in ``sage.rings.integer``
16
this is noticeably too much overhead, slowing down the
17
factorization of integers of up to about 40 bits by a factor
18
of around 10. Moreover, the initialization done in the
19
``Factorization`` class is typically unnecessary: the caller
20
can guarantee that the list contains pairs of an ``Integer``
21
and an ``int``, as well as that the list is sorted.
22
23
AUTHOR:
24
25
- Sebastian Pancratz (2010-01-10)
26
"""
27
28
def __init__(self, x, unit=None, cr=False, sort=True, simplify=True,
29
unsafe=False):
30
"""
31
Sets ``self`` to the factorization object with list ``x``,
32
which must be a sorted list of pairs, where each pair contains
33
a factor and an exponent.
34
35
If the flag ``unsafe`` is set to ``False`` this method delegates
36
the initialization to the parent class, which means that a rather
37
lenient and careful way of initialization is chosen. For example,
38
elements are coerced or converted into the right parents, multiple
39
occurrences of the same factor are collected (in the commutative
40
case), the list is sorted (unless ``sort`` is ``False``) etc.
41
42
However, if the flag is set to ``True``, no error handling is
43
carried out. The list ``x`` is assumed to list of pairs. The
44
type of the factors is assumed to be constant across all factors:
45
either ``Integer`` (the generic case) or ``int`` (as supported
46
by the flag ``int_`` of the ``factor()`` method). The type of
47
the exponents is assumed to be ``int``. The list ``x`` itself
48
will be referenced in this factorization object and hence the
49
caller is responsible for not changing the list after creating
50
the factorization. The unit is assumed to be either ``None`` or
51
of type ``Integer``, taking one of the values `+1` or `-1`.
52
53
EXAMPLES::
54
55
sage: factor(15)
56
3 * 5
57
"""
58
if unsafe:
59
if unit is None:
60
self._Factorization__unit = sage.rings.integer.ONE
61
else:
62
self._Factorization__unit = unit
63
64
self._Factorization__x = x
65
self._Factorization__universe = ZZ
66
self._Factorization__cr = cr
67
68
if sort:
69
self.sort()
70
if simplify:
71
self.simplify()
72
73
else:
74
super(IntegerFactorization, self).__init__(x,
75
unit=unit, cr=cr, sort=sort, simplify=simplify)
76
77
def __sort__(self, _cmp=None):
78
"""
79
Sort the factors in this factorization.
80
81
INPUT:
82
83
- ``_cmp`` - (default: None) comparison function
84
85
EXAMPLES::
86
87
sage: F = factor(15)
88
sage: F.sort(_cmp = lambda x,y: -cmp(x,y))
89
sage: F
90
5 * 3
91
"""
92
self.__x.sort(_cmp)
93
94
95