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

Add pari_PRIMES declaration #165

Merged
merged 1 commit into from
Oct 7, 2024
Merged

Conversation

antonio-rojas
Copy link
Contributor

Needed to port sagemath to work with pari 2.17

Needed to port sagemath to work with pari 2.17
@antonio-rojas
Copy link
Contributor Author

Note that cypari doesn't build against 2.17.0 due to an upstream bug:
https://pari.math.u-bordeaux.fr/cgi-bin/bugreport.cgi?bug=2575

Copy link
Member

@dimpase dimpase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an innocent change in header files only

@fchapoton fchapoton merged commit 064120a into sagemath:master Oct 7, 2024
94 of 119 checks passed
@tornaria
Copy link
Contributor

It seems the "right" (portable / upstream supported) way of iterating over primes involves functions forprime_init() and forprime_next().

In sagemath, something like:

--- a/src/sage/libs/pari/convert_sage.pyx
+++ b/src/sage/libs/pari/convert_sage.pyx
@@ -574,16 +574,14 @@ cpdef list pari_prime_range(long c_start, long c_stop, bint py_ints=False):
         [2, 3, 5, 7, 11, 13, 17]
     """
     cdef long p = 0
-    cdef byteptr pari_prime_ptr = diffptr
+    cdef forprime_t T
     res = []
-    while p < c_start:
-        NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
-    while p < c_stop:
+    u_forprime_init(&T, max(c_start, 0), max(c_stop-1,0))
+    while p := u_forprime_next(&T):
         if py_ints:
             res.append(p)
         else:
             z = <Integer>PY_NEW(Integer)
             mpz_set_ui(z.value, p)
             res.append(z)
-        NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
     return res

However, this fails to build because struct forprime_t is not correctly defined in cypari2. Something like:

--- a/cypari2/types.pxd
+++ b/cypari2/types.pxd
@@ -119,7 +119,7 @@ cdef extern from "pari/pari.h":
     struct nfmaxord_t
     struct forcomposite_t
     struct forpart_t
-    struct forprime_t
+    ctypedef struct forprime_t: pass
     struct forvec_t
     struct gp_context
     struct nfbasic_t

seems to be needed.

@dimpase
Copy link
Member

dimpase commented Nov 18, 2024

+    ctypedef struct forprime_t: pass

I don't grok this ctypedef. I thought it's only ctypedef Foo Bar, to use Bar instead of Foo?

@culler
Copy link

culler commented Nov 18, 2024

You are confusing the C typedef statement with the Cython ctypedef statement.

The C statement "typedef foo bar" allows bar to be used as an alias for the type foo.

The purpose of the Cython ctypedef statement is to inform the Cython compiler about typedefs that appear in C header files included by the C code generated by the Cython compiler. The idea is to tell the Cython compiler all it needs to know about a C type and nothing more. If the C code generated by the Cython compiler will only refer to a forprime_t as a struct, meaning that it will never access any fields of that struct, then the minimal ctypedef command which you do not grok is sufficient. It informs Cython that a C struct named forprime_t exists. That is all it needs to know. It does not need to know the names of any fields in that struct.

@dimpase
Copy link
Member

dimpase commented Nov 18, 2024

In my defence I can say that I can't find ctypedef in Cython docs proper - only in examples, without any : foobar part.

@culler
Copy link

culler commented Nov 18, 2024

The Cython documentation is not very helpful. Its intended audience consists of people who don't read documentation.

The colon is required for structs, but not for simple types like unsigned int.

There is no need to defend yourself; you are welcome.

@tornaria
Copy link
Contributor

See:
http://docs.cython.org/en/latest/src/userguide/external_C_code.html#styles-of-struct-union-and-enum-declaration

It seems that just cdef struct Foo declares a struct that cannot be used except by pointer (however, this prevents creating an actualy object to take the address of).

An empty struct declaration is ok if we add : pass (these are really opaque structs, we don't need to know what's inside).

In summary, the whole block of struct foo declarations are not very useful. It seems there are two possible styles depending on how the type is defined in pari headers, e.g.:

    struct bb_group: pass
    [...]
    ctypedef struct forprime_t: pass
    [...]

In the first statement, I think the cdef is implicit, but I think it also won't work without the : pass. OTOH, I'm not sure bb_group will be useful at all without declaration of members, maybe we should take it easy and only do now the ones that are clear (and for which we can write simple self-contained tests that work).

I couldn't find a way to override this declaration in sagemath without changing cypari2, which would make handling the dependency easier. If anyone sees a way, it might be nice to get this into sagemath 10.5. If it needs an update to cypari2, then it's probably better to wait until 10.6 (but we should do a cypari2 release soon, is someone in charge?).

@dimpase
Copy link
Member

dimpase commented Nov 19, 2024

we should do a cypari2 release soon, is someone in charge?

@videlec ?

@roed314
Copy link

roed314 commented Nov 19, 2024

Vincent is no longer acting as maintainer for cypari2 (see this thread on sage-devel). I appreciate the time he's taken recently to help figure out what steps might help with #107.

I'd be motivated to help resolve #107, but in practice I'm unlikely to have much time to devote to it in the next several months.

@dimpase
Copy link
Member

dimpase commented Nov 19, 2024

I can help maintain pplpy and cypari2. Feel free to make me a maintainer/admin on these. And on cysignals.
(OK, this is done)

@dimpase
Copy link
Member

dimpase commented Nov 19, 2024

@tornaria - I can now deal with merging PRs and make releases, please go ahead with your proposed changes.

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

Successfully merging this pull request may close these issues.

6 participants