Skip to content

Expose content and zpoly of fmpq_mpoly #419

Description

@magv

Hi, all. First of all, thanks a lot for python bindings, these are very useful. So, FLINT stores fmpq_mpoly as an fmpz_mpoly and an fmpq content; these are accessible via fmpq_mpoly_content_ref and fmpq_mpoly_zpoly_ref. Would it be possible to expose these values in python-flint?

Of course, exposing mutable references would be all sorts of complicated, but just adding content() and zpoly() methods that return copies would already be great. Currently (as of version 0.9) it seems like I can only do this conversion via to_dict/from_dict, which is infinitely slow compared to the proper solution.

Something like this maybe?

diff --git a/src/flint/types/fmpq_mpoly.pyx b/src/flint/types/fmpq_mpoly.pyx
--- a/src/flint/types/fmpq_mpoly.pyx
+++ b/src/flint/types/fmpq_mpoly.pyx
@@ -17,7 +17,7 @@ from flint.types.fmpz_vec cimport fmpz_vec
 from flint.types.fmpq_vec cimport fmpq_vec

 from flint.types.fmpz cimport fmpz, any_as_fmpz
-from flint.types.fmpz_mpoly cimport fmpz_mpoly
+from flint.types.fmpz_mpoly cimport fmpz_mpoly, fmpz_mpoly_ctx, create_fmpz_mpoly

 from flint.flintlib.functions.fmpq cimport fmpq_set, fmpq_one
 from flint.flintlib.functions.fmpq_mpoly cimport (
@@ -785,6 +785,16 @@ cdef class fmpq_mpoly(flint_mpoly):
         fmpq_mpoly_term_content(res.val, self.val, self.ctx.val)
         return res

+    def content(self):
+        cdef fmpq res = fmpq.__new__(fmpq)
+        fmpq_set(res.val, self.val.content)
+        return res
+
+    def zpoly(self):
+        cdef fmpz_mpoly_ctx zctx = fmpz_mpoly_ctx.from_context(self.ctx)
+        cdef fmpz_mpoly res = create_fmpz_mpoly(zctx)
+        fmpz_mpoly_set(res.val, self.val.zpoly, zctx.val)
+        return res
+
     def resultant(self, other, var):
         """
         Return the resultant of ``self`` and ``other`` with respect to variable ``var``.

diff --git a/src/flint/types/fmpq_mpoly.pyi b/src/flint/types/fmpq_mpoly.pyi
index 6dec649..e8b3b9c 100644
--- a/src/flint/types/fmpq_mpoly.pyi
+++ b/src/flint/types/fmpq_mpoly.pyi
@@ -102,6 +102,9 @@ class fmpq_mpoly(flint_mpoly[fmpq_mpoly_ctx, fmpq, ifmpq]):
     def xgcd(self, other: fmpq_mpoly, /) -> fmpq: ...
     def term_content(self) -> fmpq_mpoly: ...

+    def content(self) -> fmpq: ...
+    def zpoly(self) -> fmpz_mpoly: ...
+
     def factor(self) -> tuple[fmpq, list[tuple[fmpq_mpoly, int]]]: ...
     def factor_squarefree(self) -> tuple[fmpq, list[tuple[fmpq_mpoly, int]]]: ...

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions