Ticket #13199: trac_13199_flint_partition_function_v2.patch

File trac_13199_flint_partition_function_v2.patch, 5.9 KB (added by chapoton, 9 years ago)
  • module_list.py

    # HG changeset patch
    # User Fredrik Johansson <fredrik.johansson@gmail.com>
    # Date 1341411582 -7200
    # Node ID 24770e05d76f74e91e21cfde00b36f2719b7be91
    # Parent  014ea68136b9bbe602cd584f8861915d24fb95fa
    trac #13199 use FLINT to compute the partition function
    
    diff --git a/module_list.py b/module_list.py
    a b ext_modules = [ 
    623623    Extension('sage.libs.flint.flint',
    624624              sources = ["sage/libs/flint/flint.pyx"],
    625625              libraries = ["csage", "flint", "gmp", "gmpxx", "m", "stdc++"],
    626               extra_compile_args=["-std=c99", "-D_XPG6"],
     626              extra_compile_args = ["-std=c99", "-D_XPG6"],
    627627              depends = flint_depends),
    628628
    629629    Extension('sage.libs.flint.fmpz_poly',
    630630              sources = ["sage/libs/flint/fmpz_poly.pyx"],
    631631              libraries = ["csage", "flint", "gmp", "gmpxx", "m", "stdc++"],
    632               extra_compile_args=["-std=c99", "-D_XPG6"],
     632              extra_compile_args = ["-std=c99", "-D_XPG6"],
     633              depends = flint_depends),
     634
     635    Extension('sage.libs.flint.arith',
     636              sources = ["sage/libs/flint/arith.pyx"],
     637              libraries = ["csage", "flint", "gmp", "gmpxx", "m", "stdc++"],
     638              include_dirs = [SAGE_INC + 'flint/'],
     639              extra_compile_args = ["-std=c99", "-D_XPG6"],
    633640              depends = flint_depends),
    634641   
    635642    Extension('sage.libs.fplll.fplll',
  • sage/combinat/partition.py

    diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
    a b For display options, see :obj:`Partition 
    2626    - If given coordinates of the form ``(r, c)``, then use Python's
    2727      \*-operator.
    2828
     29
    2930    - Throughout this documentation, for a partition `\lambda` we will denote
    3031      its conjugate partition by `\lambda^{\prime}`. For more on conjugate
    3132      partitions, see :meth:`Partition.conjugate()`.
    We use the lexicographic ordering:: 
    272273
    273274from sage.interfaces.all import gap
    274275from sage.libs.all import pari
     276from sage.libs.flint.arith import number_of_partitions as flint_number_of_partitions
    275277
    276278from sage.structure.global_options import GlobalOptions
    277279from sage.structure.parent import Parent
    class Partitions_n(Partitions): 
    44994501            lst = [self.n-1, 1]
    45004502        return self.element_class(self, lst)
    45014503
    4502     def cardinality(self, algorithm='bober'):
     4504    def cardinality(self, algorithm='flint'):
    45034505        r"""
    45044506        Returns the number of partitions of the specified size.
    45054507
    class Partitions_n(Partitions): 
    45084510        - ``algorithm``  - (default: ``'bober'``)
    45094511
    45104512          - ``'bober'`` -- Use Jonathan Bober's implementation (*very* fast).
     4513          - ``'flint'`` -- use FLINT.
    45114514          - ``'gap'`` -- use GAP (VERY *slow*)
    45124515          - ``'pari'`` -- use PARI. Speed seems the same as GAP until
    45134516            `n` is in the thousands, in which case PARI is faster.
    class Partitions_n(Partitions): 
    45324535            7
    45334536            sage: Partitions(5).cardinality(algorithm='bober')
    45344537            7
     4538            sage: number_of_partitions(5, algorithm='flint')
     4539            7
    45354540
    45364541        The input must be a nonnegative integer or a ``ValueError`` is raised.
    45374542
    class Partitions_n(Partitions): 
    45754580        if algorithm == 'bober':
    45764581            return cached_number_of_partitions(self.n)
    45774582
     4583        elif algorithm == 'flint':
     4584            return flint_number_of_partitions(self.n)
     4585
    45784586        elif algorithm == 'gap':
    4579             return ZZ( gap.eval("NrPartitions(%s)"%(ZZ(self.n))) )
     4587            return ZZ(gap.eval("NrPartitions(%s)" % (ZZ(self.n))))
    45804588
    45814589        elif algorithm == 'pari':
    45824590            return ZZ(pari(ZZ(self.n)).numbpart())
    45834591
    4584         raise ValueError("unknown algorithm '%s'"%algorithm)
     4592        raise ValueError("unknown algorithm '%s'" % algorithm)
    45854593
    45864594    def random_element(self, measure = 'uniform'):
    45874595        """
    def number_of_partitions_set(S,k): 
    59135921    deprecation(13072,'number_of_partitions_set is deprecated. Use SetPartitions().cardinality() instead.')
    59145922    return sage.combinat.set_partition.SetPartitions(S,k).cardinality()
    59155923
    5916 def number_of_partitions(n,k=None, algorithm='default'):
     5924def number_of_partitions(n, k=None, algorithm='default'):
    59175925    r"""
    59185926    Returns the number of partitions of `n` with, optionally, at most `k`
    59195927    parts.
    def number_of_partitions(n,k=None, algor 
    60646072
    60656073    if algorithm == 'default':
    60666074        if k is None:
    6067             algorithm = 'bober'
     6075            algorithm = 'flint'
    60686076        else:
    60696077            algorithm = 'gap'
    60706078
    def number_of_partitions(n,k=None, algor 
    60836091    if algorithm == 'bober':
    60846092        return cached_number_of_partitions(n)
    60856093
     6094    elif algorithm == 'flint':
     6095        return flint_number_of_partitions(n)
     6096
    60866097    elif algorithm == 'pari':
    60876098        deprecation(13072,"sage.combinat.number_of_partitions is deprecated. Use  Partitions().cardinality(algorithm='pari')")
    60886099        return ZZ(pari(ZZ(n)).numbpart())
  • new file sage/libs/flint/arith.pyx

    diff --git a/sage/libs/flint/arith.pyx b/sage/libs/flint/arith.pyx
    new file mode 100644
    - +  
     1include "../../ext/interrupt.pxi"
     2include "fmpz.pxi"
     3
     4cdef extern from "flint/arith.h":
     5    void arith_number_of_partitions(fmpz_t x, unsigned long n)
     6
     7from sage.rings.integer cimport Integer
     8
     9
     10def number_of_partitions(unsigned long n):
     11    """
     12    Returns the number of partitions of the integer n.
     13
     14    EXAMPLES::
     15
     16        sage: from sage.libs.flint.arith import number_of_partitions
     17        sage: number_of_partitions(10)
     18        42
     19    """
     20    cdef fmpz_t ans_fmpz
     21    cdef Integer ans
     22
     23    fmpz_init(ans_fmpz)
     24
     25    if n > 1000:
     26        sig_on()
     27
     28    arith_number_of_partitions(ans_fmpz, n)
     29
     30    if n > 1000:
     31        sig_off()
     32
     33    ans = Integer(0)
     34    fmpz_get_mpz(ans.value, ans_fmpz)
     35    fmpz_clear(ans_fmpz)
     36    return ans
     37