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

Support gr objects in flint_printf #2226

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions doc/source/flint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,26 @@ Input/Output
We currently support printing vectors of pointers to the following base
types: :type:`slong`, :type:`ulong`, :type:`fmpz`, :type:`fmpq`,
:type:`mag_struct`, :type:`arf_struct`, :type:`arb_struct` and
:type:`acb_struct`.
:type:`acb_struct`. In this case the nonnegative length of the vector
must be passed as a second parameter following the pointer.
Warning: the length parameter must be passed as a :type:`slong`,
not ``int``.

We also support printing matrices of the following types:
:type:`nmod_mat_t`, :type:`fmpz_mat_t`, :type:`fmpq_mat_t`,
:type:`arb_mat_t` and :type:`acb_mat_t`.

Finally, we currently support printing polynomial of the following types:
We also support printing polynomial of the following types:
:type:`nmod_poly_t`, :type:`fmpz_poly_t`, :type:`fmpq_poly_t`,
:type:`arb_poly_t` and :type:`acb_poly_t`.

Finally, we support printing generic elements of type :type:`gr_ptr`
as well as :type:`gr_poly_t` and :type:`gr_mat_t`. For
each of these types, the object to be printed must be followed
by the corresponding :type:`gr_ctx_t`. The context object itself
can also printed as a standalone object.


.. code-block:: c

ulong bulong;
Expand All @@ -275,6 +285,8 @@ Input/Output
fmpz_mod_ctx_t bfmpz_mod_ctx;
mpz_t bmpz;
mpq_t bmpq;
gr_ctx_t bgr_ctx;
gr_ptr bgr;

/* Initialize and set variables */

Expand All @@ -290,7 +302,9 @@ Input/Output
"nmod: %{nmod}\n"
"fmpz_mod_ctx: %{fmpz_mod_ctx}\n"
"mpz: %{mpz}\n"
"mpq: %{mpq}\n",
"mpq: %{mpq}\n"
"gr: %{gr}\n",
"gr: %{gr_ctx}\n",
bulong,
bslong,
bfmpz,
Expand All @@ -302,7 +316,9 @@ Input/Output
bnmod,
bfmpz_mod_ctx,
bmpz,
bmpq);
bmpq,
gr, bgr_ctx,
gr_ctx);

.. code-block:: c

Expand All @@ -315,6 +331,7 @@ Input/Output
arf_ptr varf; slong varf_len;
arb_ptr varb; slong varb_len;
acb_ptr vacb; slong vacb_len;
gr_ptr vgr; slong vgr_len; gr_ctx_t vgr_ctx;

/* Initialize and set variables */

Expand All @@ -327,14 +344,16 @@ Input/Output
"arf vector: %{arf*}\n"
"arb vector: %{arb*}\n"
"acb vector: %{acb*}\n"
"gr vector: %{gr*}\n"
vslong, vslong_len, /* They require a vector length specifier */
vnmod, vnmod_len,
vfmpz, vfmpz_len,
vfmpq, vfmpq_len,
vmag, vmag_len,
varf, varf_len,
varb, varb_len,
vacb, vacb_len);
vacb, vacb_len,
vgr, vgr_len, vgr_ctx);

.. code-block:: c

Expand All @@ -344,6 +363,7 @@ Input/Output
fmpq_mat_t mfmpq;
arb_mat_t marb;
acb_mat_t macb;
gr_mat_t mgr; gr_ctx_t mgr_ctx;

/* Initialize and set variables */

Expand All @@ -352,14 +372,16 @@ Input/Output
"fmpz matrix: %{fmpz_mat}\n"
"fmpz_mod matrix: %{fmpz_mod_mat}\n"
"fmpq matrix: %{fmpq_mat}\n"
"arb vector: %{arb_mat}\n"
"acb vector: %{acb_mat}\n"
"arb matrix: %{arb_mat}\n"
"acb matrix: %{acb_mat}\n"
"gr matrix: %{gr_mat}\n"
mnmod,
mfmpz,
mfmpz_mod,
mfmpq,
marb,
macb);
macb,
mgr, mgr_ctx);

.. code-block:: c

Expand All @@ -369,6 +391,7 @@ Input/Output
fmpq_poly_t pfmpq;
arb_poly_t parb;
acb_poly_t pacb;
gr_poly_t pgr; gr_ctx_t pgr_ctx;

/* Initialize and set variables */

Expand All @@ -384,7 +407,8 @@ Input/Output
pfmpz_mod,
pfmpq,
parb,
pacb);
pacb,
pgr, pgr_ctx);

.. note::

Expand Down
54 changes: 54 additions & 0 deletions src/generic_files/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#include "arf_types.h"
#include "arb.h"
#include "acb.h"
#include "gr.h"
#include "gr_vec.h"
#include "gr_poly.h"
#include "gr_mat.h"

int _gr_mat_write(gr_stream_t out, const gr_mat_t mat, int linebreaks, gr_ctx_t ctx);

/* Helper functions **********************************************************/

Expand Down Expand Up @@ -692,6 +698,54 @@
res += __mpq_fprint(fs, va_arg(vlist, mpq_srcptr));
ip += STRING_LENGTH("mpq}");
}
else if (IS_FLINT_BASE_TYPE(ip, "gr"))
{
gr_stream_t out;
gr_stream_init_file(out, fs);

if (IS_FLINT_TYPE(ip, "gr"))
{
gr_srcptr elem = va_arg(vlist, gr_srcptr);
gr_ctx_struct * ctx = va_arg(vlist, gr_ctx_struct *);
GR_MUST_SUCCEED(gr_write(out, elem, ctx));
res += out->len;
ip += STRING_LENGTH("gr}");
}
else if (IS_FLINT_TYPE(ip, "gr*"))
{
gr_srcptr elem = va_arg(vlist, gr_srcptr);
slong len = va_arg(vlist, slong);
gr_ctx_struct * ctx = va_arg(vlist, gr_ctx_struct *);
GR_MUST_SUCCEED(_gr_vec_write(out, elem, len, ctx));
res += out->len;
ip += STRING_LENGTH("gr*}");
}
else if (IS_FLINT_TYPE(ip, "gr_poly"))
{
const gr_poly_struct * elem = va_arg(vlist, const gr_poly_struct *);
gr_ctx_struct * ctx = va_arg(vlist, gr_ctx_struct *);
GR_MUST_SUCCEED(gr_poly_write(out, elem, "x", ctx));
res += out->len;
ip += STRING_LENGTH("gr_poly}");
}
else if (IS_FLINT_TYPE(ip, "gr_mat"))
{
const gr_mat_struct * elem = va_arg(vlist, const gr_mat_struct *);
gr_ctx_struct * ctx = va_arg(vlist, gr_ctx_struct *);
GR_MUST_SUCCEED(_gr_mat_write(out, elem, 0, ctx));
res += out->len;
ip += STRING_LENGTH("gr_mat}");
}
else if (IS_FLINT_TYPE(ip, "gr_ctx"))
{
gr_ctx_struct * ctx = va_arg(vlist, gr_ctx_struct *);
GR_MUST_SUCCEED(gr_ctx_write(out, ctx));
res += out->len;
ip += STRING_LENGTH("gr_ctx}");
}
else
goto printpercentcurlybracket;

Check warning on line 747 in src/generic_files/io.c

View check run for this annotation

Codecov / codecov/patch

src/generic_files/io.c#L747

Added line #L747 was not covered by tests
}
else
{
printpercentcurlybracket:
Expand Down
10 changes: 5 additions & 5 deletions src/gr.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ void gr_stream_init_file(gr_stream_t out, FILE * fp);
#endif

void gr_stream_init_str(gr_stream_t out);
void gr_stream_write(gr_stream_t out, const char * s);
void gr_stream_write_si(gr_stream_t out, slong x);
void gr_stream_write_ui(gr_stream_t out, ulong x);
void gr_stream_write_free(gr_stream_t out, char * s);
void gr_stream_write_fmpz(gr_stream_t out, const fmpz_t x);
int gr_stream_write(gr_stream_t out, const char * s);
int gr_stream_write_si(gr_stream_t out, slong x);
int gr_stream_write_ui(gr_stream_t out, ulong x);
int gr_stream_write_free(gr_stream_t out, char * s);
int gr_stream_write_fmpz(gr_stream_t out, const fmpz_t x);

#define GR_MUST_SUCCEED(expr) do { if ((expr) != GR_SUCCESS) { flint_throw(FLINT_ERROR, "GR_MUST_SUCCEED failure: %s", __FILE__); } } while (0)
#define GR_IGNORE(expr) do { int ___unused = (expr); (void) ___unused; } while (0)
Expand Down
Loading