Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rwst opened this issue Jul 27, 2014 · 45 comments
Closed

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

rwst opened this issue Jul 27, 2014 · 45 comments

Comments

@rwst
Copy link
Contributor

rwst commented Jul 27, 2014

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 rwst added this to the sage-6.3 milestone Jul 27, 2014
@rwst
Copy link
Contributor Author

rwst commented Jul 27, 2014

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

@rwst

This comment has been minimized.

@rwst rwst changed the title not all gap integers get converted replace gap.eval with libgap.eval in combinat/combinat.py Jul 27, 2014
@rwst

This comment has been minimized.

@rwst
Copy link
Contributor Author

rwst commented Jul 27, 2014

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

@rwst

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Jul 27, 2014

Dependencies: #16380

@tscrim
Copy link
Collaborator

tscrim commented Jul 27, 2014

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
Copy link
Contributor Author

rwst commented Aug 2, 2014

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
Copy link
Contributor Author

rwst commented Aug 2, 2014

Changed dependencies from #16380 to #16380, #16750

@rwst
Copy link
Contributor Author

rwst commented Aug 2, 2014

@vbraun
Copy link
Member

vbraun commented Aug 2, 2014

New commits:

16922b516719: replace gap with libgap

@vbraun
Copy link
Member

vbraun commented Aug 2, 2014

Commit: 16922b5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 3, 2014

Changed commit from 16922b5 to b69d63f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 3, 2014

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
Copy link
Contributor Author

rwst commented Aug 3, 2014

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
Copy link
Contributor Author

rwst commented Aug 3, 2014

Author: Ralf Stephan

@rwst
Copy link
Contributor Author

rwst commented Aug 3, 2014

Changed dependencies from #16380, #16750 to #16380

@rwst
Copy link
Contributor Author

rwst commented Aug 3, 2014

Merged: #16750

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 4, 2014

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

240cc1016719 reviewer's patch: attempt to fix previous patch

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 4, 2014

Changed commit from b69d63f to 240cc10

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 6, 2014

Changed commit from 240cc10 to a1269fb

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 6, 2014

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
Copy link
Contributor Author

rwst commented Aug 6, 2014

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.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 20, 2014

Changed commit from a1269fb to e82786b

@tscrim
Copy link
Collaborator

tscrim commented Aug 22, 2014

comment:17

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

@tscrim
Copy link
Collaborator

tscrim commented Aug 22, 2014

Changed merged from #16750 to none

@tscrim
Copy link
Collaborator

tscrim commented Aug 22, 2014

Dependencies: #16750

@rwst
Copy link
Contributor Author

rwst commented Aug 22, 2014

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.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2014

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

5b156ad16719: revert change to unordered_tuples(), handled by 13982

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2014

Changed commit from e82786b to 5b156ad

@jdemeyer
Copy link
Contributor

comment:21

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

@jdemeyer
Copy link
Contributor

@jdemeyer
Copy link
Contributor

Changed commit from 5b156ad to 4339c50

@jdemeyer
Copy link
Contributor

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
Copy link
Contributor

Changed dependencies from #16750 to #17157

@jdemeyer
Copy link
Contributor

Reviewer: Jeroen Demeyer

@vbraun
Copy link
Member

vbraun commented Oct 26, 2014

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

@vbraun
Copy link
Member

vbraun commented Oct 26, 2014

Changed commit from 4339c50 to 9f4777d

@vbraun
Copy link
Member

vbraun commented Oct 26, 2014

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
Copy link
Contributor

Changed reviewer from Jeroen Demeyer to Jeroen Demeyer, Volker Braun

@jdemeyer jdemeyer changed the title replace gap.eval with libgap.eval in combinat/combinat.py replace gap.eval with libgap calls in combinat/combinat.py Oct 27, 2014
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 27, 2014

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

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 27, 2014

Changed commit from 9f4777d to d6698e2

@vbraun
Copy link
Member

vbraun commented Oct 27, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants