Ticket #13199: flint_partition_function.patch

File flint_partition_function.patch, 3.5 KB (added by fredrik.johansson, 10 years ago)
  • module_list.py

    # HG changeset patch
    # User Fredrik Johansson <fredrik.johansson@gmail.com>
    # Date 1341411582 -7200
    # Node ID b4439ff9cf448a1408958782797c6e0509e64a16
    # Parent  c64a79af8e3d7da61ef811ee5a64860590ffb67b
    use FLINT to compute the partition function
    
    diff --git a/module_list.py b/module_list.py
    a b  
    593593              include_dirs = [SAGE_INC + 'flint/'],
    594594              extra_compile_args=["-std=c99", "-D_XPG6"],
    595595              depends = flint_depends),
     596
     597    Extension('sage.libs.flint.arith',
     598              sources = ["sage/libs/flint/arith.pyx"],
     599              libraries = ["csage", "flint", "gmp", "gmpxx", "m", "stdc++"],
     600              include_dirs = [SAGE_INC + 'flint/'],
     601              extra_compile_args=["-std=c99", "-D_XPG6"],
     602              depends = flint_depends),
    596603   
    597604    Extension('sage.libs.fplll.fplll',
    598605              sources = ['sage/libs/fplll/fplll.pyx'],
  • sage/combinat/partition.py

    diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
    a b  
    231231import __builtin__
    232232from combinat import CombinatorialClass, CombinatorialObject, cyclic_permutations_iterator, InfiniteAbstractCombinatorialClass
    233233import partitions as partitions_ext
     234import sage.libs.flint.arith as flint_arith
    234235from sage.libs.all import pari
    235236import tableau
    236237import permutation
     
    43874388    -  ``algorithm`` - (default: 'default')
    43884389   
    43894390    -  ``'default'`` - If k is not None, then use Gap (very
    4390        slow). If k is None, use Jonathan Bober's highly optimized
     4391       slow). If k is None, use FLINT's highly optimized
    43914392       implementation (this is the fastest code in the world for this
    43924393       problem).
    4393    
     4394
     4395    -  ``'flint'`` - use FLINT.
     4396
    43944397    -  ``'bober'`` - use Jonathan Bober's implementation
    43954398   
    43964399    -  ``'gap'`` - use GAP (VERY *slow*)
     
    44254428        7
    44264429        sage: number_of_partitions(5, algorithm='bober')
    44274430        7
     4431        sage: number_of_partitions(5, algorithm='flint')
     4432        7
    44284433   
    44294434    The input must be a nonnegative integer or a ValueError is raised.
    44304435   
     
    45224527
    45234528    if algorithm == 'default':
    45244529        if k is None:
    4525             algorithm = 'bober'
     4530            algorithm = 'flint'
    45264531        else:
    45274532            algorithm = 'gap'
    45284533           
     
    45364541    if k is not None:
    45374542        raise ValueError, "only the GAP algorithm works if k is specified."
    45384543
    4539     if algorithm == 'bober':
     4544    if algorithm == 'flint':
     4545        return flint_arith.number_of_partitions(n)
     4546
     4547    elif algorithm == 'bober':
    45404548        return partitions_ext.number_of_partitions(n)
    45414549       
    45424550    elif algorithm == 'pari':
  • 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 "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        sage: from sage.libs.flint.arith import number_of_partitions
     16        sage: number_of_partitions(10)
     17        42
     18    """
     19    cdef fmpz_t ans_fmpz
     20    cdef Integer ans
     21
     22    fmpz_init(ans_fmpz)
     23
     24    if n > 1000:
     25        sig_on()
     26
     27    arith_number_of_partitions(ans_fmpz, n)
     28
     29    if n > 1000:
     30        sig_off()
     31
     32    ans = Integer(0)
     33    fmpz_get_mpz(ans.value, ans_fmpz)
     34    fmpz_clear(ans_fmpz)
     35    return ans
     36