sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.22k stars 425 forks source link

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

Closed rwst closed 9 years ago

rwst commented 10 years ago

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

rwst commented 10 years ago

Changed keywords from gap, bignum, expect, pexpect to gap, bignum, expect, pexpect, libgap

rwst commented 10 years ago

Description changed:

--- 
+++ 
@@ -18,4 +18,14 @@

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

-Alternatively, this ticket may discuss abandoning the GAP expect interface for such big number results and use libpari instead. +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 + +

rwst commented 10 years ago

Description changed:

--- 
+++ 
@@ -28,4 +28,12 @@
 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 + +

rwst commented 10 years ago

Changed keywords from gap, bignum, expect, pexpect, libgap to gap, bignum, expect, pexpect, libgap, stirling

rwst commented 10 years ago

Description changed:

--- 
+++ 
@@ -35,5 +35,7 @@
 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
tscrim commented 10 years ago

Dependencies: #16380

tscrim commented 10 years ago
comment:4

Also with #16380 (which I've added as a dependency because it's not been put into a release yet), we don't have to worry about the startup time of libgap anymore (which was really annoying). +1 to doing this.

rwst commented 9 years ago
comment:5

This is blocked by:

sage: ans=libgap.eval("UnorderedTuples(['a','b','c'],2)")
sage: type(ans)
<type 'sage.libs.gap.element.GapElement_List'>
sage: a=ans.sage()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-4-43379484318b> in <module>()
----> 1 a=ans.sage()

/home/ralf/sage/local/lib/python2.7/site-packages/sage/libs/gap/element.so in sage.libs.gap.element.GapElement_List.sage (build/cythonized/sage/libs/gap/element.c:14661)()

/home/ralf/sage/local/lib/python2.7/site-packages/sage/libs/gap/element.so in sage.libs.gap.element.GapElement_List.sage (build/cythonized/sage/libs/gap/element.c:14661)()

/home/ralf/sage/local/lib/python2.7/site-packages/sage/libs/gap/element.so in sage.libs.gap.element.GapElement.sage (build/cythonized/sage/libs/gap/element.c:8749)()

See #16750

rwst commented 9 years ago

Changed dependencies from #16380 to #16380, #16750

rwst commented 9 years ago

Branch: u/rws/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py

vbraun commented 9 years ago

New commits:

16922b516719: replace gap with libgap
vbraun commented 9 years ago

Commit: 16922b5

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 16922b5 to b69d63f

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

843ba48Convert Gap chars into Python strings
b69d63fMerge branch 'u/vbraun/libgap_fails_converting_string_gapelements_to_sage' of trac.sagemath.org:sage into t/16719/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py
rwst commented 9 years ago
comment:9

OK, now that strings get to GAP, back they get as GapElement_List of chars:

sage: ans = libgap.eval("UnorderedTuples(['a', 'b', 'c'],2)"); ans
[ "aa", "ab", "ac", "bb", "bc", "cc" ]
sage: type(ans[0])
<type 'sage.libs.gap.element.GapElement_List'>
sage: ans.sage()
[["'a'", "'a'"],
 ["'a'", "'b'"],
 ["'a'", "'c'"],
 ["'b'", "'b'"],
 ["'b'", "'c'"],
 ["'c'", "'c'"]]

Shouldn't lists of chars be converted to strings?

rwst commented 9 years ago

Author: Ralf Stephan

rwst commented 9 years ago

Changed dependencies from #16380, #16750 to #16380

rwst commented 9 years ago

Merged: #16750

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

240cc1016719 reviewer's patch: attempt to fix previous patch
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from b69d63f to 240cc10

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 240cc10 to a1269fb

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

47f0796Merge branch 'develop' into t/16719/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py
7cc26d5Improve GAP String handling
6fe1fc5Treat the empty GAP list as list and not as string
6a4892dMerge branch 't/16750/libgap_fails_converting_string_gapelements_to_sage' into t/16719/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py
a1269fb16719: unordered_tuples() distinguishes now between set types
rwst commented 9 years ago
comment:13

It felt more natural to give tuples of chars as string, but tuples of char+string as list of strings. If the latter had been the original behaviour the GAP T_CHAR hassle wouldn't have been necessary.

Please review.

kcrisman commented 9 years ago

Changed dependencies from #16380 to none

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

e82786bMerge branch 'develop' into t/16719/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from a1269fb to e82786b

tscrim commented 9 years ago
comment:17

Note that this will conflict with #13982 for unordered_tuples (FYI I handled GAP's output as strings differently).

tscrim commented 9 years ago

Changed merged from #16750 to none

tscrim commented 9 years ago

Dependencies: #16750

rwst commented 9 years ago
comment:19

I wouldn't mind removing the unordered tuples bit from this patch, especially for speed reasons, and choice of algorithm. It's just that I don't like it that you simply remove that doctest that results in ['aa', 'ab', 'ac', 'bb', 'bc', 'cc']. I think you should support it.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

5b156ad16719: revert change to unordered_tuples(), handled by 13982
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from e82786b to 5b156ad

jdemeyer commented 9 years ago
comment:21

For bell_number(), I'm making the change at #17157, so you can remove that piece.

jdemeyer commented 9 years ago

Changed branch from u/rws/replace_gap_eval_with_libgap_eval_in_combinat_combinat_py to u/jdemeyer/ticket/16719

jdemeyer commented 9 years ago

Changed commit from 5b156ad to 4339c50

jdemeyer commented 9 years ago
comment:23

Reviewer patch needs review.


New commits:

0266cefImprove formula for Bell numbers
fd22966Merge branch 'ticket/17157' into ticket/16719
4339c50Remove superfluous type conversions, use libgap for unordered_tuples()
jdemeyer commented 9 years ago

Changed dependencies from #16750 to #17157

jdemeyer commented 9 years ago

Reviewer: Jeroen Demeyer

vbraun commented 9 years ago

Changed branch from u/jdemeyer/ticket/16719 to u/vbraun/ticket/16719

vbraun commented 9 years ago

Changed commit from 4339c50 to 9f4777d

vbraun commented 9 years ago
comment:25

The reviewer commits look good to me. I've added a patch to make the calls to my beautiful library interface less ugly ;-)


New commits:

9f4777dBeautify libgap calls
jdemeyer commented 9 years ago

Changed reviewer from Jeroen Demeyer to Jeroen Demeyer, Volker Braun

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. Last 10 new commits:

34c9b4cChanges from the reviewers.
b47af6aA minor tweaking of the doc.
53a9d7eFamilies return output based on order of variable names. Fixed coercion issue.
339b71dFixed some typos.
3ce3f6aMerge branch 'public/algebras/weyl_clifford-15300' of git://trac.sagemath.org/sage into clifford
8698275lifting bilinear forms onto exterior algebras
a002f4bSome tweaks to lifted_bilinear_form and fixed doctest.
ff27bdcfurther fixes
330617cMerge commit 'ff27bdc5d87967d0e7fd5c1305cef4340db816c7' into ticket/17157
d6698e2Merge already-closed #17157 to resolve conflict
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 9f4777d to d6698e2

vbraun commented 9 years ago

Changed branch from u/vbraun/ticket/16719 to d6698e2