You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trac #16719: replace gap.eval with libgap calls in combinat/combinat.py
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
}}}
URL: http://trac.sagemath.org/16719
Reported by: rws
Ticket author(s): Ralf Stephan
Reviewer(s): Jeroen Demeyer, Volker Braun
0 commit comments