Skip to content

replace gap.eval with libgap calls in combinat/combinat.py #16719

@rwst

Description

@rwst

As first reported in #15625 (comment:7) with lucas_number1, the same problem is encountered with gap evaluation of Stirling numbers:

sage: stirling_number1(1000,2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-b29f3b3e2003> in <module>()
----> 1 stirling_number1(Integer(1000),Integer(2))

/home/ralf/sage/local/lib/python2.7/site-packages/sage/combinat/combinat.pyc in stirling_number1(n, k)
    650     """
    651     return Integer(gap.eval("Stirling1({0},{1})".format(Integer(n),
--> 652                                                         Integer(k))))
    653 
    654 

/home/ralf/sage/local/lib/python2.7/site-packages/sage/rings/integer.so in sage.rings.integer.Integer.__init__ (build/cythonized/sage/rings/integer.c:7902)()

TypeError: unable to convert x (=<integer 301...000 (2566 digits)>) to an integer

The way to go would be to replace gap.eval with libgap.eval and it's faster, too:

sage: timeit('Integer(gap.eval("Stirling1(100,2)"))')
625 loops, best of 3: 419 µs per loop
sage: timeit('libgap.eval("Stirling1(100,2)").sage()')
625 loops, best of 3: 125 µs per loop
sage: timeit('libgap.eval("Stirling1(1000,2)").sage()')
125 loops, best of 3: 6.45 ms per loop

Addendum: even faster would be to use (n-1)!*H(n-1) for stirling(n,2), dependent on #16671:

sage: harmonic_number(99)*factorial(99)==stirling_number1(100,2)
True
sage: timeit('harmonic_number(99)*factorial(99)')
625 loops, best of 3: 56.9 µs per loop
sage: timeit('harmonic_number(999)*factorial(999)')
625 loops, best of 3: 119 µs per loop

Depends on #17157

CC: @vbraun

Component: combinatorics

Keywords: gap, bignum, expect, pexpect, libgap, stirling

Author: Ralf Stephan

Branch/Commit: d6698e2

Reviewer: Jeroen Demeyer, Volker Braun

Issue created by migration from https://trac.sagemath.org/ticket/16719

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions