-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Better coercion to ZZ for libGAP integers, modular integers #37580
Better coercion to ZZ for libGAP integers, modular integers #37580
Conversation
Is this duplicating changes in gappy by any chance? |
AFAIK, gappy didn't go there, it's an unfinished (sadly) attempt to spin the Cython interface to (lib)gap into a separate Python package. |
Oops, no, my memory served me wrong, this part indeed was fixed in gappy, more precisely, in gappy+reworked Sage inteface to gappy, see https://github.com/sagemath/sagetrac-mirror/tree/u/dimpase/gappy-without-wrappers (look at the diff of element.pyx and see the chunk with "waste" comment removed) |
However, shouldn't we just fix this within our own code (independently of gappy)? |
well, it would be nice, but this PR doesn't work - exactly at this place, it seems, see CI output |
Oops, it looks like I pushed the wrong version of the code originally. This is now fixed and the checks seem to be passing now. |
src/sage/libs/gap/element.pyx
Outdated
elif size < 0: | ||
c_sign = -1 | ||
c_size = -size | ||
else: # Something is wrong, fall back to string representation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need this else
? As far as I understand from Int GAP_SizeInt(Obj obj) you get size==0
, the only 3rd possible case, if self.value==0
. In any event, there seem to be nothing "wrong" with getting 0 here.
@fingolfin - is this right?
Note that if I call GAP_SizeInt()
on a non-integer, I'll get
Error, GAP_SizeInt: <obj> must be an integer (not a list (string))
As far as I see, GAP's Ints are created by (GAP_)MakeObjInt(limbs,size), with limbs an array of unsigned ints, and size being >0 for getting a positive Int, <0 for getting negatve Int, and 0 for getting 0.
As far as I understand, c_size
should be 1 if size==0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact the else
should never be triggered: if self.value == 0
then self.is_C_int()
should return True, so we don't reach this if
in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fingolfin - oops, I should have looked at GAP's src/integer.h
(or is there any other source for docs?)
src/sage/libs/gap/element.pyx
Outdated
r""" | ||
TESTS:: | ||
|
||
sage: n = libgap.eval('One(ZmodnZ(123)) * 13') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also do this:
sage: n = libgap.eval('One(ZmodnZ(123)) * 13') | |
sage: n = libgap.eval('ZmodnZObj(13, 123))') |
or, if libgap makes it easy to directly call GAP functions with small int arguments, then something like this:
sage: n = libgap.eval('One(ZmodnZ(123)) * 13') | |
sage: n = libgap.ZmodnZObj(13, 123) |
Looking at this file some more, I see various GAP commands being surrounded by |
Documentation preview for this PR (built with commit 2bcbfd6; changes) is ready! 🎉 |
Not all gap commands can cause garbage collection to run (roughly the C macros don't, but thats not a 100% reliable rule). If in doubt its better to add the |
Fair enough, I added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me
This PR implements some more sensible conversions for libGAP integers and modular integers.
For integers, we fix a TODO to convert long libGAP integers by directly accessing their underlying GMP integers. The current implementation instead uses a decimal string representation as an intermediate step.
For modular integers, we implement a
lift
method; this allows the.sage
method to use a more direct coercion into the appropriate ring.