1

I am trying to write a Cython function which returns an array of mpz_t (number type of integers from the gmpy2 library). I can successfully define a fixed-size array:

cpdef void pows(mpz number):
    cdef:
        int x
        mpz_t powers[10]

    for 1 <= x <= 10:
        # do stuff and store in powers

however I want to be able to define a dynamic-size array of mpz_t (i.e. to start with it empty and add values to it) and then return powers from this function. When defining the return type, so far I have tried:

cpdef mpt_t pows(mpz number):

cpdef mpz_t[:] pows(mpz number):

cpdef mpz_t * pows(mpz number):

none of which have worked. Is this possible to do?

4
  • cpdef functions need to be callable from Python, which means that any return type must either be a Python object or just be possible to convert to a Python object. Commented Jun 24, 2024 at 6:03
  • Okay, how do I make this compatible with an array of mpz_t? Commented Jun 24, 2024 at 6:45
  • Probably declare cdef class that wraps an mpz_t (and return a list of them) or declare a cdef class that wraps an array of mpz_t. Commented Jun 24, 2024 at 7:12
  • Any ideas how to do this in a way that can be passed back to python (and is still very efficient)? Commented Jun 24, 2024 at 8:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.