Skip to content

Commit

Permalink
Support gr objects in flint_printf (#2226)
Browse files Browse the repository at this point in the history
* Support gr objects in flint_printf
  • Loading branch information
fredrik-johansson authored Feb 1, 2025
1 parent b4b6c21 commit a71fd14
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 49 deletions.
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 @@ int flint_vfprintf(FILE * fs, const char * ip, va_list vlist)
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;
}
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

0 comments on commit a71fd14

Please sign in to comment.