From f67980cd64d20a62059f394c739cf2c221c1fed4 Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Sat, 11 Jan 2025 16:02:02 +0100 Subject: [PATCH 1/4] refactor gr_mpoly context object --- src/gr/mpoly.c | 246 +++++++++++++++++++++++++------------------------ 1 file changed, 128 insertions(+), 118 deletions(-) diff --git a/src/gr/mpoly.c b/src/gr/mpoly.c index 351aedb339..8d3622a094 100644 --- a/src/gr/mpoly.c +++ b/src/gr/mpoly.c @@ -19,198 +19,202 @@ typedef struct { - gr_ctx_struct * base_ring; - mpoly_ctx_t mctx; + gr_ctx_struct * cctx; + mpoly_ctx_struct * mctx; char ** vars; -} -_gr_gr_mpoly_ctx_t; +} _gr_mpoly_ctx_struct; + +typedef gr_ctx_struct gr_mpoly_ctx_struct; +typedef gr_mpoly_ctx_struct gr_mpoly_ctx_t[1]; -#define MPOLYNOMIAL_CTX(ring_ctx) ((_gr_gr_mpoly_ctx_t *)(GR_CTX_DATA_AS_PTR(ring_ctx))) -#define MPOLYNOMIAL_ELEM_CTX(ring_ctx) (MPOLYNOMIAL_CTX(ring_ctx)->base_ring) -#define MPOLYNOMIAL_MCTX(ring_ctx) (MPOLYNOMIAL_CTX(ring_ctx)->mctx) +#define GR_MPOLY_MCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->mctx) +#define GR_MPOLY_CCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->cctx) +#define GR_MPOLY_VARS(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->vars) +#define GR_MPOLY_NVARS(ctx) (GR_MPOLY_MCTX(ctx)->nvars) -int _gr_gr_mpoly_ctx_write(gr_stream_t out, gr_ctx_t ctx) + +int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx) { gr_stream_write(out, "Ring of multivariate polynomials over "); - gr_ctx_write(out, MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_ctx_write(out, GR_MPOLY_CCTX(ctx)); gr_stream_write(out, " in "); - gr_stream_write_si(out, MPOLYNOMIAL_MCTX(ctx)->nvars); + gr_stream_write_si(out, GR_MPOLY_NVARS(ctx)); gr_stream_write(out, " variables"); - if (MPOLYNOMIAL_MCTX(ctx)->ord == ORD_LEX) + if (GR_MPOLY_MCTX(ctx)->ord == ORD_LEX) gr_stream_write(out, ", lex order"); - else if (MPOLYNOMIAL_MCTX(ctx)->ord == ORD_DEGLEX) + else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGLEX) gr_stream_write(out, ", deglex order"); - else if (MPOLYNOMIAL_MCTX(ctx)->ord == ORD_DEGREVLEX) + else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGREVLEX) gr_stream_write(out, ", degrevlex order"); return GR_SUCCESS; } void -_gr_gr_mpoly_ctx_clear(gr_ctx_t ctx) +gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx) { - if (MPOLYNOMIAL_CTX(ctx)->vars != NULL) + if (GR_MPOLY_VARS(ctx) != NULL) { slong i; - for (i = 0; i < MPOLYNOMIAL_MCTX(ctx)->nvars; i++) - flint_free(MPOLYNOMIAL_CTX(ctx)->vars[i]); - flint_free(MPOLYNOMIAL_CTX(ctx)->vars); + for (i = 0; i < GR_MPOLY_NVARS(ctx); i++) + flint_free(GR_MPOLY_VARS(ctx)[i]); + flint_free(GR_MPOLY_VARS(ctx)); } - mpoly_ctx_clear(MPOLYNOMIAL_MCTX(ctx)); - flint_free(GR_CTX_DATA_AS_PTR(ctx)); + mpoly_ctx_clear(GR_MPOLY_MCTX(ctx)); + flint_free(GR_MPOLY_MCTX(ctx)); } int -_gr_gr_mpoly_ctx_set_gen_names(gr_ctx_t ctx, const char ** s) +gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s) { slong i, nvars, len; - nvars = MPOLYNOMIAL_MCTX(ctx)->nvars; + nvars = GR_MPOLY_NVARS(ctx); - if (MPOLYNOMIAL_CTX(ctx)->vars == NULL) - { - MPOLYNOMIAL_CTX(ctx)->vars = flint_malloc(nvars * sizeof(char *)); - for (i = 0; i < nvars; i++) - MPOLYNOMIAL_CTX(ctx)->vars[i] = NULL; - } - else + if (GR_MPOLY_VARS(ctx) == NULL) { + GR_MPOLY_VARS(ctx) = flint_malloc(nvars * sizeof(char *)); for (i = 0; i < nvars; i++) - flint_free(MPOLYNOMIAL_CTX(ctx)->vars[i]); + GR_MPOLY_VARS(ctx)[i] = NULL; } for (i = 0; i < nvars; i++) { len = strlen(s[i]); - MPOLYNOMIAL_CTX(ctx)->vars[i] = flint_realloc(MPOLYNOMIAL_CTX(ctx)->vars[i], len + 1); - memcpy(MPOLYNOMIAL_CTX(ctx)->vars[i], s[i], len + 1); + GR_MPOLY_VARS(ctx)[i] = flint_realloc(GR_MPOLY_VARS(ctx)[i], len + 1); + memcpy(GR_MPOLY_VARS(ctx)[i], s[i], len + 1); } return GR_SUCCESS; } -/* todo: everything is a ring when there are 0 vars? */ truth_t -_gr_gr_mpoly_ctx_is_ring(gr_ctx_t ctx) +gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx) { - return gr_ctx_is_ring(MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_ctx_is_ring(GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_ctx_is_commutative_ring(gr_ctx_t ctx) +gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx) { - return gr_ctx_is_commutative_ring(MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_ctx_is_zero_ring(GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_ctx_is_integral_domain(gr_ctx_t ctx) +gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx) { - return gr_ctx_is_integral_domain(MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_ctx_is_commutative_ring(GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_ctx_is_field(gr_ctx_t ctx) +gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx) { - if (MPOLYNOMIAL_MCTX(ctx)->nvars == 0) - return gr_ctx_is_field(MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_ctx_is_integral_domain(GR_MPOLY_CCTX(ctx)); +} - return T_FALSE; +truth_t +gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx) +{ + if (GR_MPOLY_NVARS(ctx) == 0) + return gr_ctx_is_field(GR_MPOLY_CCTX(ctx)); + else + return T_FALSE; } truth_t -_gr_gr_mpoly_ctx_is_threadsafe(gr_ctx_t ctx) +gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx) { - return gr_ctx_is_threadsafe(MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_ctx_is_threadsafe(GR_MPOLY_CCTX(ctx)); } void -_gr_gr_mpoly_init(gr_mpoly_t res, gr_ctx_t ctx) +_gr_gr_mpoly_init(gr_mpoly_t res, gr_mpoly_ctx_t ctx) { - gr_mpoly_init(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_mpoly_init(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } void -_gr_gr_mpoly_clear(gr_mpoly_t res, gr_ctx_t ctx) +_gr_gr_mpoly_clear(gr_mpoly_t res, gr_mpoly_ctx_t ctx) { - gr_mpoly_clear(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_mpoly_clear(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } void -_gr_gr_mpoly_swap(gr_mpoly_t poly1, gr_mpoly_t poly2, gr_ctx_t ctx) +_gr_gr_mpoly_swap(gr_mpoly_t poly1, gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) { - gr_mpoly_swap(poly1, poly2, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_mpoly_swap(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } void -_gr_gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_ctx_t ctx) +_gr_gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { *res = *poly; } int -_gr_gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_ctx_t ctx) +_gr_gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) { - return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } slong -_gr_gr_mpoly_length(const gr_mpoly_t x, gr_ctx_t ctx) +_gr_gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) { return x->length; } int -_gr_gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_ctx_t ctx) +_gr_gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { - return gr_mpoly_write_pretty(out, poly, (const char **) MPOLYNOMIAL_CTX(ctx)->vars, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_write_pretty(out, poly, (const char **) GR_MPOLY_VARS(ctx), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_equal(const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_ctx_t ctx) +_gr_gr_mpoly_equal(const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) { - return gr_mpoly_equal(poly1, poly2, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_equal(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_is_zero(const gr_mpoly_t poly, gr_ctx_t ctx) +_gr_gr_mpoly_is_zero(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { - return gr_mpoly_is_zero(poly, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_is_zero(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } truth_t -_gr_gr_mpoly_is_one(const gr_mpoly_t poly, gr_ctx_t ctx) +_gr_gr_mpoly_is_one(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { - return gr_mpoly_is_one(poly, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_is_one(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_gens(gr_vec_t res, gr_ctx_t ctx) +_gr_gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) { slong i, n; int status = GR_SUCCESS; - n = MPOLYNOMIAL_MCTX(ctx)->nvars; + n = GR_MPOLY_NVARS(ctx); gr_vec_set_length(res, n, ctx); for (i = 0; i < n; i++) - status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); return status; } int -_gr_gr_mpoly_gens_recursive(gr_vec_t vec, gr_ctx_t ctx) +_gr_gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) { int status; gr_vec_t vec1; slong i, n, m; /* Get generators of the element ring */ - gr_vec_init(vec1, 0, MPOLYNOMIAL_ELEM_CTX(ctx)); - status = gr_gens_recursive(vec1, MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_vec_init(vec1, 0, GR_MPOLY_CCTX(ctx)); + status = gr_gens_recursive(vec1, GR_MPOLY_CCTX(ctx)); n = vec1->length; - m = MPOLYNOMIAL_MCTX(ctx)->nvars; + m = GR_MPOLY_NVARS(ctx); gr_vec_set_length(vec, n + m, ctx); @@ -218,111 +222,112 @@ _gr_gr_mpoly_gens_recursive(gr_vec_t vec, gr_ctx_t ctx) for (i = 0; i < n; i++) { status |= gr_mpoly_set_scalar(gr_vec_entry_ptr(vec, i, ctx), - gr_vec_entry_srcptr(vec1, i, MPOLYNOMIAL_ELEM_CTX(ctx)), - MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_vec_entry_srcptr(vec1, i, GR_MPOLY_CCTX(ctx)), + GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } for (i = 0; i < m; i++) { status |= gr_mpoly_gen(((gr_mpoly_struct *) vec->entries) + n + i, - i, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + i, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } - gr_vec_clear(vec1, MPOLYNOMIAL_ELEM_CTX(ctx)); + gr_vec_clear(vec1, GR_MPOLY_CCTX(ctx)); return status; } int -_gr_gr_mpoly_zero(gr_mpoly_t res, gr_ctx_t ctx) +_gr_gr_mpoly_zero(gr_mpoly_t res, gr_mpoly_ctx_t ctx) { - return gr_mpoly_zero(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_one(gr_mpoly_t res, gr_ctx_t ctx) +_gr_gr_mpoly_one(gr_mpoly_t res, gr_mpoly_ctx_t ctx) { - return gr_mpoly_one(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_one(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_set(gr_mpoly_t res, const gr_mpoly_t mat, gr_ctx_t ctx) +_gr_gr_mpoly_set(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set(res, mat, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_set(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_set_si(gr_mpoly_t res, slong v, gr_ctx_t ctx) +_gr_gr_mpoly_set_si(gr_mpoly_t res, slong v, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set_si(res, v, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_set_si(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_set_ui(gr_mpoly_t res, ulong v, gr_ctx_t ctx) +_gr_gr_mpoly_set_ui(gr_mpoly_t res, ulong v, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set_ui(res, v, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_set_ui(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_set_fmpz(gr_mpoly_t res, const fmpz_t v, gr_ctx_t ctx) +_gr_gr_mpoly_set_fmpz(gr_mpoly_t res, const fmpz_t v, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set_fmpz(res, v, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_set_fmpz(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_set_fmpq(gr_mpoly_t res, const fmpq_t v, gr_ctx_t ctx) +_gr_gr_mpoly_set_fmpq(gr_mpoly_t res, const fmpq_t v, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set_fmpq(res, v, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_set_fmpq(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_neg(gr_mpoly_t res, const gr_mpoly_t mat, gr_ctx_t ctx) +_gr_gr_mpoly_neg(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) { - return gr_mpoly_neg(res, mat, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_neg(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_add(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_ctx_t ctx) +_gr_gr_mpoly_add(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) { if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - return gr_mpoly_add(res, poly1, poly2, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_add(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_sub(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_ctx_t ctx) +_gr_gr_mpoly_sub(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) { if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - return gr_mpoly_sub(res, poly1, poly2, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_sub(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } int -_gr_gr_mpoly_mul(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_ctx_t ctx) +_gr_gr_mpoly_mul(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) { if (poly1->length * poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - return gr_mpoly_mul(res, poly1, poly2, MPOLYNOMIAL_MCTX(ctx), MPOLYNOMIAL_ELEM_CTX(ctx)); + return gr_mpoly_mul(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); } -int _gr__gr_gr_mpoly_methods_initialized = 0; +int _gr_mpoly_methods_initialized = 0; -gr_static_method_table _gr__gr_gr_mpoly_methods; +gr_static_method_table _gr_mpoly_methods; -gr_method_tab_input _gr__gr_gr_mpoly_methods_input[] = +gr_method_tab_input _gr_mpoly_methods_input[] = { - {GR_METHOD_CTX_WRITE, (gr_funcptr) _gr_gr_mpoly_ctx_write}, - {GR_METHOD_CTX_CLEAR, (gr_funcptr) _gr_gr_mpoly_ctx_clear}, - {GR_METHOD_CTX_IS_RING, (gr_funcptr) _gr_gr_mpoly_ctx_is_ring}, - {GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) _gr_gr_mpoly_ctx_is_commutative_ring}, - {GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) _gr_gr_mpoly_ctx_is_integral_domain}, - {GR_METHOD_CTX_IS_FIELD, (gr_funcptr) _gr_gr_mpoly_ctx_is_field}, - {GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) _gr_gr_mpoly_ctx_is_threadsafe}, - {GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) _gr_gr_mpoly_ctx_set_gen_names}, + {GR_METHOD_CTX_WRITE, (gr_funcptr) gr_mpoly_ctx_write}, + {GR_METHOD_CTX_CLEAR, (gr_funcptr) gr_mpoly_ctx_clear}, + {GR_METHOD_CTX_IS_RING, (gr_funcptr) gr_mpoly_ctx_is_ring}, + {GR_METHOD_CTX_IS_ZERO_RING, (gr_funcptr) gr_mpoly_ctx_is_zero_ring}, + {GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) gr_mpoly_ctx_is_commutative_ring}, + {GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) gr_mpoly_ctx_is_integral_domain}, + {GR_METHOD_CTX_IS_FIELD, (gr_funcptr) gr_mpoly_ctx_is_field}, + {GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) gr_mpoly_ctx_is_threadsafe}, + {GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) gr_mpoly_ctx_set_gen_names}, {GR_METHOD_INIT, (gr_funcptr) _gr_gr_mpoly_init}, {GR_METHOD_CLEAR, (gr_funcptr) _gr_gr_mpoly_clear}, {GR_METHOD_SWAP, (gr_funcptr) _gr_gr_mpoly_swap}, @@ -350,23 +355,28 @@ gr_method_tab_input _gr__gr_gr_mpoly_methods_input[] = {0, (gr_funcptr) NULL}, }; +/* todo: first arg as gr_mpoly_ctx_t */ void gr_ctx_init_gr_mpoly(gr_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord) { ctx->which_ring = GR_CTX_GR_MPOLY; ctx->sizeof_elem = sizeof(gr_mpoly_struct); - GR_CTX_DATA_AS_PTR(ctx) = flint_malloc(sizeof(_gr_gr_mpoly_ctx_t)); ctx->size_limit = WORD_MAX; - MPOLYNOMIAL_ELEM_CTX(ctx) = base_ring; - mpoly_ctx_init(MPOLYNOMIAL_MCTX(ctx), nvars, ord); - MPOLYNOMIAL_CTX(ctx)->vars = NULL; + /* by reference */ + GR_MPOLY_CCTX(ctx) = base_ring; + + /* allocated here */ + GR_MPOLY_MCTX(ctx) = flint_malloc(sizeof(mpoly_ctx_struct)); + mpoly_ctx_init(GR_MPOLY_MCTX(ctx), nvars, ord); + + GR_MPOLY_VARS(ctx) = NULL; - ctx->methods = _gr__gr_gr_mpoly_methods; + ctx->methods = _gr_mpoly_methods; - if (!_gr__gr_gr_mpoly_methods_initialized) + if (!_gr_mpoly_methods_initialized) { - gr_method_tab_init(_gr__gr_gr_mpoly_methods, _gr__gr_gr_mpoly_methods_input); - _gr__gr_gr_mpoly_methods_initialized = 1; + gr_method_tab_init(_gr_mpoly_methods, _gr_mpoly_methods_input); + _gr_mpoly_methods_initialized = 1; } } From 66909691b7316ec9f7116caf630d4a03acae314c Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Sat, 11 Jan 2025 22:26:54 +0100 Subject: [PATCH 2/4] wip refactor gr_mpoly --- src/gr/mpoly.c | 19 +- src/gr_mpoly.h | 210 +++++++++++++- src/gr_mpoly/get_coeff_scalar_fmpz.c | 15 +- src/gr_mpoly/get_coeff_scalar_ui.c | 8 +- src/gr_mpoly/set_coeff_scalar_fmpz.c | 25 +- src/gr_mpoly/set_coeff_scalar_ui.c | 26 +- src/gr_mpoly/test/t-add_sub.c | 398 ++++++++------------------- src/gr_mpoly/test/t-get_set_coeff.c | 74 ++--- src/gr_mpoly/test/t-mul_johnson.c | 72 ++--- src/gr_mpoly/test/t-mul_monomial.c | 72 ++--- 10 files changed, 460 insertions(+), 459 deletions(-) diff --git a/src/gr/mpoly.c b/src/gr/mpoly.c index 8d3622a094..395453b65b 100644 --- a/src/gr/mpoly.c +++ b/src/gr/mpoly.c @@ -17,20 +17,7 @@ #include "gr_mpoly.h" #include "gr_generic.h" -typedef struct -{ - gr_ctx_struct * cctx; - mpoly_ctx_struct * mctx; - char ** vars; -} _gr_mpoly_ctx_struct; - -typedef gr_ctx_struct gr_mpoly_ctx_struct; -typedef gr_mpoly_ctx_struct gr_mpoly_ctx_t[1]; -#define GR_MPOLY_MCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->mctx) -#define GR_MPOLY_CCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->cctx) -#define GR_MPOLY_VARS(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->vars) -#define GR_MPOLY_NVARS(ctx) (GR_MPOLY_MCTX(ctx)->nvars) int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx) @@ -380,3 +367,9 @@ gr_ctx_init_gr_mpoly(gr_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const orderi _gr_mpoly_methods_initialized = 1; } } + +void +gr_mpoly_ctx_init_rand(gr_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars) +{ + gr_ctx_init_gr_mpoly(ctx, base_ring, n_randint(state, max_nvars + 1), mpoly_ordering_randtest(state)); +} diff --git a/src/gr_mpoly.h b/src/gr_mpoly.h index aa6a7e3a6d..7c187290e3 100644 --- a/src/gr_mpoly.h +++ b/src/gr_mpoly.h @@ -44,6 +44,25 @@ gr_mpoly_struct; typedef gr_mpoly_struct gr_mpoly_t[1]; +typedef struct +{ + gr_ctx_struct * cctx; + mpoly_ctx_struct * mctx; + char ** vars; +} _gr_mpoly_ctx_struct; + +typedef gr_ctx_struct gr_mpoly_ctx_struct; +typedef gr_mpoly_ctx_struct gr_mpoly_ctx_t[1]; + +#define GR_MPOLY_MCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->mctx) +#define GR_MPOLY_CCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->cctx) +#define GR_MPOLY_VARS(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->vars) +#define GR_MPOLY_NVARS(ctx) (GR_MPOLY_MCTX(ctx)->nvars) + +/* Context object */ + +void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars); +void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx); /* Memory management */ @@ -161,6 +180,7 @@ int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint /* Input and output */ +/* todo: vars stored in context object */ int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx); int gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx); @@ -193,20 +213,20 @@ truth_t gr_mpoly_is_one(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cct /* Coefficient/exponent access */ -WARN_UNUSED_RESULT int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_get_coeff_scalar_ui(gr_ptr c, const gr_mpoly_t A, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_get_coeff_scalar_ui(gr_ptr c, const gr_mpoly_t A, const ulong * exp, gr_mpoly_ctx_t ctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_fmpz(gr_mpoly_t A, ulong c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_fmpz(gr_mpoly_t A, slong c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpz_fmpz(gr_mpoly_t A, const fmpz_t c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpq_fmpz(gr_mpoly_t A, const fmpq_t c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_fmpz(gr_mpoly_t A, ulong c, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_fmpz(gr_mpoly_t A, slong c, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpz_fmpz(gr_mpoly_t A, const fmpz_t c, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpq_fmpz(gr_mpoly_t A, const fmpq_t c, const fmpz * exp, gr_mpoly_ctx_t ctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, gr_srcptr c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_ui(gr_mpoly_t A, ulong c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_ui(gr_mpoly_t A, slong c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpz_ui(gr_mpoly_t A, const fmpz_t c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpq_ui(gr_mpoly_t A, const fmpq_t c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, gr_srcptr c, const ulong * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_ui(gr_mpoly_t A, ulong c, const ulong * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_ui(gr_mpoly_t A, slong c, const ulong * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpz_ui(gr_mpoly_t A, const fmpz_t c, const ulong * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpq_ui(gr_mpoly_t A, const fmpq_t c, const ulong * exp, gr_mpoly_ctx_t ctx); /* Arithmetic */ @@ -224,6 +244,172 @@ WARN_UNUSED_RESULT int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c WARN_UNUSED_RESULT int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); WARN_UNUSED_RESULT int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); +/* Refactoring */ + +FLINT_FORCE_INLINE void +NEW_gr_mpoly_init(gr_mpoly_t res, gr_mpoly_ctx_t ctx) +{ + gr_mpoly_init(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE void +NEW_gr_mpoly_clear(gr_mpoly_t res, gr_mpoly_ctx_t ctx) +{ + gr_mpoly_clear(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE void +NEW_gr_mpoly_swap(gr_mpoly_t poly1, gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + gr_mpoly_swap(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE void +NEW_gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + *res = *poly; +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_randtest_bits(gr_mpoly_t res, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_randtest_bits(res, state, length, exp_bits, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE slong +NEW_gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) +{ + return x->length; +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_write_pretty(out, poly, (const char **) GR_MPOLY_VARS(ctx), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE truth_t +NEW_gr_mpoly_equal(const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_equal(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE truth_t +NEW_gr_mpoly_is_zero(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_is_zero(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE truth_t +NEW_gr_mpoly_is_one(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_is_one(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_zero(gr_mpoly_t res, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_one(gr_mpoly_t res, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_one(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_set(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_set(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_set_si(gr_mpoly_t res, slong v, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_set_si(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_set_ui(gr_mpoly_t res, ulong v, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_set_ui(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_set_fmpz(gr_mpoly_t res, const fmpz_t v, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_set_fmpz(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_set_fmpq(gr_mpoly_t res, const fmpq_t v, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_set_fmpq(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_neg(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_neg(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_add(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + if (poly1->length + poly2->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); + + return gr_mpoly_add(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_sub(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + if (poly1->length + poly2->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); + + return gr_mpoly_sub(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_mul(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + if (poly1->length * poly2->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); + + return gr_mpoly_mul(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE void NEW_gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) +{ + gr_mpoly_assert_canonical(A, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + + +FLINT_FORCE_INLINE int NEW_gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x_in, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_print_pretty(A, x_in, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_mul_johnson(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_mul_johnson(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + +FLINT_FORCE_INLINE int +NEW_gr_mpoly_mul_monomial(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_mul_monomial(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); +} + #ifdef __cplusplus } #endif diff --git a/src/gr_mpoly/get_coeff_scalar_fmpz.c b/src/gr_mpoly/get_coeff_scalar_fmpz.c index f2f358a5f8..c7a8eeaf61 100644 --- a/src/gr_mpoly/get_coeff_scalar_fmpz.c +++ b/src/gr_mpoly/get_coeff_scalar_fmpz.c @@ -17,15 +17,16 @@ mpoly_monomial_index_pfmpz function */ int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, - const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) + const fmpz * exp, gr_mpoly_ctx_t ctx) { int status; slong index; + slong nvars = GR_MPOLY_NVARS(ctx); fmpz ** exp_ptr; slong i; TMP_INIT; - for (i = 0; i < mctx->nvars; i++) + for (i = 0; i < nvars; i++) { if (fmpz_sgn(exp + i) < 0) return GR_DOMAIN; @@ -33,19 +34,19 @@ int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, TMP_START; - exp_ptr = (fmpz **) TMP_ALLOC(sizeof(fmpz *) * mctx->nvars); - for (i = 0; i < mctx->nvars; i++) + exp_ptr = (fmpz **) TMP_ALLOC(sizeof(fmpz *) * nvars); + for (i = 0; i < nvars; i++) exp_ptr[i] = (fmpz *) (exp + i); - index = mpoly_monomial_index_pfmpz(A->exps, A->bits, A->length, exp_ptr, mctx); + index = mpoly_monomial_index_pfmpz(A->exps, A->bits, A->length, exp_ptr, GR_MPOLY_MCTX(ctx)); if (index < 0) { - status = gr_zero(c, cctx); + status = gr_zero(c, GR_MPOLY_CCTX(ctx)); } else { FLINT_ASSERT(index < A->length); - status = gr_set(c, GR_ENTRY(A->coeffs, index, cctx->sizeof_elem), cctx); + status = gr_set(c, GR_ENTRY(A->coeffs, index, GR_MPOLY_CCTX(ctx)->sizeof_elem), GR_MPOLY_CCTX(ctx)); } TMP_END; diff --git a/src/gr_mpoly/get_coeff_scalar_ui.c b/src/gr_mpoly/get_coeff_scalar_ui.c index 403ad987ed..6e7943b1c7 100644 --- a/src/gr_mpoly/get_coeff_scalar_ui.c +++ b/src/gr_mpoly/get_coeff_scalar_ui.c @@ -16,19 +16,19 @@ mpoly_monomial_index_pfmpz function */ int gr_mpoly_get_coeff_scalar_ui(gr_ptr c, const gr_mpoly_t A, - const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) + const ulong * exp, gr_mpoly_ctx_t ctx) { slong index; - index = mpoly_monomial_index_ui(A->exps, A->bits, A->length, exp, mctx); + index = mpoly_monomial_index_ui(A->exps, A->bits, A->length, exp, GR_MPOLY_MCTX(ctx)); if (index < 0) { - return gr_zero(c, cctx); + return gr_zero(c, GR_MPOLY_CCTX(ctx)); } else { FLINT_ASSERT(index < A->length); - return gr_set(c, GR_ENTRY(A->coeffs, index, cctx->sizeof_elem), cctx); + return gr_set(c, GR_ENTRY(A->coeffs, index, GR_MPOLY_CCTX(ctx)->sizeof_elem), GR_MPOLY_CCTX(ctx)); } } diff --git a/src/gr_mpoly/set_coeff_scalar_fmpz.c b/src/gr_mpoly/set_coeff_scalar_fmpz.c index 9ec9f25aff..37f022e797 100644 --- a/src/gr_mpoly/set_coeff_scalar_fmpz.c +++ b/src/gr_mpoly/set_coeff_scalar_fmpz.c @@ -18,8 +18,10 @@ int gr_mpoly_set_coeff_scalar_fmpz( gr_mpoly_t A, gr_srcptr c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); flint_bitcnt_t exp_bits; slong i, N, index; ulong * cmpmask; @@ -27,6 +29,7 @@ int gr_mpoly_set_coeff_scalar_fmpz( int exists; int status = GR_SUCCESS; slong sz = cctx->sizeof_elem; + TMP_INIT; for (i = 0; i < mctx->nvars; i++) @@ -95,14 +98,15 @@ int gr_mpoly_set_coeff_ui_fmpz( gr_mpoly_t A, ulong c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_ui(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -112,14 +116,15 @@ int gr_mpoly_set_coeff_si_fmpz( gr_mpoly_t A, slong c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_si(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -129,14 +134,15 @@ int gr_mpoly_set_coeff_fmpz_fmpz( gr_mpoly_t A, const fmpz_t c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpz(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -146,15 +152,16 @@ int gr_mpoly_set_coeff_fmpq_fmpz( gr_mpoly_t A, const fmpq_t c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpq(t, c, cctx); if (status == GR_SUCCESS) - status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_fmpz(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; diff --git a/src/gr_mpoly/set_coeff_scalar_ui.c b/src/gr_mpoly/set_coeff_scalar_ui.c index 6ce09ed6e8..2dc9de94c5 100644 --- a/src/gr_mpoly/set_coeff_scalar_ui.c +++ b/src/gr_mpoly/set_coeff_scalar_ui.c @@ -14,9 +14,9 @@ #include "gr_mpoly.h" int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, - gr_srcptr c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_srcptr c, const ulong * exp, gr_mpoly_ctx_t ctx) { - slong i, nvars = mctx->nvars; + slong i, nvars = GR_MPOLY_NVARS(ctx); fmpz * newexp; int status; TMP_INIT; @@ -26,7 +26,7 @@ int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, for (i = 0; i < nvars; i++) fmpz_init_set_ui(newexp + i, exp[i]); - status = gr_mpoly_set_coeff_scalar_fmpz(poly, c, newexp, mctx, cctx); + status = gr_mpoly_set_coeff_scalar_fmpz(poly, c, newexp, ctx); for (i = 0; i < nvars; i++) fmpz_clear(newexp + i); @@ -40,14 +40,15 @@ int gr_mpoly_set_coeff_ui_ui( gr_mpoly_t A, ulong c, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_ui(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -57,14 +58,15 @@ int gr_mpoly_set_coeff_si_ui( gr_mpoly_t A, slong c, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_si(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -74,14 +76,15 @@ int gr_mpoly_set_coeff_fmpz_ui( gr_mpoly_t A, const fmpz_t c, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpz(t, c, cctx); - status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; @@ -91,15 +94,16 @@ int gr_mpoly_set_coeff_fmpq_ui( gr_mpoly_t A, const fmpq_t c, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpq(t, c, cctx); if (status == GR_SUCCESS) - status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_ui(A, t, exp, ctx); GR_TMP_CLEAR(t, cctx); return status; diff --git a/src/gr_mpoly/test/t-add_sub.c b/src/gr_mpoly/test/t-add_sub.c index 36e289ac14..0d4599be73 100644 --- a/src/gr_mpoly/test/t-add_sub.c +++ b/src/gr_mpoly/test/t-add_sub.c @@ -17,23 +17,24 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { slong i, j; - /* Check (f + g) - g = f */ for (i = 0; i < 100; i++) { gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h, k; + gr_mpoly_ctx_t ctx; + gr_mpoly_t f, g, h, k, k1, k2; slong len, len1, len2; flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; int status; gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); + gr_mpoly_ctx_init_rand(ctx, state, cctx, 10); - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k, mctx, cctx); + NEW_gr_mpoly_init(f, ctx); + NEW_gr_mpoly_init(g, ctx); + NEW_gr_mpoly_init(h, ctx); + NEW_gr_mpoly_init(k, ctx); + NEW_gr_mpoly_init(k1, ctx); + NEW_gr_mpoly_init(k2, ctx); len = n_randint(state, 100); len1 = n_randint(state, 100); @@ -43,340 +44,172 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) exp_bits1 = n_randint(state, 200) + 2; exp_bits2 = n_randint(state, 200) + 2; + /* Check (f + g) - g = f */ for (j = 0; j < 10; j++) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= gr_mpoly_add(h, g, f, mctx, cctx); - status |= gr_mpoly_sub(k, h, g, mctx, cctx); + status |= NEW_gr_mpoly_add(h, g, f, ctx); + status |= NEW_gr_mpoly_sub(k, h, g, ctx); if (status == GR_SUCCESS) { - gr_mpoly_assert_canonical(h, mctx, cctx); - gr_mpoly_assert_canonical(k, mctx, cctx); + NEW_gr_mpoly_assert_canonical(h, ctx); + NEW_gr_mpoly_assert_canonical(k, ctx); } - if (status == GR_SUCCESS && gr_mpoly_equal(f, k, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, k, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f + g = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("(f + g) - g = "); gr_mpoly_print_pretty(k, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("f + g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("(f + g) - g = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k, mctx, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - - /* Check f + g = g + f */ - for (i = 0; i < 10; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h, k; - slong len, len1, len2; - flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; - int status; - - gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); - - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k, mctx, cctx); - - len = n_randint(state, 100); - len1 = n_randint(state, 100); - len2 = n_randint(state, 100); - - exp_bits = n_randint(state, 200) + 2; - exp_bits1 = n_randint(state, 200) + 2; - exp_bits2 = n_randint(state, 200) + 2; - + /* Check f + g = g + f */ for (j = 0; j < 10; j++) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= gr_mpoly_add(h, f, g, mctx, cctx); - status |= gr_mpoly_add(k, g, f, mctx, cctx); + status |= NEW_gr_mpoly_add(h, f, g, ctx); + status |= NEW_gr_mpoly_add(k, g, f, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(h, k, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(h, k, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) = (g + f)\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f + g = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g + f = "); gr_mpoly_print_pretty(k, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("f + g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("g + f = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k, mctx, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - - /* Check f - g = -g + f */ - for (i = 0; i < 10; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h, k; - slong len, len1, len2; - flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; - int status; - - gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); - - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k, mctx, cctx); - - len = n_randint(state, 100); - len1 = n_randint(state, 100); - len2 = n_randint(state, 100); - - exp_bits = n_randint(state, 200) + 2; - exp_bits1 = n_randint(state, 200) + 2; - exp_bits2 = n_randint(state, 200) + 2; - + /* Check f - g = -g + f */ for (j = 0; j < 10; j++) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= gr_mpoly_sub(h, f, g, mctx, cctx); - status |= gr_mpoly_neg(k, g, mctx, cctx); - status |= gr_mpoly_add(k, k, f, mctx, cctx); + status |= NEW_gr_mpoly_sub(h, f, g, ctx); + status |= NEW_gr_mpoly_neg(k, g, ctx); + status |= NEW_gr_mpoly_add(k, k, f, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(h, k, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(h, k, ctx) == T_FALSE) { flint_printf("FAIL: Check f - g = -g + f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f - g = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("-g + f = "); gr_mpoly_print_pretty(k, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("f - g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("-g + f = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k, mctx, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - - /* Check f + (g + h) = (f + g) + h */ - for (i = 0; i < 10; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h, k1, k2; - slong len, len1, len2; - flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; - int status; - - gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); - - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k1, mctx, cctx); - gr_mpoly_init(k2, mctx, cctx); - - len = n_randint(state, 100); - len1 = n_randint(state, 100); - len2 = n_randint(state, 100); - - exp_bits = n_randint(state, 200) + 2; - exp_bits1 = n_randint(state, 200) + 2; - exp_bits2 = n_randint(state, 200) + 2; - + /* Check f + (g + h) = (f + g) + h */ for (j = 0; j < 10; j++) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= gr_mpoly_add(k1, g, h, mctx, cctx); - status |= gr_mpoly_add(k1, f, k1, mctx, cctx); - status |= gr_mpoly_add(k2, f, g, mctx, cctx); - status |= gr_mpoly_add(k2, k2, h, mctx, cctx); + status |= NEW_gr_mpoly_add(k1, g, h, ctx); + status |= NEW_gr_mpoly_add(k1, f, k1, ctx); + status |= NEW_gr_mpoly_add(k2, f, g, ctx); + status |= NEW_gr_mpoly_add(k2, k2, h, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL Check f + (g + h) = (f + g) + h\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("h = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f + (g + h) = "); gr_mpoly_print_pretty(k1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("(f + g) + h = "); gr_mpoly_print_pretty(k2, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("f + (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); + flint_printf("(f + g) + h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k1, mctx, cctx); - gr_mpoly_clear(k2, mctx, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - - /* Check f - (g + h) = (f - g) - h */ - for (i = 0; i < 100; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h, k1, k2; - slong len, len1, len2; - flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; - int status; - - gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); - - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k1, mctx, cctx); - gr_mpoly_init(k2, mctx, cctx); - - len = n_randint(state, 100); - len1 = n_randint(state, 100); - len2 = n_randint(state, 100); - - exp_bits = n_randint(state, 200) + 2; - exp_bits1 = n_randint(state, 200) + 2; - exp_bits2 = n_randint(state, 200) + 2; - + /* Check f - (g + h) = (f - g) - h */ for (j = 0; j < 10; j++) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= gr_mpoly_add(k1, g, h, mctx, cctx); - status |= gr_mpoly_sub(k1, f, k1, mctx, cctx); - status |= gr_mpoly_sub(k2, f, g, mctx, cctx); - status |= gr_mpoly_sub(k2, k2, h, mctx, cctx); + status |= NEW_gr_mpoly_add(k1, g, h, ctx); + status |= NEW_gr_mpoly_sub(k1, f, k1, ctx); + status |= NEW_gr_mpoly_sub(k2, f, g, ctx); + status |= NEW_gr_mpoly_sub(k2, k2, h, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL Check f + (g + h) = (f + g) + h\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("h = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f + (g + h) = "); gr_mpoly_print_pretty(k1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("(f + g) + h = "); gr_mpoly_print_pretty(k2, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("f + (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); + flint_printf("(f + g) + h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k1, mctx, cctx); - gr_mpoly_clear(k2, mctx, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - -#if 0 - /* Check aliasing */ - for (i = 0; i < 100; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f, g, h; - slong len, len1, len2; - flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; - - gr_mpoly_ctx_init_rand_bits(ctx, state, 20, 200); + /* Check aliasing */ + for (j = 0; j < 1; j++) + { + status = GR_SUCCESS; - gr_mpoly_init(f, ctx); - gr_mpoly_init(g, ctx); - gr_mpoly_init(h, ctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_set(h, f, ctx); - len = n_randint(state, 100); - len1 = n_randint(state, 100); - len2 = n_randint(state, 100); + status |= NEW_gr_mpoly_add(f, f, g, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); + status |= NEW_gr_mpoly_sub(f, f, g, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); - exp_bits = n_randint(state, 200) + 2; - exp_bits1 = n_randint(state, 200) + 2; - exp_bits2 = n_randint(state, 200) + 2; - - for (j = 0; j < 10; j++) - { - gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - gr_mpoly_set(h, f, ctx); - - gr_mpoly_add(f, f, g, ctx); - gr_mpoly_assert_canonical(f, ctx); - gr_mpoly_sub(f, f, g, ctx); - gr_mpoly_assert_canonical(f, ctx); - if (!gr_mpoly_equal(f, h, ctx)) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, h, ctx) == T_FALSE) { flint_printf("FAIL: Check aliasing first arg\n"); flint_printf("i = %wd, j = %wd\n", i, j); @@ -384,26 +217,26 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) flint_abort(); } - gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); if ((j % 2) == 0) { - gr_mpoly_add(h, g, f, ctx); - gr_mpoly_assert_canonical(h, ctx); - gr_mpoly_add(f, g, f, ctx); - gr_mpoly_assert_canonical(f, ctx); + status |= NEW_gr_mpoly_add(h, g, f, ctx); + NEW_gr_mpoly_assert_canonical(h, ctx); + status |= NEW_gr_mpoly_add(f, g, f, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); } else { - gr_mpoly_sub(h, g, f, ctx); - gr_mpoly_assert_canonical(h, ctx); - gr_mpoly_sub(f, g, f, ctx); - gr_mpoly_assert_canonical(f, ctx); + status |= NEW_gr_mpoly_sub(h, g, f, ctx); + NEW_gr_mpoly_assert_canonical(h, ctx); + status |= NEW_gr_mpoly_sub(f, g, f, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); } - if (!gr_mpoly_equal(f, h, ctx)) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, h, ctx) == T_FALSE) { flint_printf("FAIL: Check aliasing second arg\n"); flint_printf("i = %wd, j = %wd\n", i, j); @@ -412,13 +245,16 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) } } - gr_mpoly_clear(f, ctx); - gr_mpoly_clear(g, ctx); - gr_mpoly_clear(h, ctx); + NEW_gr_mpoly_clear(f, ctx); + NEW_gr_mpoly_clear(g, ctx); + NEW_gr_mpoly_clear(h, ctx); + NEW_gr_mpoly_clear(k, ctx); + NEW_gr_mpoly_clear(k1, ctx); + NEW_gr_mpoly_clear(k2, ctx); gr_mpoly_ctx_clear(ctx); + gr_ctx_clear(cctx); } -#endif TEST_FUNCTION_END(state); } diff --git a/src/gr_mpoly/test/t-get_set_coeff.c b/src/gr_mpoly/test/t-get_set_coeff.c index 3ea5f83d9c..3bf84f3727 100644 --- a/src/gr_mpoly/test/t-get_set_coeff.c +++ b/src/gr_mpoly/test/t-get_set_coeff.c @@ -21,45 +21,47 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) for (i = 0; i < 100; i++) { gr_ctx_t cctx; - mpoly_ctx_t mctx; + gr_mpoly_ctx_t ctx; gr_mpoly_t f; gr_ptr c, d; slong len, k; flint_bitcnt_t exp_bits; int status; + slong nvars; gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); + gr_mpoly_ctx_init_rand(ctx, state, cctx, 20); + nvars = GR_MPOLY_NVARS(ctx); - gr_mpoly_init(f, mctx, cctx); + NEW_gr_mpoly_init(f, ctx); c = gr_heap_init(cctx); d = gr_heap_init(cctx); len = n_randint(state, 100); exp_bits = n_randint(state, 200) + 2; - GR_MUST_SUCCEED(gr_mpoly_randtest_bits(f, state, len, exp_bits, mctx, cctx)); + GR_MUST_SUCCEED(NEW_gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); for (j = 0; j < 10; j++) { - ulong * exp = FLINT_ARRAY_ALLOC(mctx->nvars, ulong); + ulong * exp = FLINT_ARRAY_ALLOC(nvars, ulong); status = GR_SUCCESS; GR_MUST_SUCCEED(gr_randtest(c, state, cctx)); - for (k = 0; k < mctx->nvars; k++) + for (k = 0; k < nvars; k++) exp[k] = n_randtest(state); - status |= gr_mpoly_set_coeff_scalar_ui(f, c, exp, mctx, cctx); - gr_mpoly_assert_canonical(f, mctx, cctx); - status |= gr_mpoly_get_coeff_scalar_ui(d, f, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_ui(f, c, exp, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_get_coeff_scalar_ui(d, f, exp, ctx); if (status == GR_SUCCESS && gr_equal(c, d, cctx) == T_FALSE) { flint_printf("FAIL: scalar_ui\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); flint_printf("c = "); gr_print(c, cctx); flint_printf("\n"); flint_printf("d = "); gr_print(d, cctx); flint_printf("\n"); fflush(stdout); @@ -69,69 +71,41 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) flint_free(exp); } - gr_mpoly_clear(f, mctx, cctx); - gr_heap_clear(c, cctx); - gr_heap_clear(d, cctx); - - mpoly_ctx_clear(mctx); - gr_ctx_clear(cctx); - } - - for (i = 0; i < 100; i++) - { - gr_ctx_t cctx; - mpoly_ctx_t mctx; - gr_mpoly_t f; - gr_ptr c, d; - slong len; - flint_bitcnt_t exp_bits; - int status; - - gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 20); - - gr_mpoly_init(f, mctx, cctx); - c = gr_heap_init(cctx); - d = gr_heap_init(cctx); - - len = n_randint(state, 100); - exp_bits = n_randint(state, 200) + 2; - - GR_MUST_SUCCEED(gr_mpoly_randtest_bits(f, state, len, exp_bits, mctx, cctx)); + GR_MUST_SUCCEED(NEW_gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); for (j = 0; j < 10; j++) { - fmpz * exp = _fmpz_vec_init(mctx->nvars); + fmpz * exp = _fmpz_vec_init(nvars); status = GR_SUCCESS; GR_MUST_SUCCEED(gr_randtest(c, state, cctx)); - _fmpz_vec_randtest(exp, state, mctx->nvars, exp_bits); + _fmpz_vec_randtest(exp, state, nvars, exp_bits); - status |= gr_mpoly_set_coeff_scalar_fmpz(f, c, exp, mctx, cctx); - gr_mpoly_assert_canonical(f, mctx, cctx); - status |= gr_mpoly_get_coeff_scalar_fmpz(d, f, exp, mctx, cctx); + status |= gr_mpoly_set_coeff_scalar_fmpz(f, c, exp, ctx); + NEW_gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_get_coeff_scalar_fmpz(d, f, exp, ctx); if (status == GR_SUCCESS && gr_equal(c, d, cctx) == T_FALSE) { flint_printf("FAIL: scalar_fmpz\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); flint_printf("c = "); gr_print(c, cctx); flint_printf("\n"); flint_printf("d = "); gr_print(d, cctx); flint_printf("\n"); fflush(stdout); flint_abort(); } - _fmpz_vec_clear(exp, mctx->nvars); + _fmpz_vec_clear(exp, nvars); } - gr_mpoly_clear(f, mctx, cctx); + NEW_gr_mpoly_clear(f, ctx); gr_heap_clear(c, cctx); gr_heap_clear(d, cctx); - mpoly_ctx_clear(mctx); + gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); } diff --git a/src/gr_mpoly/test/t-mul_johnson.c b/src/gr_mpoly/test/t-mul_johnson.c index b32efd4b07..01c50233cd 100644 --- a/src/gr_mpoly/test/t-mul_johnson.c +++ b/src/gr_mpoly/test/t-mul_johnson.c @@ -22,21 +22,21 @@ TEST_FUNCTION_START(gr_mpoly_mul_johnson, state) for (i = 0; i < 100; i++) { gr_ctx_t cctx; - mpoly_ctx_t mctx; + gr_mpoly_ctx_t ctx; gr_mpoly_t f, g, h, k1, k2, t; slong len, len1, len2; flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; int status; gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 4); + gr_mpoly_ctx_init_rand(ctx, state, cctx, 4); - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k1, mctx, cctx); - gr_mpoly_init(k2, mctx, cctx); - gr_mpoly_init(t, mctx, cctx); + NEW_gr_mpoly_init(f, ctx); + NEW_gr_mpoly_init(g, ctx); + NEW_gr_mpoly_init(h, ctx); + NEW_gr_mpoly_init(k1, ctx); + NEW_gr_mpoly_init(k2, ctx); + NEW_gr_mpoly_init(t, ctx); if (cctx->methods == _ca_methods) { @@ -61,57 +61,57 @@ TEST_FUNCTION_START(gr_mpoly_mul_johnson, state) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= gr_mpoly_add(k1, g, h, mctx, cctx); + status |= NEW_gr_mpoly_add(k1, g, h, ctx); if (n_randint(state, 2)) - status |= gr_mpoly_mul_johnson(k1, f, k1, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(k1, f, k1, ctx); else - status |= gr_mpoly_mul_johnson(k1, k1, f, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(k1, k1, f, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(k1, mctx, cctx); + NEW_gr_mpoly_assert_canonical(k1, ctx); - status |= gr_mpoly_mul_johnson(k2, f, g, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(k2, f, g, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(k2, mctx, cctx); + NEW_gr_mpoly_assert_canonical(k2, ctx); - status |= gr_mpoly_mul_johnson(t, f, h, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(t, f, h, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(t, mctx, cctx); + NEW_gr_mpoly_assert_canonical(t, ctx); - status |= gr_mpoly_add(k2, k2, t, mctx, cctx); + status |= NEW_gr_mpoly_add(k2, k2, t, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("h = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f * (g + h) = "); gr_mpoly_print_pretty(k1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f * g + f * h = "); gr_mpoly_print_pretty(k2, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("f * (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); + flint_printf("f * g + f * h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k1, mctx, cctx); - gr_mpoly_clear(k2, mctx, cctx); - gr_mpoly_clear(t, mctx, cctx); + NEW_gr_mpoly_clear(f, ctx); + NEW_gr_mpoly_clear(g, ctx); + NEW_gr_mpoly_clear(h, ctx); + NEW_gr_mpoly_clear(k1, ctx); + NEW_gr_mpoly_clear(k2, ctx); + NEW_gr_mpoly_clear(t, ctx); - mpoly_ctx_clear(mctx); + gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); } diff --git a/src/gr_mpoly/test/t-mul_monomial.c b/src/gr_mpoly/test/t-mul_monomial.c index d28ccbbaef..ee3a87602a 100644 --- a/src/gr_mpoly/test/t-mul_monomial.c +++ b/src/gr_mpoly/test/t-mul_monomial.c @@ -22,21 +22,21 @@ TEST_FUNCTION_START(gr_mpoly_mul_monomial, state) for (i = 0; i < 100 * flint_test_multiplier(); i++) { gr_ctx_t cctx; - mpoly_ctx_t mctx; + gr_mpoly_ctx_t ctx; gr_mpoly_t f, g, h, k1, k2, t; slong len, len1; flint_bitcnt_t exp_bits, exp_bits1, exp_bits2; int status; gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 4); + gr_mpoly_ctx_init_rand(ctx, state, cctx, 4); - gr_mpoly_init(f, mctx, cctx); - gr_mpoly_init(g, mctx, cctx); - gr_mpoly_init(h, mctx, cctx); - gr_mpoly_init(k1, mctx, cctx); - gr_mpoly_init(k2, mctx, cctx); - gr_mpoly_init(t, mctx, cctx); + NEW_gr_mpoly_init(f, ctx); + NEW_gr_mpoly_init(g, ctx); + NEW_gr_mpoly_init(h, ctx); + NEW_gr_mpoly_init(k1, ctx); + NEW_gr_mpoly_init(k2, ctx); + NEW_gr_mpoly_init(t, ctx); if (cctx->methods == _ca_methods) { @@ -59,60 +59,60 @@ TEST_FUNCTION_START(gr_mpoly_mul_monomial, state) { status = GR_SUCCESS; - status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, mctx, cctx); - status |= gr_mpoly_randtest_bits(g, state, 1, exp_bits2, mctx, cctx); - status |= gr_mpoly_randtest_bits(h, state, 1, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, mctx, cctx); - status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, mctx, cctx); + status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= NEW_gr_mpoly_randtest_bits(g, state, 1, exp_bits2, ctx); + status |= NEW_gr_mpoly_randtest_bits(h, state, 1, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); if (g->length != 1 || h->length != 1) continue; - status |= gr_mpoly_add(k1, g, h, mctx, cctx); + status |= NEW_gr_mpoly_add(k1, g, h, ctx); if (n_randint(state, 2)) - status |= gr_mpoly_mul_johnson(k1, f, k1, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(k1, f, k1, ctx); else - status |= gr_mpoly_mul_johnson(k1, k1, f, mctx, cctx); + status |= NEW_gr_mpoly_mul_johnson(k1, k1, f, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(k1, mctx, cctx); + NEW_gr_mpoly_assert_canonical(k1, ctx); - status |= gr_mpoly_mul_monomial(k2, f, g, mctx, cctx); + status |= NEW_gr_mpoly_mul_monomial(k2, f, g, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(k2, mctx, cctx); + NEW_gr_mpoly_assert_canonical(k2, ctx); - status |= gr_mpoly_mul_monomial(t, f, h, mctx, cctx); + status |= NEW_gr_mpoly_mul_monomial(t, f, h, ctx); if (status == GR_SUCCESS) - gr_mpoly_assert_canonical(t, mctx, cctx); + NEW_gr_mpoly_assert_canonical(t, ctx); - status |= gr_mpoly_add(k2, k2, t, mctx, cctx); + status |= NEW_gr_mpoly_add(k2, k2, t, ctx); - if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, mctx, cctx) == T_FALSE) + if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); - gr_ctx_println(cctx); - flint_printf("f = "); gr_mpoly_print_pretty(f, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("g = "); gr_mpoly_print_pretty(g, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("h = "); gr_mpoly_print_pretty(h, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f * (g + h) = "); gr_mpoly_print_pretty(k1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f * g + f * h = "); gr_mpoly_print_pretty(k2, NULL, mctx, cctx); flint_printf("\n"); + gr_ctx_println(ctx); + flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); + flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); + flint_printf("f * (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); + flint_printf("f * g + f * h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - gr_mpoly_clear(f, mctx, cctx); - gr_mpoly_clear(g, mctx, cctx); - gr_mpoly_clear(h, mctx, cctx); - gr_mpoly_clear(k1, mctx, cctx); - gr_mpoly_clear(k2, mctx, cctx); - gr_mpoly_clear(t, mctx, cctx); + NEW_gr_mpoly_clear(f, ctx); + NEW_gr_mpoly_clear(g, ctx); + NEW_gr_mpoly_clear(h, ctx); + NEW_gr_mpoly_clear(k1, ctx); + NEW_gr_mpoly_clear(k2, ctx); + NEW_gr_mpoly_clear(t, ctx); - mpoly_ctx_clear(mctx); + gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); } From d5d2f682a168cf3dd28edf75c917cbcbe68244a7 Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Sat, 11 Jan 2025 23:42:51 +0100 Subject: [PATCH 3/4] refactor gr_mpoly, continued --- src/gr/mpoly.c | 195 +++---------------- src/gr_mpoly.h | 279 +++++++-------------------- src/gr_mpoly/add.c | 23 +-- src/gr_mpoly/combine_like_terms.c | 4 +- src/gr_mpoly/equal.c | 10 +- src/gr_mpoly/fit_bits.c | 3 +- src/gr_mpoly/fit_length.c | 10 +- src/gr_mpoly/fit_length_fit_bits.c | 4 +- src/gr_mpoly/fit_length_reset_bits.c | 6 +- src/gr_mpoly/gen.c | 21 +- src/gr_mpoly/init.c | 10 +- src/gr_mpoly/is_canonical.c | 8 +- src/gr_mpoly/mul.c | 7 +- src/gr_mpoly/mul_johnson.c | 35 ++-- src/gr_mpoly/mul_monomial.c | 12 +- src/gr_mpoly/mul_scalar.c | 39 ++-- src/gr_mpoly/neg.c | 8 +- src/gr_mpoly/push_term.c | 20 +- src/gr_mpoly/randtest_bits.c | 14 +- src/gr_mpoly/randtest_bound.c | 14 +- src/gr_mpoly/set.c | 8 +- src/gr_mpoly/set_coeff_scalar_fmpz.c | 4 +- src/gr_mpoly/set_scalar.c | 58 +++--- src/gr_mpoly/sort_terms.c | 4 +- src/gr_mpoly/sub.c | 23 +-- src/gr_mpoly/test/t-add_sub.c | 198 +++++++++---------- src/gr_mpoly/test/t-gen.c | 66 +++---- src/gr_mpoly/test/t-get_set_coeff.c | 16 +- src/gr_mpoly/test/t-mul_johnson.c | 64 +++--- src/gr_mpoly/test/t-mul_monomial.c | 64 +++--- src/gr_mpoly/write.c | 12 +- 31 files changed, 506 insertions(+), 733 deletions(-) diff --git a/src/gr/mpoly.c b/src/gr/mpoly.c index 395453b65b..6571606ac0 100644 --- a/src/gr/mpoly.c +++ b/src/gr/mpoly.c @@ -114,68 +114,14 @@ gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx) return gr_ctx_is_threadsafe(GR_MPOLY_CCTX(ctx)); } -void -_gr_gr_mpoly_init(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_init(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -void -_gr_gr_mpoly_clear(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_clear(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -void -_gr_gr_mpoly_swap(gr_mpoly_t poly1, gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_swap(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -void -_gr_gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - *res = *poly; -} - int -_gr_gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) +gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { - return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -slong -_gr_gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) -{ - return x->length; + return gr_mpoly_write_pretty(out, poly, ctx); } int -_gr_gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_write_pretty(out, poly, (const char **) GR_MPOLY_VARS(ctx), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -truth_t -_gr_gr_mpoly_equal(const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_equal(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -truth_t -_gr_gr_mpoly_is_zero(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_is_zero(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -truth_t -_gr_gr_mpoly_is_one(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_is_one(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) +gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) { slong i, n; int status = GR_SUCCESS; @@ -184,13 +130,13 @@ _gr_gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) gr_vec_set_length(res, n, ctx); for (i = 0; i < n; i++) - status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); + status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, ctx); return status; } int -_gr_gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) +gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) { int status; gr_vec_t vec1; @@ -207,98 +153,17 @@ _gr_gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) /* Promote to polynomials */ for (i = 0; i < n; i++) - { status |= gr_mpoly_set_scalar(gr_vec_entry_ptr(vec, i, ctx), - gr_vec_entry_srcptr(vec1, i, GR_MPOLY_CCTX(ctx)), - GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - } + gr_vec_entry_srcptr(vec1, i, GR_MPOLY_CCTX(ctx)), ctx); for (i = 0; i < m; i++) - { - status |= gr_mpoly_gen(((gr_mpoly_struct *) vec->entries) + n + i, - i, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - } + status |= gr_mpoly_gen(((gr_mpoly_struct *) vec->entries) + n + i, i, ctx); gr_vec_clear(vec1, GR_MPOLY_CCTX(ctx)); return status; } -int -_gr_gr_mpoly_zero(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_one(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_one(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_set(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_set_si(gr_mpoly_t res, slong v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_si(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_set_ui(gr_mpoly_t res, ulong v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_ui(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_set_fmpz(gr_mpoly_t res, const fmpz_t v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_fmpz(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_set_fmpq(gr_mpoly_t res, const fmpq_t v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_fmpq(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_neg(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_neg(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_add(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_add(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_sub(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_sub(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -int -_gr_gr_mpoly_mul(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length * poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_mul(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - int _gr_mpoly_methods_initialized = 0; @@ -315,30 +180,30 @@ gr_method_tab_input _gr_mpoly_methods_input[] = {GR_METHOD_CTX_IS_FIELD, (gr_funcptr) gr_mpoly_ctx_is_field}, {GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) gr_mpoly_ctx_is_threadsafe}, {GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) gr_mpoly_ctx_set_gen_names}, - {GR_METHOD_INIT, (gr_funcptr) _gr_gr_mpoly_init}, - {GR_METHOD_CLEAR, (gr_funcptr) _gr_gr_mpoly_clear}, - {GR_METHOD_SWAP, (gr_funcptr) _gr_gr_mpoly_swap}, - {GR_METHOD_SET_SHALLOW, (gr_funcptr) _gr_gr_mpoly_set_shallow}, - {GR_METHOD_RANDTEST, (gr_funcptr) _gr_gr_mpoly_randtest}, - {_GR_METHOD_LENGTH, (gr_funcptr) _gr_gr_mpoly_length}, - {GR_METHOD_WRITE, (gr_funcptr) _gr_gr_mpoly_write}, - {GR_METHOD_GENS, (gr_funcptr) _gr_gr_mpoly_gens}, - {GR_METHOD_GENS_RECURSIVE, (gr_funcptr) _gr_gr_mpoly_gens_recursive}, - {GR_METHOD_ZERO, (gr_funcptr) _gr_gr_mpoly_zero}, - {GR_METHOD_ONE, (gr_funcptr) _gr_gr_mpoly_one}, - {GR_METHOD_IS_ZERO, (gr_funcptr) _gr_gr_mpoly_is_zero}, - {GR_METHOD_IS_ONE, (gr_funcptr) _gr_gr_mpoly_is_one}, - {GR_METHOD_EQUAL, (gr_funcptr) _gr_gr_mpoly_equal}, - {GR_METHOD_SET, (gr_funcptr) _gr_gr_mpoly_set}, - {GR_METHOD_SET_UI, (gr_funcptr) _gr_gr_mpoly_set_ui}, - {GR_METHOD_SET_SI, (gr_funcptr) _gr_gr_mpoly_set_si}, - {GR_METHOD_SET_FMPZ, (gr_funcptr) _gr_gr_mpoly_set_fmpz}, - {GR_METHOD_SET_FMPQ, (gr_funcptr) _gr_gr_mpoly_set_fmpq}, + {GR_METHOD_INIT, (gr_funcptr) gr_mpoly_init}, + {GR_METHOD_CLEAR, (gr_funcptr) gr_mpoly_clear}, + {GR_METHOD_SWAP, (gr_funcptr) gr_mpoly_swap}, + {GR_METHOD_SET_SHALLOW, (gr_funcptr) gr_mpoly_set_shallow}, + {GR_METHOD_RANDTEST, (gr_funcptr) gr_mpoly_randtest}, + {_GR_METHOD_LENGTH, (gr_funcptr) gr_mpoly_length}, + {GR_METHOD_WRITE, (gr_funcptr) gr_mpoly_write}, + {GR_METHOD_GENS, (gr_funcptr) gr_mpoly_gens}, + {GR_METHOD_GENS_RECURSIVE, (gr_funcptr) gr_mpoly_gens_recursive}, + {GR_METHOD_ZERO, (gr_funcptr) gr_mpoly_zero}, + {GR_METHOD_ONE, (gr_funcptr) gr_mpoly_one}, + {GR_METHOD_IS_ZERO, (gr_funcptr) gr_mpoly_is_zero}, + {GR_METHOD_IS_ONE, (gr_funcptr) gr_mpoly_is_one}, + {GR_METHOD_EQUAL, (gr_funcptr) gr_mpoly_equal}, + {GR_METHOD_SET, (gr_funcptr) gr_mpoly_set}, + {GR_METHOD_SET_UI, (gr_funcptr) gr_mpoly_set_ui}, + {GR_METHOD_SET_SI, (gr_funcptr) gr_mpoly_set_si}, + {GR_METHOD_SET_FMPZ, (gr_funcptr) gr_mpoly_set_fmpz}, + {GR_METHOD_SET_FMPQ, (gr_funcptr) gr_mpoly_set_fmpq}, {GR_METHOD_SET_STR, (gr_funcptr) gr_generic_set_str_balance_additions}, - {GR_METHOD_NEG, (gr_funcptr) _gr_gr_mpoly_neg}, - {GR_METHOD_ADD, (gr_funcptr) _gr_gr_mpoly_add}, - {GR_METHOD_SUB, (gr_funcptr) _gr_gr_mpoly_sub}, - {GR_METHOD_MUL, (gr_funcptr) _gr_gr_mpoly_mul}, + {GR_METHOD_NEG, (gr_funcptr) gr_mpoly_neg}, + {GR_METHOD_ADD, (gr_funcptr) gr_mpoly_add}, + {GR_METHOD_SUB, (gr_funcptr) gr_mpoly_sub}, + {GR_METHOD_MUL, (gr_funcptr) gr_mpoly_mul}, {0, (gr_funcptr) NULL}, }; diff --git a/src/gr_mpoly.h b/src/gr_mpoly.h index 7c187290e3..8cfaa1c34a 100644 --- a/src/gr_mpoly.h +++ b/src/gr_mpoly.h @@ -67,7 +67,7 @@ void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx); /* Memory management */ GR_MPOLY_INLINE -void gr_mpoly_init(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +void gr_mpoly_init(gr_mpoly_t A, gr_mpoly_ctx_t ctx) { A->coeffs = NULL; A->exps = NULL; @@ -77,20 +77,16 @@ void gr_mpoly_init(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) A->exps_alloc = 0; } -void gr_mpoly_init3(gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx); - -void gr_mpoly_init2( gr_mpoly_t A, slong alloc, - const mpoly_ctx_t mctx, gr_ctx_t cctx); +void gr_mpoly_init3(gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx); +void gr_mpoly_init2( gr_mpoly_t A, slong alloc, gr_mpoly_ctx_t ctx); GR_MPOLY_INLINE -void gr_mpoly_clear(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +void gr_mpoly_clear(gr_mpoly_t A, gr_mpoly_ctx_t ctx) { - _gr_vec_clear(A->coeffs, A->coeffs_alloc, cctx); + _gr_vec_clear(A->coeffs, A->coeffs_alloc, GR_MPOLY_CCTX(ctx)); if (A->coeffs_alloc > 0) flint_free(A->coeffs); - if (A->exps_alloc > 0) flint_free(A->exps); } @@ -104,29 +100,27 @@ void _gr_mpoly_fit_length( slong length, gr_ctx_t cctx); -void gr_mpoly_fit_length(gr_mpoly_t A, slong len, const mpoly_ctx_t mctx, gr_ctx_t cctx); - -void gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx); +void gr_mpoly_fit_length(gr_mpoly_t A, slong len, gr_mpoly_ctx_t ctx); +void gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx); void gr_mpoly_fit_length_fit_bits( gr_mpoly_t A, slong len, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx); + gr_mpoly_ctx_t ctx); void gr_mpoly_fit_length_reset_bits( gr_mpoly_t A, slong len, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx); + gr_mpoly_ctx_t ctx); /* todo: when to zero out coefficients? */ GR_MPOLY_INLINE -void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, - const mpoly_ctx_t mctx, gr_ctx_t cctx) +void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, gr_mpoly_ctx_t ctx) { FLINT_ASSERT(newlen <= A->coeffs_alloc); - FLINT_ASSERT(mpoly_words_per_exp(A->bits, mctx)*newlen <= A->exps_alloc); + FLINT_ASSERT(mpoly_words_per_exp(A->bits, GR_MPOLY_MCTX(ctx))*newlen <= A->exps_alloc); A->length = newlen; } @@ -134,80 +128,83 @@ void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, /* Basic manipulation */ GR_MPOLY_INLINE -void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx) +void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t ctx) { FLINT_SWAP(gr_mpoly_struct, *A, *B); } -WARN_UNUSED_RESULT int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx); GR_MPOLY_INLINE WARN_UNUSED_RESULT -int gr_mpoly_zero(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_zero(gr_mpoly_t A, gr_mpoly_ctx_t ctx) { - _gr_mpoly_set_length(A, 0, mctx, cctx); + _gr_mpoly_set_length(A, 0, ctx); return GR_SUCCESS; } GR_MPOLY_INLINE -truth_t gr_mpoly_is_zero(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +truth_t gr_mpoly_is_zero(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { if (A->length == 0) return T_TRUE; /* todo: skip when we have canonical representation */ - return _gr_vec_is_zero(A->coeffs, A->length, cctx); + return _gr_vec_is_zero(A->coeffs, A->length, GR_MPOLY_CCTX(ctx)); } -WARN_UNUSED_RESULT int gr_mpoly_gen(gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, gr_ctx_t cctx); -truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_gen(gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx); -truth_t gr_mpoly_equal(const gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx); +truth_t gr_mpoly_equal(const gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx); /* Container operations */ -void _gr_mpoly_push_exp_ui(gr_mpoly_t A, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_push_term_scalar_ui(gr_mpoly_t A, gr_srcptr c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -void _gr_mpoly_push_exp_fmpz(gr_mpoly_t A, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_push_term_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx); -void gr_mpoly_sort_terms(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_combine_like_terms(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx); -truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx); -void gr_mpoly_assert_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx); +void _gr_mpoly_push_exp_ui(gr_mpoly_t A, const ulong * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_push_term_scalar_ui(gr_mpoly_t A, gr_srcptr c, const ulong * exp, gr_mpoly_ctx_t ctx); + +void _gr_mpoly_push_exp_fmpz(gr_mpoly_t A, const fmpz * exp, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_push_term_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, gr_mpoly_ctx_t ctx); + +void gr_mpoly_sort_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_combine_like_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx); + +truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx); +void gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx); /* Random generation */ -int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, const mpoly_ctx_t mctx, gr_ctx_t cctx); +int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx); /* Input and output */ /* todo: vars stored in context object */ -int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx); -int gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx); +int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, gr_mpoly_ctx_t ctx); +int gr_mpoly_print_pretty(const gr_mpoly_t A, gr_mpoly_ctx_t ctx); /* Constants */ -WARN_UNUSED_RESULT int gr_mpoly_set_scalar(gr_mpoly_t A, gr_srcptr c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_ui(gr_mpoly_t A, ulong c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_si(gr_mpoly_t A, slong c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_fmpz(gr_mpoly_t A, const fmpz_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_set_fmpq(gr_mpoly_t A, const fmpq_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_set_scalar(gr_mpoly_t A, gr_srcptr c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_ui(gr_mpoly_t A, ulong c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_si(gr_mpoly_t A, slong c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_fmpz(gr_mpoly_t A, const fmpz_t c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_set_fmpq(gr_mpoly_t A, const fmpq_t c, gr_mpoly_ctx_t ctx); GR_MPOLY_INLINE WARN_UNUSED_RESULT -int gr_mpoly_one(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_one(gr_mpoly_t A, gr_mpoly_ctx_t ctx) { - return gr_mpoly_set_ui(A, 1, mctx, cctx); + return gr_mpoly_set_ui(A, 1, ctx); } /* todo: efficient version */ GR_MPOLY_INLINE -truth_t gr_mpoly_is_one(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +truth_t gr_mpoly_is_one(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { gr_mpoly_t t; truth_t res = T_UNKNOWN; - gr_mpoly_init(t, mctx, cctx); - if (gr_mpoly_one(t, mctx, cctx) == GR_SUCCESS) - res = gr_mpoly_equal(A, t, mctx, cctx); - gr_mpoly_clear(t, mctx, cctx); + gr_mpoly_init(t, ctx); + if (gr_mpoly_one(t, ctx) == GR_SUCCESS) + res = gr_mpoly_equal(A, t, ctx); + gr_mpoly_clear(t, ctx); return res; } @@ -230,186 +227,40 @@ WARN_UNUSED_RESULT int gr_mpoly_set_coeff_fmpq_ui(gr_mpoly_t A, const fmpq_t c, /* Arithmetic */ -WARN_UNUSED_RESULT int gr_mpoly_neg(gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_add(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_sub(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx); - -WARN_UNUSED_RESULT int gr_mpoly_mul(gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_johnson(gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_monomial(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx); - -WARN_UNUSED_RESULT int gr_mpoly_mul_scalar(gr_mpoly_t A, const gr_mpoly_t B, gr_srcptr c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_si(gr_mpoly_t A, const gr_mpoly_t B, slong c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); -WARN_UNUSED_RESULT int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx); +WARN_UNUSED_RESULT int gr_mpoly_neg(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_add(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_sub(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx); -/* Refactoring */ +WARN_UNUSED_RESULT int gr_mpoly_mul(gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_johnson(gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_monomial(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx); -FLINT_FORCE_INLINE void -NEW_gr_mpoly_init(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_init(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} +WARN_UNUSED_RESULT int gr_mpoly_mul_scalar(gr_mpoly_t A, const gr_mpoly_t B, gr_srcptr c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_si(gr_mpoly_t A, const gr_mpoly_t B, slong c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, gr_mpoly_ctx_t ctx); -FLINT_FORCE_INLINE void -NEW_gr_mpoly_clear(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_clear(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} +/* Todo */ -FLINT_FORCE_INLINE void -NEW_gr_mpoly_swap(gr_mpoly_t poly1, gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) +GR_MPOLY_INLINE WARN_UNUSED_RESULT int +gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) { - gr_mpoly_swap(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); + return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), ctx); } -FLINT_FORCE_INLINE void -NEW_gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +GR_MPOLY_INLINE void +gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) { *res = *poly; } -FLINT_FORCE_INLINE int -NEW_gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_randtest_bits(gr_mpoly_t res, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_randtest_bits(res, state, length, exp_bits, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE slong -NEW_gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) +GR_MPOLY_INLINE slong +gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) { return x->length; } -FLINT_FORCE_INLINE int -NEW_gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_write_pretty(out, poly, (const char **) GR_MPOLY_VARS(ctx), GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE truth_t -NEW_gr_mpoly_equal(const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_equal(poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE truth_t -NEW_gr_mpoly_is_zero(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_is_zero(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE truth_t -NEW_gr_mpoly_is_one(const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_is_one(poly, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_zero(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_one(gr_mpoly_t res, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_one(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_set(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_set_si(gr_mpoly_t res, slong v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_si(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_set_ui(gr_mpoly_t res, ulong v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_ui(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_set_fmpz(gr_mpoly_t res, const fmpz_t v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_fmpz(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_set_fmpq(gr_mpoly_t res, const fmpq_t v, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_set_fmpq(res, v, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_neg(gr_mpoly_t res, const gr_mpoly_t mat, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_neg(res, mat, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_add(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_add(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_sub(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length + poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_sub(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_mul(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - if (poly1->length * poly2->length > ctx->size_limit) - return GR_UNABLE | gr_mpoly_zero(res, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); - - return gr_mpoly_mul(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE void NEW_gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) -{ - gr_mpoly_assert_canonical(A, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - - -FLINT_FORCE_INLINE int NEW_gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x_in, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_print_pretty(A, x_in, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_mul_johnson(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_mul_johnson(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - -FLINT_FORCE_INLINE int -NEW_gr_mpoly_mul_monomial(gr_mpoly_t res, const gr_mpoly_t poly1, const gr_mpoly_t poly2, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_mul_monomial(res, poly1, poly2, GR_MPOLY_MCTX(ctx), GR_MPOLY_CCTX(ctx)); -} - #ifdef __cplusplus } #endif diff --git a/src/gr_mpoly/add.c b/src/gr_mpoly/add.c index db77ec982b..898630e114 100644 --- a/src/gr_mpoly/add.c +++ b/src/gr_mpoly/add.c @@ -77,12 +77,10 @@ slong _gr_mpoly_add( return status; } -int gr_mpoly_add( - gr_mpoly_t A, - const gr_mpoly_t B, - const gr_mpoly_t C, - const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_add(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong Abits, N; ulong * Bexps = B->exps, * Cexps = C->exps; ulong * cmpmask; @@ -91,10 +89,13 @@ int gr_mpoly_add( TMP_INIT; if (B->length == 0) - return gr_mpoly_set(A, C, mctx, cctx); + return gr_mpoly_set(A, C, ctx); if (C->length == 0) - return gr_mpoly_set(A, B, mctx, cctx); + return gr_mpoly_set(A, B, ctx); + + if (B->length + C->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(A, ctx); TMP_START; Abits = FLINT_MAX(B->bits, C->bits); @@ -121,17 +122,17 @@ int gr_mpoly_add( if (A == B || A == C) { gr_mpoly_t T; - gr_mpoly_init3(T, B->length + C->length, Abits, mctx, cctx); + gr_mpoly_init3(T, B->length + C->length, Abits, ctx); status = _gr_mpoly_add(&T->length, T->coeffs, T->exps, B->coeffs, Bexps, B->length, C->coeffs, Cexps, C->length, N, cmpmask, cctx); - gr_mpoly_swap(A, T, mctx, cctx); - gr_mpoly_clear(T, mctx, cctx); + gr_mpoly_swap(A, T, ctx); + gr_mpoly_clear(T, ctx); } else { - gr_mpoly_fit_length_reset_bits(A, B->length + C->length, Abits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, B->length + C->length, Abits, ctx); status = _gr_mpoly_add(&A->length, A->coeffs, A->exps, B->coeffs, Bexps, B->length, C->coeffs, Cexps, C->length, diff --git a/src/gr_mpoly/combine_like_terms.c b/src/gr_mpoly/combine_like_terms.c index cd3b03c1d2..820dbf50fc 100644 --- a/src/gr_mpoly/combine_like_terms.c +++ b/src/gr_mpoly/combine_like_terms.c @@ -21,8 +21,10 @@ */ int gr_mpoly_combine_like_terms( gr_mpoly_t A, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); gr_method_binary_op add = GR_BINARY_OP(cctx, ADD); gr_method_swap_op swap = GR_SWAP_OP(cctx, SWAP); gr_method_unary_predicate is_zero = GR_UNARY_PREDICATE(cctx, IS_ZERO); diff --git a/src/gr_mpoly/equal.c b/src/gr_mpoly/equal.c index f9252e25ca..7d2d9fed6b 100644 --- a/src/gr_mpoly/equal.c +++ b/src/gr_mpoly/equal.c @@ -15,7 +15,7 @@ truth_t gr_mpoly_equal( const gr_mpoly_t A, const gr_mpoly_t B, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { truth_t eq; gr_mpoly_t t; @@ -34,14 +34,14 @@ truth_t gr_mpoly_equal( A->length, ctx->minfo)) ? T_TRUE : T_FALSE; */ - gr_mpoly_init(t, mctx, cctx); + gr_mpoly_init(t, ctx); - if (gr_mpoly_sub(t, A, B, mctx, cctx) == GR_SUCCESS) - eq = gr_mpoly_is_zero(t, mctx, cctx); + if (gr_mpoly_sub(t, A, B, ctx) == GR_SUCCESS) + eq = gr_mpoly_is_zero(t, ctx); else eq = T_UNKNOWN; - gr_mpoly_clear(t, mctx, cctx); + gr_mpoly_clear(t, ctx); return eq; } diff --git a/src/gr_mpoly/fit_bits.c b/src/gr_mpoly/fit_bits.c index f4ef12fda4..a988e3a27f 100644 --- a/src/gr_mpoly/fit_bits.c +++ b/src/gr_mpoly/fit_bits.c @@ -13,12 +13,13 @@ #include "gr_mpoly.h" void -gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) +gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx) { if (A->bits < bits) { if (A->exps_alloc != 0) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); slong N = mpoly_words_per_exp(bits, mctx); ulong * t = (ulong *) flint_malloc(N*A->exps_alloc*sizeof(ulong)); mpoly_repack_monomials(t, bits, A->exps, A->bits, A->length, mctx); diff --git a/src/gr_mpoly/fit_length.c b/src/gr_mpoly/fit_length.c index e32786cc5a..f8fac44d2e 100644 --- a/src/gr_mpoly/fit_length.c +++ b/src/gr_mpoly/fit_length.c @@ -19,10 +19,12 @@ void _gr_mpoly_fit_length( slong * exps_alloc, slong N, slong length, - gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { if (length > *coeffs_alloc) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); + slong old_alloc = *coeffs_alloc; slong new_alloc = FLINT_MAX(length, old_alloc*2); slong sz = cctx->sizeof_elem; @@ -42,10 +44,10 @@ void _gr_mpoly_fit_length( void gr_mpoly_fit_length( gr_mpoly_t A, slong len, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - slong N = mpoly_words_per_exp(A->bits, mctx); + slong N = mpoly_words_per_exp(A->bits, GR_MPOLY_MCTX(ctx)); _gr_mpoly_fit_length(&A->coeffs, &A->coeffs_alloc, - &A->exps, &A->exps_alloc, N, len, cctx); + &A->exps, &A->exps_alloc, N, len, ctx); } diff --git a/src/gr_mpoly/fit_length_fit_bits.c b/src/gr_mpoly/fit_length_fit_bits.c index e563f4cc53..cdef11d493 100644 --- a/src/gr_mpoly/fit_length_fit_bits.c +++ b/src/gr_mpoly/fit_length_fit_bits.c @@ -16,8 +16,10 @@ void gr_mpoly_fit_length_fit_bits( gr_mpoly_t A, slong len, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N = mpoly_words_per_exp(A->bits, mctx); if (len > A->coeffs_alloc) diff --git a/src/gr_mpoly/fit_length_reset_bits.c b/src/gr_mpoly/fit_length_reset_bits.c index 2dab7fc3a4..0fb5e32eab 100644 --- a/src/gr_mpoly/fit_length_reset_bits.c +++ b/src/gr_mpoly/fit_length_reset_bits.c @@ -16,10 +16,10 @@ void gr_mpoly_fit_length_reset_bits( gr_mpoly_t A, slong len, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - slong N = mpoly_words_per_exp(bits, mctx); + slong N = mpoly_words_per_exp(bits, GR_MPOLY_MCTX(ctx)); _gr_mpoly_fit_length(&A->coeffs, &A->coeffs_alloc, - &A->exps, &A->exps_alloc, N, len, cctx); + &A->exps, &A->exps_alloc, N, len, ctx); A->bits = bits; } diff --git a/src/gr_mpoly/gen.c b/src/gr_mpoly/gen.c index 3f0cc20f72..e8229b5cf4 100644 --- a/src/gr_mpoly/gen.c +++ b/src/gr_mpoly/gen.c @@ -15,8 +15,10 @@ int gr_mpoly_gen( gr_mpoly_t A, slong var, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); flint_bitcnt_t bits; int status = GR_SUCCESS; @@ -25,7 +27,7 @@ int gr_mpoly_gen( bits = mpoly_gen_bits_required(var, mctx); bits = mpoly_fix_bits(bits, mctx); - gr_mpoly_fit_length_reset_bits(A, 1, bits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, 1, bits, ctx); if (bits <= FLINT_BITS) mpoly_gen_monomial_sp(A->exps, var, bits, mctx); @@ -33,13 +35,15 @@ int gr_mpoly_gen( mpoly_gen_monomial_offset_mp(A->exps, var, bits, mctx); status |= gr_one(A->coeffs, cctx); - _gr_mpoly_set_length(A, gr_is_zero(A->coeffs, cctx) != T_TRUE, mctx, cctx); + _gr_mpoly_set_length(A, gr_is_zero(A->coeffs, cctx) != T_TRUE, ctx); return status; } -truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, gr_ctx_t cctx) +truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); truth_t res; if (var >= mctx->nvars || mctx->nvars == 0) @@ -62,15 +66,14 @@ truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, g { /* todo: cheaper check when possible */ gr_mpoly_t t; + gr_mpoly_init(t, ctx); - gr_mpoly_init(t, mctx, cctx); - - if (gr_mpoly_gen(t, var, mctx, cctx) != GR_SUCCESS) + if (gr_mpoly_gen(t, var, ctx) != GR_SUCCESS) res = T_UNKNOWN; else - res = gr_mpoly_equal(A, t, mctx, cctx); + res = gr_mpoly_equal(A, t, ctx); - gr_mpoly_clear(t, mctx, cctx); + gr_mpoly_clear(t, ctx); } return res; diff --git a/src/gr_mpoly/init.c b/src/gr_mpoly/init.c index 141609baa2..e3564d9078 100644 --- a/src/gr_mpoly/init.c +++ b/src/gr_mpoly/init.c @@ -16,8 +16,10 @@ void gr_mpoly_init3( gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N = mpoly_words_per_exp(bits, mctx); if (alloc > 0) @@ -43,8 +45,8 @@ void gr_mpoly_init3( void gr_mpoly_init2( gr_mpoly_t A, slong alloc, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - flint_bitcnt_t bits = mpoly_fix_bits(MPOLY_MIN_BITS, mctx); - gr_mpoly_init3(A, alloc, bits, mctx, cctx); + flint_bitcnt_t bits = mpoly_fix_bits(MPOLY_MIN_BITS, GR_MPOLY_MCTX(ctx)); + gr_mpoly_init3(A, alloc, bits, ctx); } diff --git a/src/gr_mpoly/is_canonical.c b/src/gr_mpoly/is_canonical.c index 8f4791c22e..0acce2403c 100644 --- a/src/gr_mpoly/is_canonical.c +++ b/src/gr_mpoly/is_canonical.c @@ -12,8 +12,10 @@ #include "mpoly.h" #include "gr_mpoly.h" -truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N = mpoly_words_per_exp(A->bits, mctx); slong i; truth_t ok; @@ -41,8 +43,10 @@ truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx return ok; } -void gr_mpoly_assert_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +void gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N = mpoly_words_per_exp(A->bits, mctx); slong i; diff --git a/src/gr_mpoly/mul.c b/src/gr_mpoly/mul.c index 565f825ca1..31c4ffda81 100644 --- a/src/gr_mpoly/mul.c +++ b/src/gr_mpoly/mul.c @@ -15,7 +15,10 @@ int gr_mpoly_mul(gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - return gr_mpoly_mul_johnson(poly1, poly2, poly3, mctx, cctx); + if (poly2->length * poly3->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(poly1, ctx); + + return gr_mpoly_mul_johnson(poly1, poly2, poly3, ctx); } diff --git a/src/gr_mpoly/mul_johnson.c b/src/gr_mpoly/mul_johnson.c index cb0c69b70f..439d5c5cbb 100644 --- a/src/gr_mpoly/mul_johnson.c +++ b/src/gr_mpoly/mul_johnson.c @@ -23,8 +23,9 @@ int _gr_mpoly_mul_johnson( flint_bitcnt_t bits, slong N, const ulong * cmpmask, - gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); gr_method_binary_op mul = GR_BINARY_OP(cctx, MUL); gr_method_binary_op add = GR_BINARY_OP(cctx, ADD); slong i, j; @@ -86,7 +87,7 @@ int _gr_mpoly_mul_johnson( { exp = heap[1].exp; - _gr_mpoly_fit_length(&p1, alloc, &e1, exps_alloc, N, len1 + 1, cctx); + _gr_mpoly_fit_length(&p1, alloc, &e1, exps_alloc, N, len1 + 1, ctx); mpoly_monomial_set(e1 + len1*N, exp, N); @@ -190,8 +191,10 @@ int gr_mpoly_mul_johnson( gr_mpoly_t poly1, const gr_mpoly_t poly2, const gr_mpoly_t poly3, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, N, len1 = 0; flint_bitcnt_t exp_bits; fmpz * max_fields2, * max_fields3; @@ -203,18 +206,18 @@ int gr_mpoly_mul_johnson( if (poly2->length == 0 || poly3->length == 0) { - return gr_mpoly_zero(poly1, mctx, cctx); + return gr_mpoly_zero(poly1, ctx); } if (poly3->length == 1) { - return gr_mpoly_mul_monomial(poly1, poly2, poly3, mctx, cctx); + return gr_mpoly_mul_monomial(poly1, poly2, poly3, ctx); } /* todo: could have a version of mul_monomial for the noncommutative case */ if (poly2->length == 1 && gr_ctx_is_commutative_ring(cctx) == T_TRUE) { - return gr_mpoly_mul_monomial(poly1, poly3, poly2, mctx, cctx); + return gr_mpoly_mul_monomial(poly1, poly3, poly2, ctx); } TMP_START; @@ -270,9 +273,9 @@ int gr_mpoly_mul_johnson( { gr_mpoly_t temp; - gr_mpoly_init(temp, mctx, cctx); + gr_mpoly_init(temp, ctx); gr_mpoly_fit_length_reset_bits(temp, - poly2->length + poly3->length, exp_bits, mctx, cctx); + poly2->length + poly3->length, exp_bits, ctx); if (poly2->length >= poly3->length) { @@ -280,7 +283,7 @@ int gr_mpoly_mul_johnson( &temp->coeffs, &temp->exps, &temp->coeffs_alloc, &temp->exps_alloc, poly3->coeffs, exp3, poly3->length, poly2->coeffs, exp2, poly2->length, - exp_bits, N, cmpmask, cctx); + exp_bits, N, cmpmask, ctx); } else { @@ -288,15 +291,15 @@ int gr_mpoly_mul_johnson( &temp->coeffs, &temp->exps, &temp->coeffs_alloc, &temp->exps_alloc, poly2->coeffs, exp2, poly2->length, poly3->coeffs, exp3, poly3->length, - exp_bits, N, cmpmask, cctx); + exp_bits, N, cmpmask, ctx); } - gr_mpoly_swap(temp, poly1, mctx, cctx); - gr_mpoly_clear(temp, mctx, cctx); + gr_mpoly_swap(temp, poly1, ctx); + gr_mpoly_clear(temp, ctx); } else { - gr_mpoly_fit_length_reset_bits(poly1, poly2->length + poly3->length, exp_bits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(poly1, poly2->length + poly3->length, exp_bits, ctx); if (poly2->length > poly3->length) { @@ -304,7 +307,7 @@ int gr_mpoly_mul_johnson( &poly1->coeffs, &poly1->exps, &poly1->coeffs_alloc, &poly1->exps_alloc, poly3->coeffs, exp3, poly3->length, poly2->coeffs, exp2, poly2->length, - exp_bits, N, cmpmask, cctx); + exp_bits, N, cmpmask, ctx); } else { @@ -312,7 +315,7 @@ int gr_mpoly_mul_johnson( &poly1->coeffs, &poly1->exps, &poly1->coeffs_alloc, &poly1->exps_alloc, poly2->coeffs, exp2, poly2->length, poly3->coeffs, exp3, poly3->length, - exp_bits, N, cmpmask, cctx); + exp_bits, N, cmpmask, ctx); } } @@ -322,7 +325,7 @@ int gr_mpoly_mul_johnson( if (free3) flint_free(exp3); - _gr_mpoly_set_length(poly1, len1, mctx, cctx); + _gr_mpoly_set_length(poly1, len1, ctx); TMP_END; diff --git a/src/gr_mpoly/mul_monomial.c b/src/gr_mpoly/mul_monomial.c index 57fb9f5a11..73711a2ab5 100644 --- a/src/gr_mpoly/mul_monomial.c +++ b/src/gr_mpoly/mul_monomial.c @@ -17,8 +17,10 @@ int gr_mpoly_mul_monomial( gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, N, Alen, Blen = B->length; ulong ofmask; flint_bitcnt_t Abits; @@ -44,7 +46,7 @@ int gr_mpoly_mul_monomial( if (C->exps[0] == 0 && mpoly_monomial_is_zero(C->exps, mpoly_words_per_exp(C->bits, mctx))) { - status |= gr_mpoly_mul_scalar(A, B, Ccoeff0, mctx, cctx); + status |= gr_mpoly_mul_scalar(A, B, Ccoeff0, ctx); goto cleanup_C; } @@ -62,7 +64,7 @@ int gr_mpoly_mul_monomial( if (A == B) { /* inplace operation on A */ - gr_mpoly_fit_bits(A, Abits, mctx, cctx); + gr_mpoly_fit_bits(A, Abits, ctx); Bexps = Aexps = A->exps; } else @@ -73,7 +75,7 @@ int gr_mpoly_mul_monomial( mpoly_repack_monomials(Bexps, Abits, B->exps, B->bits, Blen, mctx); } - gr_mpoly_fit_length_reset_bits(A, Blen, Abits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, Blen, Abits, ctx); Aexps = A->exps; } @@ -114,7 +116,7 @@ int gr_mpoly_mul_monomial( /* todo: when we can verify (quickly) that C is invertible */ #if 0 status |= _gr_vec_mul_scalar(A->coeffs, B->coeffs, Blen, Ccoeff0, cctx); - _gr_mpoly_set_length(A, Blen, mctx, cctx); + _gr_mpoly_set_length(A, Blen, ctx); #else { slong sz = cctx->sizeof_elem; diff --git a/src/gr_mpoly/mul_scalar.c b/src/gr_mpoly/mul_scalar.c index cb3c9b60fd..0cc33db403 100644 --- a/src/gr_mpoly/mul_scalar.c +++ b/src/gr_mpoly/mul_scalar.c @@ -13,13 +13,14 @@ #include "mpoly.h" #include "gr_mpoly.h" -/* c is assumed to be reduced mod n */ int gr_mpoly_mul_scalar( gr_mpoly_t A, const gr_mpoly_t B, gr_srcptr c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, N; slong Alen; slong Blen = B->length; @@ -32,10 +33,12 @@ int gr_mpoly_mul_scalar( if (Blen == 0 || gr_is_zero(c, cctx) == T_TRUE) { - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); } /* + todo: integral domains + todo if (gr_is_one(c) == T_TRUE || (Blen > 10 && gr_is_invertible(c, cctx) == T_TRUE)) { @@ -45,7 +48,7 @@ int gr_mpoly_mul_scalar( N = mpoly_words_per_exp(B->bits, mctx); - gr_mpoly_fit_length_reset_bits(A, B->length, B->bits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, B->length, B->bits, ctx); Aexp = A->exps; Bexp = B->exps; @@ -69,19 +72,20 @@ int gr_mpoly_mul_si( gr_mpoly_t A, const gr_mpoly_t B, slong c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { if (c == 0 || B->length == 0) { - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); } else { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_si(t, c, cctx); - status |= gr_mpoly_mul_scalar(A, B, t, mctx, cctx); + status |= gr_mpoly_mul_scalar(A, B, t, ctx); GR_TMP_CLEAR(t, cctx); return status; } @@ -91,19 +95,20 @@ int gr_mpoly_mul_ui( gr_mpoly_t A, const gr_mpoly_t B, ulong c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { if (c == 0 || B->length == 0) { - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); } else { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_ui(t, c, cctx); - status |= gr_mpoly_mul_scalar(A, B, t, mctx, cctx); + status |= gr_mpoly_mul_scalar(A, B, t, ctx); GR_TMP_CLEAR(t, cctx); return status; } @@ -113,19 +118,20 @@ int gr_mpoly_mul_fmpz( gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { if (fmpz_is_zero(c) || B->length == 0) { - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); } else { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpz(t, c, cctx); - status |= gr_mpoly_mul_scalar(A, B, t, mctx, cctx); + status |= gr_mpoly_mul_scalar(A, B, t, ctx); GR_TMP_CLEAR(t, cctx); return status; } @@ -135,19 +141,20 @@ int gr_mpoly_mul_fmpq( gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { if (fmpq_is_zero(c) || B->length == 0) { - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); } else { + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; gr_ptr t; GR_TMP_INIT(t, cctx); status = gr_set_fmpq(t, c, cctx); - status |= gr_mpoly_mul_scalar(A, B, t, mctx, cctx); + status |= gr_mpoly_mul_scalar(A, B, t, ctx); GR_TMP_CLEAR(t, cctx); return status; } diff --git a/src/gr_mpoly/neg.c b/src/gr_mpoly/neg.c index 99984b9c80..a795c28cc7 100644 --- a/src/gr_mpoly/neg.c +++ b/src/gr_mpoly/neg.c @@ -15,8 +15,10 @@ int gr_mpoly_neg( gr_mpoly_t A, const gr_mpoly_t B, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N; slong Blen = B->length; int status = GR_SUCCESS; @@ -24,12 +26,12 @@ int gr_mpoly_neg( if (A != B) { N = mpoly_words_per_exp(B->bits, mctx); - gr_mpoly_fit_length_reset_bits(A, Blen, B->bits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, Blen, B->bits, ctx); mpoly_copy_monomials(A->exps, B->exps, Blen, N); } status = _gr_vec_neg(A->coeffs, B->coeffs, Blen, cctx); - _gr_mpoly_set_length(A, Blen, mctx, cctx); + _gr_mpoly_set_length(A, Blen, ctx); return status; } diff --git a/src/gr_mpoly/push_term.c b/src/gr_mpoly/push_term.c index 0bf4a5d13f..65901376f6 100644 --- a/src/gr_mpoly/push_term.c +++ b/src/gr_mpoly/push_term.c @@ -15,15 +15,16 @@ void _gr_mpoly_push_exp_ui( gr_mpoly_t A, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); slong N; slong old_length = A->length; flint_bitcnt_t exp_bits; exp_bits = mpoly_exp_bits_required_ui(exp, mctx); exp_bits = mpoly_fix_bits(exp_bits, mctx); - gr_mpoly_fit_length_fit_bits(A, old_length + 1, exp_bits, mctx, cctx); + gr_mpoly_fit_length_fit_bits(A, old_length + 1, exp_bits, ctx); A->length = old_length + 1; N = mpoly_words_per_exp(A->bits, mctx); @@ -34,24 +35,26 @@ int gr_mpoly_push_term_scalar_ui( gr_mpoly_t A, gr_srcptr c, const ulong * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - _gr_mpoly_push_exp_ui(A, exp, mctx, cctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); + _gr_mpoly_push_exp_ui(A, exp, ctx); return gr_set(GR_ENTRY(A->coeffs, A->length - 1, cctx->sizeof_elem), c, cctx); } void _gr_mpoly_push_exp_fmpz( gr_mpoly_t A, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); slong N; slong old_length = A->length; flint_bitcnt_t exp_bits; exp_bits = mpoly_exp_bits_required_ffmpz(exp, mctx); exp_bits = mpoly_fix_bits(exp_bits, mctx); - gr_mpoly_fit_length_fit_bits(A, old_length + 1, exp_bits, mctx, cctx); + gr_mpoly_fit_length_fit_bits(A, old_length + 1, exp_bits, ctx); A->length = old_length + 1; N = mpoly_words_per_exp(A->bits, mctx); @@ -62,8 +65,9 @@ int gr_mpoly_push_term_scalar_fmpz( gr_mpoly_t A, gr_srcptr c, const fmpz * exp, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { - _gr_mpoly_push_exp_fmpz(A, exp, mctx, cctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); + _gr_mpoly_push_exp_fmpz(A, exp, ctx); return gr_set(GR_ENTRY(A->coeffs, A->length - 1, cctx->sizeof_elem), c, cctx); } diff --git a/src/gr_mpoly/randtest_bits.c b/src/gr_mpoly/randtest_bits.c index 310a19fd73..e6d52397d6 100644 --- a/src/gr_mpoly/randtest_bits.c +++ b/src/gr_mpoly/randtest_bits.c @@ -14,8 +14,10 @@ #include "gr_mpoly.h" int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, - slong length, flint_bitcnt_t exp_bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) + slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, j, nvars = mctx->nvars; fmpz * exp; int status = GR_SUCCESS; @@ -27,19 +29,19 @@ int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, for (j = 0; j < nvars; j++) fmpz_init(exp + j); - status = gr_mpoly_zero(A, mctx, cctx); + status = gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length_reset_bits(A, 0, MPOLY_MIN_BITS, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, 0, MPOLY_MIN_BITS, ctx); for (i = 0; i < length; i++) { mpoly_monomial_randbits_fmpz(exp, state, exp_bits, mctx); - _gr_mpoly_push_exp_fmpz(A, exp, mctx, cctx); + _gr_mpoly_push_exp_fmpz(A, exp, ctx); status |= gr_randtest(GR_ENTRY(A->coeffs, A->length - 1, cctx->sizeof_elem), state, cctx); } - gr_mpoly_sort_terms(A, mctx, cctx); - status |= gr_mpoly_combine_like_terms(A, mctx, cctx); + gr_mpoly_sort_terms(A, ctx); + status |= gr_mpoly_combine_like_terms(A, ctx); for (j = 0; j < nvars; j++) fmpz_clear(exp + j); diff --git a/src/gr_mpoly/randtest_bound.c b/src/gr_mpoly/randtest_bound.c index 63a31917c1..0cab916e6d 100644 --- a/src/gr_mpoly/randtest_bound.c +++ b/src/gr_mpoly/randtest_bound.c @@ -12,8 +12,10 @@ #include "gr_mpoly.h" int gr_mpoly_randtest_bound(gr_mpoly_t A, flint_rand_t state, - slong length, ulong exp_bound, const mpoly_ctx_t mctx, gr_ctx_t cctx) + slong length, ulong exp_bound, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, j, nvars = mctx->nvars; ulong * exp; int status = GR_SUCCESS; @@ -22,20 +24,20 @@ int gr_mpoly_randtest_bound(gr_mpoly_t A, flint_rand_t state, TMP_START; exp = (ulong *) TMP_ALLOC(nvars*sizeof(ulong)); - status = gr_mpoly_zero(A, mctx, cctx); + status = gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length_reset_bits(A, 0, MPOLY_MIN_BITS, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, 0, MPOLY_MIN_BITS, ctx); for (i = 0; i < length; i++) { for (j = 0; j < nvars; j++) exp[j] = n_randint(state, exp_bound); - _gr_mpoly_push_exp_ui(A, exp, mctx, cctx); + _gr_mpoly_push_exp_ui(A, exp, ctx); status |= gr_randtest(GR_ENTRY(A->coeffs, A->length - 1, cctx->sizeof_elem), state, cctx); } - gr_mpoly_sort_terms(A, mctx, cctx); - status |= gr_mpoly_combine_like_terms(A, mctx, cctx); + gr_mpoly_sort_terms(A, ctx); + status |= gr_mpoly_combine_like_terms(A, ctx); TMP_END; diff --git a/src/gr_mpoly/set.c b/src/gr_mpoly/set.c index 09e2e3384c..4e4558b6ff 100644 --- a/src/gr_mpoly/set.c +++ b/src/gr_mpoly/set.c @@ -15,8 +15,10 @@ int gr_mpoly_set( gr_mpoly_t A, const gr_mpoly_t B, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong N; int status = GR_SUCCESS; @@ -25,11 +27,11 @@ int gr_mpoly_set( N = mpoly_words_per_exp(B->bits, mctx); - gr_mpoly_fit_length_reset_bits(A, B->length, B->bits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, B->length, B->bits, ctx); status = _gr_vec_set(A->coeffs, B->coeffs, B->length, cctx); mpoly_copy_monomials(A->exps, B->exps, B->length, N); - _gr_mpoly_set_length(A, B->length, mctx, cctx); + _gr_mpoly_set_length(A, B->length, ctx); return status; } diff --git a/src/gr_mpoly/set_coeff_scalar_fmpz.c b/src/gr_mpoly/set_coeff_scalar_fmpz.c index 37f022e797..59426c6e75 100644 --- a/src/gr_mpoly/set_coeff_scalar_fmpz.c +++ b/src/gr_mpoly/set_coeff_scalar_fmpz.c @@ -42,7 +42,7 @@ int gr_mpoly_set_coeff_scalar_fmpz( exp_bits = mpoly_exp_bits_required_ffmpz(exp, mctx); exp_bits = mpoly_fix_bits(exp_bits, mctx); - gr_mpoly_fit_length_fit_bits(A, A->length, exp_bits, mctx, cctx); + gr_mpoly_fit_length_fit_bits(A, A->length, exp_bits, ctx); N = mpoly_words_per_exp(A->bits, mctx); cmpmask = (ulong*) TMP_ALLOC(N*sizeof(ulong)); @@ -58,7 +58,7 @@ int gr_mpoly_set_coeff_scalar_fmpz( { if (gr_is_zero(c, cctx) != T_TRUE) /* make new term only if coeff is nonzero*/ { - gr_mpoly_fit_length(A, A->length + 1, mctx, cctx); + gr_mpoly_fit_length(A, A->length + 1, ctx); for (i = A->length; i >= index + 1; i--) { diff --git a/src/gr_mpoly/set_scalar.c b/src/gr_mpoly/set_scalar.c index 8117b2c5a6..a60440f22a 100644 --- a/src/gr_mpoly/set_scalar.c +++ b/src/gr_mpoly/set_scalar.c @@ -15,106 +15,116 @@ int gr_mpoly_set_scalar(gr_mpoly_t A, gr_srcptr c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; if (gr_is_zero(c, cctx) == T_TRUE) - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length(A, 1, mctx, cctx); + gr_mpoly_fit_length(A, 1, ctx); mpoly_monomial_zero(A->exps, mpoly_words_per_exp(A->bits, mctx)); status = gr_set(A->coeffs, c, cctx); - _gr_mpoly_set_length(A, 1, mctx, cctx); + _gr_mpoly_set_length(A, 1, ctx); return status; } int gr_mpoly_set_ui(gr_mpoly_t A, ulong c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; if (c == 0) - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length(A, 1, mctx, cctx); + gr_mpoly_fit_length(A, 1, ctx); mpoly_monomial_zero(A->exps, mpoly_words_per_exp(A->bits, mctx)); status = gr_set_ui(A->coeffs, c, cctx); if (gr_is_zero(A->coeffs, cctx) == T_TRUE) - _gr_mpoly_set_length(A, 0, mctx, cctx); + _gr_mpoly_set_length(A, 0, ctx); else - _gr_mpoly_set_length(A, 1, mctx, cctx); + _gr_mpoly_set_length(A, 1, ctx); return status; } int gr_mpoly_set_si(gr_mpoly_t A, slong c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; if (c == 0) - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length(A, 1, mctx, cctx); + gr_mpoly_fit_length(A, 1, ctx); mpoly_monomial_zero(A->exps, mpoly_words_per_exp(A->bits, mctx)); status = gr_set_si(A->coeffs, c, cctx); if (gr_is_zero(A->coeffs, cctx) == T_TRUE) - _gr_mpoly_set_length(A, 0, mctx, cctx); + _gr_mpoly_set_length(A, 0, ctx); else - _gr_mpoly_set_length(A, 1, mctx, cctx); + _gr_mpoly_set_length(A, 1, ctx); return status; } int gr_mpoly_set_fmpz(gr_mpoly_t A, const fmpz_t c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; if (fmpz_is_zero(c)) - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length(A, 1, mctx, cctx); + gr_mpoly_fit_length(A, 1, ctx); mpoly_monomial_zero(A->exps, mpoly_words_per_exp(A->bits, mctx)); status = gr_set_fmpz(A->coeffs, c, cctx); if (gr_is_zero(A->coeffs, cctx) == T_TRUE) - _gr_mpoly_set_length(A, 0, mctx, cctx); + _gr_mpoly_set_length(A, 0, ctx); else - _gr_mpoly_set_length(A, 1, mctx, cctx); + _gr_mpoly_set_length(A, 1, ctx); return status; } int gr_mpoly_set_fmpq(gr_mpoly_t A, const fmpq_t c, - const mpoly_ctx_t mctx, gr_ctx_t cctx) + gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); int status; if (fmpq_is_zero(c)) - return gr_mpoly_zero(A, mctx, cctx); + return gr_mpoly_zero(A, ctx); - gr_mpoly_fit_length(A, 1, mctx, cctx); + gr_mpoly_fit_length(A, 1, ctx); mpoly_monomial_zero(A->exps, mpoly_words_per_exp(A->bits, mctx)); status = gr_set_fmpq(A->coeffs, c, cctx); if (gr_is_zero(A->coeffs, cctx) == T_TRUE) - _gr_mpoly_set_length(A, 0, mctx, cctx); + _gr_mpoly_set_length(A, 0, ctx); else - _gr_mpoly_set_length(A, 1, mctx, cctx); + _gr_mpoly_set_length(A, 1, ctx); return status; } diff --git a/src/gr_mpoly/sort_terms.c b/src/gr_mpoly/sort_terms.c index d41f3f7df0..9fde003e7e 100644 --- a/src/gr_mpoly/sort_terms.c +++ b/src/gr_mpoly/sort_terms.c @@ -187,8 +187,10 @@ void _gr_mpoly_radix_sort( sort the terms in A by exponent assuming that the exponents are valid (other than being in order) */ -void gr_mpoly_sort_terms(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +void gr_mpoly_sort_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong i, N; flint_bitcnt_t pos; gr_ptr Acoeffs = A->coeffs; diff --git a/src/gr_mpoly/sub.c b/src/gr_mpoly/sub.c index 7a97a5d5b9..cbf9704ed4 100644 --- a/src/gr_mpoly/sub.c +++ b/src/gr_mpoly/sub.c @@ -78,12 +78,10 @@ slong _gr_mpoly_sub( return status; } -int gr_mpoly_sub( - gr_mpoly_t A, - const gr_mpoly_t B, - const gr_mpoly_t C, - const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_sub(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong Abits, N; ulong * Bexps = B->exps, * Cexps = C->exps; ulong * cmpmask; @@ -92,10 +90,13 @@ int gr_mpoly_sub( TMP_INIT; if (B->length == 0) - return gr_mpoly_neg(A, C, mctx, cctx); + return gr_mpoly_neg(A, C, ctx); if (C->length == 0) - return gr_mpoly_set(A, B, mctx, cctx); + return gr_mpoly_set(A, B, ctx); + + if (B->length + C->length > ctx->size_limit) + return GR_UNABLE | gr_mpoly_zero(A, ctx); TMP_START; Abits = FLINT_MAX(B->bits, C->bits); @@ -122,17 +123,17 @@ int gr_mpoly_sub( if (A == B || A == C) { gr_mpoly_t T; - gr_mpoly_init3(T, B->length + C->length, Abits, mctx, cctx); + gr_mpoly_init3(T, B->length + C->length, Abits, ctx); status = _gr_mpoly_sub(&T->length, T->coeffs, T->exps, B->coeffs, Bexps, B->length, C->coeffs, Cexps, C->length, N, cmpmask, cctx); - gr_mpoly_swap(A, T, mctx, cctx); - gr_mpoly_clear(T, mctx, cctx); + gr_mpoly_swap(A, T, ctx); + gr_mpoly_clear(T, ctx); } else { - gr_mpoly_fit_length_reset_bits(A, B->length + C->length, Abits, mctx, cctx); + gr_mpoly_fit_length_reset_bits(A, B->length + C->length, Abits, ctx); status = _gr_mpoly_sub(&A->length, A->coeffs, A->exps, B->coeffs, Bexps, B->length, C->coeffs, Cexps, C->length, diff --git a/src/gr_mpoly/test/t-add_sub.c b/src/gr_mpoly/test/t-add_sub.c index 0d4599be73..ca3eb2e8e9 100644 --- a/src/gr_mpoly/test/t-add_sub.c +++ b/src/gr_mpoly/test/t-add_sub.c @@ -29,12 +29,12 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) gr_ctx_init_random(cctx, state); gr_mpoly_ctx_init_rand(ctx, state, cctx, 10); - NEW_gr_mpoly_init(f, ctx); - NEW_gr_mpoly_init(g, ctx); - NEW_gr_mpoly_init(h, ctx); - NEW_gr_mpoly_init(k, ctx); - NEW_gr_mpoly_init(k1, ctx); - NEW_gr_mpoly_init(k2, ctx); + gr_mpoly_init(f, ctx); + gr_mpoly_init(g, ctx); + gr_mpoly_init(h, ctx); + gr_mpoly_init(k, ctx); + gr_mpoly_init(k1, ctx); + gr_mpoly_init(k2, ctx); len = n_randint(state, 100); len1 = n_randint(state, 100); @@ -49,29 +49,29 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_add(h, g, f, ctx); - status |= NEW_gr_mpoly_sub(k, h, g, ctx); + status |= gr_mpoly_add(h, g, f, ctx); + status |= gr_mpoly_sub(k, h, g, ctx); if (status == GR_SUCCESS) { - NEW_gr_mpoly_assert_canonical(h, ctx); - NEW_gr_mpoly_assert_canonical(k, ctx); + gr_mpoly_assert_canonical(h, ctx); + gr_mpoly_assert_canonical(k, ctx); } - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, k, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(f, k, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("f + g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("(f + g) - g = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("f + g = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("(f + g) - g = "); gr_mpoly_print_pretty(k, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } @@ -82,23 +82,23 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_add(h, f, g, ctx); - status |= NEW_gr_mpoly_add(k, g, f, ctx); + status |= gr_mpoly_add(h, f, g, ctx); + status |= gr_mpoly_add(k, g, f, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(h, k, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(h, k, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) = (g + f)\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("f + g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("g + f = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("f + g = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("g + f = "); gr_mpoly_print_pretty(k, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } @@ -109,24 +109,24 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_sub(h, f, g, ctx); - status |= NEW_gr_mpoly_neg(k, g, ctx); - status |= NEW_gr_mpoly_add(k, k, f, ctx); + status |= gr_mpoly_sub(h, f, g, ctx); + status |= gr_mpoly_neg(k, g, ctx); + status |= gr_mpoly_add(k, k, f, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(h, k, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(h, k, ctx) == T_FALSE) { flint_printf("FAIL: Check f - g = -g + f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("f - g = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("-g + f = "); NEW_gr_mpoly_print_pretty(k, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("f - g = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("-g + f = "); gr_mpoly_print_pretty(k, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } @@ -137,27 +137,27 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_add(k1, g, h, ctx); - status |= NEW_gr_mpoly_add(k1, f, k1, ctx); - status |= NEW_gr_mpoly_add(k2, f, g, ctx); - status |= NEW_gr_mpoly_add(k2, k2, h, ctx); + status |= gr_mpoly_add(k1, g, h, ctx); + status |= gr_mpoly_add(k1, f, k1, ctx); + status |= gr_mpoly_add(k2, f, g, ctx); + status |= gr_mpoly_add(k2, k2, h, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL Check f + (g + h) = (f + g) + h\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("f + (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); - flint_printf("(f + g) + h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("h = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("f + (g + h) = "); gr_mpoly_print_pretty(k1, ctx); flint_printf("\n"); + flint_printf("(f + g) + h = "); gr_mpoly_print_pretty(k2, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } @@ -168,27 +168,27 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_add(k1, g, h, ctx); - status |= NEW_gr_mpoly_sub(k1, f, k1, ctx); - status |= NEW_gr_mpoly_sub(k2, f, g, ctx); - status |= NEW_gr_mpoly_sub(k2, k2, h, ctx); + status |= gr_mpoly_add(k1, g, h, ctx); + status |= gr_mpoly_sub(k1, f, k1, ctx); + status |= gr_mpoly_sub(k2, f, g, ctx); + status |= gr_mpoly_sub(k2, k2, h, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL Check f + (g + h) = (f + g) + h\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("f + (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); - flint_printf("(f + g) + h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("h = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("f + (g + h) = "); gr_mpoly_print_pretty(k1, ctx); flint_printf("\n"); + flint_printf("(f + g) + h = "); gr_mpoly_print_pretty(k2, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } @@ -199,17 +199,17 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_set(h, f, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_set(h, f, ctx); - status |= NEW_gr_mpoly_add(f, f, g, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); - status |= NEW_gr_mpoly_sub(f, f, g, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_add(f, f, g, ctx); + gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_sub(f, f, g, ctx); + gr_mpoly_assert_canonical(f, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, h, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(f, h, ctx) == T_FALSE) { flint_printf("FAIL: Check aliasing first arg\n"); flint_printf("i = %wd, j = %wd\n", i, j); @@ -217,26 +217,26 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) flint_abort(); } - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); if ((j % 2) == 0) { - status |= NEW_gr_mpoly_add(h, g, f, ctx); - NEW_gr_mpoly_assert_canonical(h, ctx); - status |= NEW_gr_mpoly_add(f, g, f, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_add(h, g, f, ctx); + gr_mpoly_assert_canonical(h, ctx); + status |= gr_mpoly_add(f, g, f, ctx); + gr_mpoly_assert_canonical(f, ctx); } else { - status |= NEW_gr_mpoly_sub(h, g, f, ctx); - NEW_gr_mpoly_assert_canonical(h, ctx); - status |= NEW_gr_mpoly_sub(f, g, f, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); + status |= gr_mpoly_sub(h, g, f, ctx); + gr_mpoly_assert_canonical(h, ctx); + status |= gr_mpoly_sub(f, g, f, ctx); + gr_mpoly_assert_canonical(f, ctx); } - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(f, h, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(f, h, ctx) == T_FALSE) { flint_printf("FAIL: Check aliasing second arg\n"); flint_printf("i = %wd, j = %wd\n", i, j); @@ -245,12 +245,12 @@ TEST_FUNCTION_START(gr_mpoly_add_sub, state) } } - NEW_gr_mpoly_clear(f, ctx); - NEW_gr_mpoly_clear(g, ctx); - NEW_gr_mpoly_clear(h, ctx); - NEW_gr_mpoly_clear(k, ctx); - NEW_gr_mpoly_clear(k1, ctx); - NEW_gr_mpoly_clear(k2, ctx); + gr_mpoly_clear(f, ctx); + gr_mpoly_clear(g, ctx); + gr_mpoly_clear(h, ctx); + gr_mpoly_clear(k, ctx); + gr_mpoly_clear(k1, ctx); + gr_mpoly_clear(k2, ctx); gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); diff --git a/src/gr_mpoly/test/t-gen.c b/src/gr_mpoly/test/t-gen.c index bdd6fa13a4..bea5d0f029 100644 --- a/src/gr_mpoly/test/t-gen.c +++ b/src/gr_mpoly/test/t-gen.c @@ -20,7 +20,7 @@ TEST_FUNCTION_START(gr_mpoly_gen, state) for (iter = 0; iter < 100; iter++) { gr_ctx_t cctx; - mpoly_ctx_t mctx; + gr_mpoly_ctx_t ctx; gr_mpoly_t f1, f2; slong len, k1, k2; flint_bitcnt_t exp_bits; @@ -28,45 +28,45 @@ TEST_FUNCTION_START(gr_mpoly_gen, state) truth_t eq1, eq2, eq3, eq4; gr_ctx_init_random(cctx, state); - mpoly_ctx_init_rand(mctx, state, 10); + gr_mpoly_ctx_init_rand(ctx, state, cctx, 10); - gr_mpoly_init(f1, mctx, cctx); - gr_mpoly_init(f2, mctx, cctx); + gr_mpoly_init(f1, ctx); + gr_mpoly_init(f2, ctx); len = n_randint(state, 10); exp_bits = n_randint(state, 100) + 2; - status = gr_mpoly_zero(f1, mctx, cctx); - status = gr_mpoly_one(f2, mctx, cctx); + status = gr_mpoly_zero(f1, ctx); + status = gr_mpoly_one(f2, ctx); - if (gr_mpoly_equal(f1, f2, mctx, cctx) != T_TRUE) + if (gr_mpoly_equal(f1, f2, ctx) != T_TRUE) { - gr_mpoly_randtest_bits(f1, state, len, exp_bits, mctx, cctx); - gr_mpoly_randtest_bits(f2, state, len, exp_bits, mctx, cctx); + gr_mpoly_randtest_bits(f1, state, len, exp_bits, ctx); + gr_mpoly_randtest_bits(f2, state, len, exp_bits, ctx); - k1 = n_randint(state, FLINT_MAX(1, mctx->nvars)); - k2 = n_randint(state, FLINT_MAX(1, mctx->nvars)); + k1 = n_randint(state, FLINT_MAX(1, GR_MPOLY_NVARS(ctx))); + k2 = n_randint(state, FLINT_MAX(1, GR_MPOLY_NVARS(ctx))); status = GR_SUCCESS; - status |= gr_mpoly_gen(f1, k1, mctx, cctx); - gr_mpoly_assert_canonical(f1, mctx, cctx); + status |= gr_mpoly_gen(f1, k1, ctx); + gr_mpoly_assert_canonical(f1, ctx); - status |= gr_mpoly_gen(f2, k2, mctx, cctx); - gr_mpoly_assert_canonical(f2, mctx, cctx); + status |= gr_mpoly_gen(f2, k2, ctx); + gr_mpoly_assert_canonical(f2, ctx); - eq1 = gr_mpoly_is_gen(f1, k1, mctx, cctx); - eq2 = gr_mpoly_is_gen(f1, -1, mctx, cctx); - eq3 = gr_mpoly_is_gen(f2, k2, mctx, cctx); - eq4 = gr_mpoly_is_gen(f2, -1, mctx, cctx); + eq1 = gr_mpoly_is_gen(f1, k1, ctx); + eq2 = gr_mpoly_is_gen(f1, -1, ctx); + eq3 = gr_mpoly_is_gen(f2, k2, ctx); + eq4 = gr_mpoly_is_gen(f2, -1, ctx); if (status == GR_SUCCESS && (eq1 == T_FALSE || eq2 == T_FALSE || eq3 == T_FALSE || eq4 == T_FALSE)) { flint_printf("FAIL\n"); gr_ctx_println(cctx); - flint_printf("nvars = %wd\n", mctx->nvars); - flint_printf("f1 = "); gr_mpoly_print_pretty(f1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f2 = "); gr_mpoly_print_pretty(f2, NULL, mctx, cctx); flint_printf("\n"); + flint_printf("nvars = %wd\n", GR_MPOLY_NVARS(ctx)); + flint_printf("f1 = "); gr_mpoly_print_pretty(f1, ctx); flint_printf("\n"); + flint_printf("f2 = "); gr_mpoly_print_pretty(f2, ctx); flint_printf("\n"); flint_printf("k1 = %wd\n", k1); flint_printf("k2 = %wd\n", k2); flint_printf("eq = %d %d %d %d\n", eq1, eq2, eq3, eq4); @@ -75,21 +75,21 @@ TEST_FUNCTION_START(gr_mpoly_gen, state) } if (n_randint(state, 2)) - status |= gr_mpoly_add(f1, f1, f2, mctx, cctx); + status |= gr_mpoly_add(f1, f1, f2, ctx); else - status |= gr_mpoly_mul(f1, f1, f2, mctx, cctx); + status |= gr_mpoly_mul(f1, f1, f2, ctx); - eq1 = gr_mpoly_is_gen(f1, k1, mctx, cctx); - eq2 = gr_mpoly_is_gen(f1, k2, mctx, cctx); - eq3 = gr_mpoly_is_gen(f1, -1, mctx, cctx); + eq1 = gr_mpoly_is_gen(f1, k1, ctx); + eq2 = gr_mpoly_is_gen(f1, k2, ctx); + eq3 = gr_mpoly_is_gen(f1, -1, ctx); if (status == GR_SUCCESS && (eq1 == T_TRUE || eq2 == T_TRUE || eq3 == T_TRUE)) { flint_printf("FAIL\n"); gr_ctx_println(cctx); - flint_printf("nvars = %wd\n", mctx->nvars); - flint_printf("f1 = "); gr_mpoly_print_pretty(f1, NULL, mctx, cctx); flint_printf("\n"); - flint_printf("f2 = "); gr_mpoly_print_pretty(f2, NULL, mctx, cctx); flint_printf("\n"); + flint_printf("nvars = %wd\n", GR_MPOLY_NVARS(ctx)); + flint_printf("f1 = "); gr_mpoly_print_pretty(f1, ctx); flint_printf("\n"); + flint_printf("f2 = "); gr_mpoly_print_pretty(f2, ctx); flint_printf("\n"); flint_printf("k1 = %wd\n", k1); flint_printf("k2 = %wd\n", k2); flint_printf("eq = %d %d %d\n", eq1, eq2, eq3); @@ -98,10 +98,10 @@ TEST_FUNCTION_START(gr_mpoly_gen, state) } } - gr_mpoly_clear(f1, mctx, cctx); - gr_mpoly_clear(f2, mctx, cctx); + gr_mpoly_clear(f1, ctx); + gr_mpoly_clear(f2, ctx); - mpoly_ctx_clear(mctx); + gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); } diff --git a/src/gr_mpoly/test/t-get_set_coeff.c b/src/gr_mpoly/test/t-get_set_coeff.c index 3bf84f3727..f0a0880da1 100644 --- a/src/gr_mpoly/test/t-get_set_coeff.c +++ b/src/gr_mpoly/test/t-get_set_coeff.c @@ -33,14 +33,14 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) gr_mpoly_ctx_init_rand(ctx, state, cctx, 20); nvars = GR_MPOLY_NVARS(ctx); - NEW_gr_mpoly_init(f, ctx); + gr_mpoly_init(f, ctx); c = gr_heap_init(cctx); d = gr_heap_init(cctx); len = n_randint(state, 100); exp_bits = n_randint(state, 200) + 2; - GR_MUST_SUCCEED(NEW_gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); + GR_MUST_SUCCEED(gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); for (j = 0; j < 10; j++) { @@ -53,7 +53,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) exp[k] = n_randtest(state); status |= gr_mpoly_set_coeff_scalar_ui(f, c, exp, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); + gr_mpoly_assert_canonical(f, ctx); status |= gr_mpoly_get_coeff_scalar_ui(d, f, exp, ctx); if (status == GR_SUCCESS && gr_equal(c, d, cctx) == T_FALSE) @@ -61,7 +61,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) flint_printf("FAIL: scalar_ui\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); flint_printf("c = "); gr_print(c, cctx); flint_printf("\n"); flint_printf("d = "); gr_print(d, cctx); flint_printf("\n"); fflush(stdout); @@ -71,7 +71,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) flint_free(exp); } - GR_MUST_SUCCEED(NEW_gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); + GR_MUST_SUCCEED(gr_mpoly_randtest_bits(f, state, len, exp_bits, ctx)); for (j = 0; j < 10; j++) { @@ -83,7 +83,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) _fmpz_vec_randtest(exp, state, nvars, exp_bits); status |= gr_mpoly_set_coeff_scalar_fmpz(f, c, exp, ctx); - NEW_gr_mpoly_assert_canonical(f, ctx); + gr_mpoly_assert_canonical(f, ctx); status |= gr_mpoly_get_coeff_scalar_fmpz(d, f, exp, ctx); if (status == GR_SUCCESS && gr_equal(c, d, cctx) == T_FALSE) @@ -91,7 +91,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) flint_printf("FAIL: scalar_fmpz\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); flint_printf("c = "); gr_print(c, cctx); flint_printf("\n"); flint_printf("d = "); gr_print(d, cctx); flint_printf("\n"); fflush(stdout); @@ -101,7 +101,7 @@ TEST_FUNCTION_START(gr_mpoly_get_set_coeff, state) _fmpz_vec_clear(exp, nvars); } - NEW_gr_mpoly_clear(f, ctx); + gr_mpoly_clear(f, ctx); gr_heap_clear(c, cctx); gr_heap_clear(d, cctx); diff --git a/src/gr_mpoly/test/t-mul_johnson.c b/src/gr_mpoly/test/t-mul_johnson.c index 01c50233cd..16176fac93 100644 --- a/src/gr_mpoly/test/t-mul_johnson.c +++ b/src/gr_mpoly/test/t-mul_johnson.c @@ -31,12 +31,12 @@ TEST_FUNCTION_START(gr_mpoly_mul_johnson, state) gr_ctx_init_random(cctx, state); gr_mpoly_ctx_init_rand(ctx, state, cctx, 4); - NEW_gr_mpoly_init(f, ctx); - NEW_gr_mpoly_init(g, ctx); - NEW_gr_mpoly_init(h, ctx); - NEW_gr_mpoly_init(k1, ctx); - NEW_gr_mpoly_init(k2, ctx); - NEW_gr_mpoly_init(t, ctx); + gr_mpoly_init(f, ctx); + gr_mpoly_init(g, ctx); + gr_mpoly_init(h, ctx); + gr_mpoly_init(k1, ctx); + gr_mpoly_init(k2, ctx); + gr_mpoly_init(t, ctx); if (cctx->methods == _ca_methods) { @@ -61,55 +61,55 @@ TEST_FUNCTION_START(gr_mpoly_mul_johnson, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_add(k1, g, h, ctx); + status |= gr_mpoly_add(k1, g, h, ctx); if (n_randint(state, 2)) - status |= NEW_gr_mpoly_mul_johnson(k1, f, k1, ctx); + status |= gr_mpoly_mul_johnson(k1, f, k1, ctx); else - status |= NEW_gr_mpoly_mul_johnson(k1, k1, f, ctx); + status |= gr_mpoly_mul_johnson(k1, k1, f, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(k1, ctx); + gr_mpoly_assert_canonical(k1, ctx); - status |= NEW_gr_mpoly_mul_johnson(k2, f, g, ctx); + status |= gr_mpoly_mul_johnson(k2, f, g, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(k2, ctx); + gr_mpoly_assert_canonical(k2, ctx); - status |= NEW_gr_mpoly_mul_johnson(t, f, h, ctx); + status |= gr_mpoly_mul_johnson(t, f, h, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(t, ctx); + gr_mpoly_assert_canonical(t, ctx); - status |= NEW_gr_mpoly_add(k2, k2, t, ctx); + status |= gr_mpoly_add(k2, k2, t, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("f * (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); - flint_printf("f * g + f * h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("h = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("f * (g + h) = "); gr_mpoly_print_pretty(k1, ctx); flint_printf("\n"); + flint_printf("f * g + f * h = "); gr_mpoly_print_pretty(k2, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - NEW_gr_mpoly_clear(f, ctx); - NEW_gr_mpoly_clear(g, ctx); - NEW_gr_mpoly_clear(h, ctx); - NEW_gr_mpoly_clear(k1, ctx); - NEW_gr_mpoly_clear(k2, ctx); - NEW_gr_mpoly_clear(t, ctx); + gr_mpoly_clear(f, ctx); + gr_mpoly_clear(g, ctx); + gr_mpoly_clear(h, ctx); + gr_mpoly_clear(k1, ctx); + gr_mpoly_clear(k2, ctx); + gr_mpoly_clear(t, ctx); gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); diff --git a/src/gr_mpoly/test/t-mul_monomial.c b/src/gr_mpoly/test/t-mul_monomial.c index ee3a87602a..86555c042d 100644 --- a/src/gr_mpoly/test/t-mul_monomial.c +++ b/src/gr_mpoly/test/t-mul_monomial.c @@ -31,12 +31,12 @@ TEST_FUNCTION_START(gr_mpoly_mul_monomial, state) gr_ctx_init_random(cctx, state); gr_mpoly_ctx_init_rand(ctx, state, cctx, 4); - NEW_gr_mpoly_init(f, ctx); - NEW_gr_mpoly_init(g, ctx); - NEW_gr_mpoly_init(h, ctx); - NEW_gr_mpoly_init(k1, ctx); - NEW_gr_mpoly_init(k2, ctx); - NEW_gr_mpoly_init(t, ctx); + gr_mpoly_init(f, ctx); + gr_mpoly_init(g, ctx); + gr_mpoly_init(h, ctx); + gr_mpoly_init(k1, ctx); + gr_mpoly_init(k2, ctx); + gr_mpoly_init(t, ctx); if (cctx->methods == _ca_methods) { @@ -59,58 +59,58 @@ TEST_FUNCTION_START(gr_mpoly_mul_monomial, state) { status = GR_SUCCESS; - status |= NEW_gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); - status |= NEW_gr_mpoly_randtest_bits(g, state, 1, exp_bits2, ctx); - status |= NEW_gr_mpoly_randtest_bits(h, state, 1, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); - status |= NEW_gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx); + status |= gr_mpoly_randtest_bits(g, state, 1, exp_bits2, ctx); + status |= gr_mpoly_randtest_bits(h, state, 1, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k1, state, len, exp_bits, ctx); + status |= gr_mpoly_randtest_bits(k2, state, len, exp_bits, ctx); if (g->length != 1 || h->length != 1) continue; - status |= NEW_gr_mpoly_add(k1, g, h, ctx); + status |= gr_mpoly_add(k1, g, h, ctx); if (n_randint(state, 2)) - status |= NEW_gr_mpoly_mul_johnson(k1, f, k1, ctx); + status |= gr_mpoly_mul_johnson(k1, f, k1, ctx); else - status |= NEW_gr_mpoly_mul_johnson(k1, k1, f, ctx); + status |= gr_mpoly_mul_johnson(k1, k1, f, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(k1, ctx); + gr_mpoly_assert_canonical(k1, ctx); - status |= NEW_gr_mpoly_mul_monomial(k2, f, g, ctx); + status |= gr_mpoly_mul_monomial(k2, f, g, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(k2, ctx); + gr_mpoly_assert_canonical(k2, ctx); - status |= NEW_gr_mpoly_mul_monomial(t, f, h, ctx); + status |= gr_mpoly_mul_monomial(t, f, h, ctx); if (status == GR_SUCCESS) - NEW_gr_mpoly_assert_canonical(t, ctx); + gr_mpoly_assert_canonical(t, ctx); - status |= NEW_gr_mpoly_add(k2, k2, t, ctx); + status |= gr_mpoly_add(k2, k2, t, ctx); - if (status == GR_SUCCESS && NEW_gr_mpoly_equal(k1, k2, ctx) == T_FALSE) + if (status == GR_SUCCESS && gr_mpoly_equal(k1, k2, ctx) == T_FALSE) { flint_printf("FAIL: Check (f + g) - g = f\n"); flint_printf("i = %wd, j = %wd\n", i ,j); gr_ctx_println(ctx); - flint_printf("f = "); NEW_gr_mpoly_print_pretty(f, NULL, ctx); flint_printf("\n"); - flint_printf("g = "); NEW_gr_mpoly_print_pretty(g, NULL, ctx); flint_printf("\n"); - flint_printf("h = "); NEW_gr_mpoly_print_pretty(h, NULL, ctx); flint_printf("\n"); - flint_printf("f * (g + h) = "); NEW_gr_mpoly_print_pretty(k1, NULL, ctx); flint_printf("\n"); - flint_printf("f * g + f * h = "); NEW_gr_mpoly_print_pretty(k2, NULL, ctx); flint_printf("\n"); + flint_printf("f = "); gr_mpoly_print_pretty(f, ctx); flint_printf("\n"); + flint_printf("g = "); gr_mpoly_print_pretty(g, ctx); flint_printf("\n"); + flint_printf("h = "); gr_mpoly_print_pretty(h, ctx); flint_printf("\n"); + flint_printf("f * (g + h) = "); gr_mpoly_print_pretty(k1, ctx); flint_printf("\n"); + flint_printf("f * g + f * h = "); gr_mpoly_print_pretty(k2, ctx); flint_printf("\n"); fflush(stdout); flint_abort(); } } - NEW_gr_mpoly_clear(f, ctx); - NEW_gr_mpoly_clear(g, ctx); - NEW_gr_mpoly_clear(h, ctx); - NEW_gr_mpoly_clear(k1, ctx); - NEW_gr_mpoly_clear(k2, ctx); - NEW_gr_mpoly_clear(t, ctx); + gr_mpoly_clear(f, ctx); + gr_mpoly_clear(g, ctx); + gr_mpoly_clear(h, ctx); + gr_mpoly_clear(k1, ctx); + gr_mpoly_clear(k2, ctx); + gr_mpoly_clear(t, ctx); gr_mpoly_ctx_clear(ctx); gr_ctx_clear(cctx); diff --git a/src/gr_mpoly/write.c b/src/gr_mpoly/write.c index 03e08ae6ec..adf8330369 100644 --- a/src/gr_mpoly/write.c +++ b/src/gr_mpoly/write.c @@ -43,16 +43,17 @@ want_parens(const char * s) } /* todo: error handling */ -int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, - const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { + mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx); + gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx); slong len = A->length; ulong * exp = A->exps; slong bits = A->bits; slong i, j, N; fmpz * exponents; char * s; - char ** x = (char **) x_in; + char ** x = GR_MPOLY_VARS(ctx); TMP_INIT; if (len == 0) @@ -187,10 +188,9 @@ int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, return GR_SUCCESS; } -int gr_mpoly_print_pretty(const gr_mpoly_t A, - const char ** x_in, const mpoly_ctx_t mctx, gr_ctx_t cctx) +int gr_mpoly_print_pretty(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) { gr_stream_t out; gr_stream_init_file(out, stdout); - return gr_mpoly_write_pretty(out, A, x_in, mctx, cctx); + return gr_mpoly_write_pretty(out, A, ctx); } From 23a01a75453cd041cd603bb33ac22c6902bdccb5 Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Sun, 12 Jan 2025 14:28:37 +0100 Subject: [PATCH 4/4] cleanup; docs --- doc/source/gr_mpoly.rst | 247 +++++++++++++++++++++++++++++----------- src/gr/mpoly.c | 222 +----------------------------------- src/gr_mpoly.h | 58 ++++++---- src/gr_mpoly/ctx.c | 229 +++++++++++++++++++++++++++++++++++++ src/gr_mpoly/write.c | 6 + 5 files changed, 451 insertions(+), 311 deletions(-) create mode 100644 src/gr_mpoly/ctx.c diff --git a/doc/source/gr_mpoly.rst b/doc/source/gr_mpoly.rst index da81e45e3d..9c8164c9a3 100644 --- a/doc/source/gr_mpoly.rst +++ b/doc/source/gr_mpoly.rst @@ -3,89 +3,196 @@ **gr_mpoly.h** -- sparse multivariate polynomials over generic rings =============================================================================== -A :type:`gr_mpoly_t` represents a multivariate polynomial -`f \in R[X_1,\ldots,X_n]` implemented as an array of coefficients -in a generic ring *R* together with an array of packed exponents. - -Weak normalization -------------------------------------------------------------------------------- - -A :type:`gr_mpoly_t` is always normalised by removing zero -coefficients. -For rings without decidable equality (e.g. rings with inexact -representation), only coefficients that are provably zero will be -removed, and there can thus be spurious zeros in the -internal representation. -Methods that depend on knowing the exact structure of a polynomial -will act appropriately, typically by returning ``GR_UNABLE`` -when it is unknown whether any stored coefficients are nonzero. +This module implements multivariate polynomials +with ``gr`` coefficients. Types, macros and constants ------------------------------------------------------------------------------- .. type:: gr_mpoly_struct - -.. type:: gr_mpoly_t + gr_mpoly_t + + Represents a multivariate polynomial + `f \in R[X_1,\ldots,X_n]` as an array of coefficients + in a generic ring *R* together with an array of packed exponents. + The two arrays always have the same length. + + A :type:`gr_mpoly_t` is always normalised by removing zero + coefficients. + For rings without decidable equality (e.g. rings with inexact + representation), only coefficients that are provably zero will be + removed, and there can thus be spurious zeros in the + internal representation. For example, with ball coefficients + one can have the polynomial `3 x y^2 + [\pm 0.01] x y`. + Over rings with this issue, the represented lengths or degrees are + thus upper bounds, and methods that depend on knowing the exact + term structure of a polynomial will return ``GR_UNABLE`` + when encountering such input. A ``gr_mpoly_t`` is defined as an array of length one of type ``gr_mpoly_struct``, permitting a ``gr_mpoly_t`` to be passed by reference. +.. type:: gr_mpoly_ctx_struct + gr_mpoly_ctx_t + + Context object representing a multivariate polynomial ring + `R[X_1,\ldots,X_n]`. + This subtypes :type:`gr_ctx_t`, allowing + generic ``gr`` and ``gr_ctx`` methods to be used interchangeably + with ``gr_mpoly`` and ``gr_mpoly_ctx`` methods. For example, + :func:`gr_add` with :type:`gr_mpoly_t` and :type:`gr_mpoly_ctx_t` + arguments is equivalent to :func:`gr_mpoly_add`. + A context object contains the following data: + + * A pointer *mctx* to a :type:`gr_ctx_t` representing the coefficient type *R*. + The coefficient context object is not considered owned by + the ``gr_mpoly_ctx`` and the user must ensure + that it stays alive as long as the ``gr_mpoly_ctx`` is alive. + + * A pointer *cctx* to a :type:`mpoly_ctx_t` defining the number of + variables and term ordering. This object is considered + owned by and will automatically be initialized and cleared + along with the ``gr_mpoly_ctx``. + + * An optional pointer *vars* to an array of strings + specifying names of the generators `X_1, \ldots, X_n`. + This can be set with + :func:`gr_mpoly_ctx_set_gen_names`. By default, *vars* will be + initialized to ``NULL`` in which case some default names + are used. + Names are used for printing and parsing from strings + with :func:`gr_set_str` and are otherwise ignored for computations. + Currently, coercions between multivariate polynomial rings + match generators by index and ignore names; + in the future, an option may be added to match by name. + +.. macro:: GR_MPOLY_MCTX(ctx) + + Access the mpoly context object *mctx*. + +.. macro:: GR_MPOLY_CCTX(ctx) + + Access the coefficient context object *cctx*. + +.. macro:: GR_MPOLY_VARS(ctx) + + Access the array of variable names *vars*. + +.. macro:: GR_MPOLY_NVARS(ctx) + + Access the number of variables of this context object. + +Context object methods +------------------------------------------------------------------------------- + +.. function:: void gr_mpoly_ctx_init(gr_mpoly_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord) + + Initializes ``ctx`` to represent a polynomial ring with + coefficients in ``base_ring``, with ``nvars`` variables + and term ordering ``ord``. + +.. function:: void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx) + + Clears the context object ``ctx``. + +.. function:: void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars) + + Initializes ``ctx`` with a random number of variables + up to ``max_nvars`` inclusive and with a random term ordering. + +The following methods implement parts of the standard interface +for ``gr`` context objects. + +.. function:: int gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s) + + Sets the names of the generators to the strings in ``s``. + +.. function:: int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx) + truth_t gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx) + Memory management ------------------------------------------------------------------------------- -.. function:: void gr_mpoly_init(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_init(gr_mpoly_t A, gr_mpoly_ctx_t ctx) Initializes and sets *A* to the zero polynomial. -.. function:: void gr_mpoly_init3(gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) - void gr_mpoly_init2(gr_mpoly_t A, slong alloc, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_init3(gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx) + void gr_mpoly_init2(gr_mpoly_t A, slong alloc, gr_mpoly_ctx_t ctx) Initializes *A* with space allocated for the given number of coefficients and exponents with the given number of bits. -.. function:: void gr_mpoly_clear(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_clear(gr_mpoly_t A, gr_mpoly_ctx_t ctx) Clears *A*, freeing all allocated data. Basic manipulation ------------------------------------------------------------------------------- -.. function:: void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t ctx) Swaps *A* and *B* efficiently. -.. function:: int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_set_shallow(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx) + + Sets *A* to a shallow copy of *B* (unsafe). + +.. function:: int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx) Sets *A* to *B*. -.. function:: int gr_mpoly_zero(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_zero(gr_mpoly_t A, gr_mpoly_ctx_t ctx) Sets *A* to the zero polynomial. -.. function:: truth_t gr_mpoly_is_zero(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: truth_t gr_mpoly_is_zero(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) Returns whether *A* is the zero polynomial. -.. function:: int gr_mpoly_gen(gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: slong gr_mpoly_length(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) + + Returns the number of terms in *A*. + +Generators +------------------------------------------------------------------------------- + +.. function:: int gr_mpoly_gen(gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx) Sets *A* to the generator with index *var* (indexed from zero). -.. function:: truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx) Returns whether *A* is the generator with index *var* (indexed from zero). +.. function:: int gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) + + Sets the vector *res* to a list of the generators `X_1, \ldots, X_n`. + +.. function:: int gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) + + Sets the vector *res* to a list of the recursive generators of `R` + (as constant elements of `R[X_1, \ldots, X_n]`) + followed by the generators `X_1, \ldots, X_n`. + + Comparisons ------------------------------------------------------------------------------- -.. function:: truth_t gr_mpoly_equal(const gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: truth_t gr_mpoly_equal(const gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx) Returns whether *A* and *B* are equal. Random generation ------------------------------------------------------------------------------- -.. function:: int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx) Sets *A* to a random polynomial with up to *length* terms and up to *exp_bits* bits in the exponents. @@ -93,8 +200,10 @@ Random generation Input and output ------------------------------------------------------------------------------- -.. function:: int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, const char ** x, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x, const mpoly_ctx_t mctx, gr_ctx_t cctx) +Note: :func:`gr_set_str` can be used for parsing. + +.. function:: int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, const char ** x, gr_mpoly_ctx_t ctx) + int gr_mpoly_print_pretty(const gr_mpoly_t A, const char ** x, gr_mpoly_ctx_t ctx) Prints *A* using the strings in *x* for the variables. If *x* is *NULL*, defaults are used. @@ -102,22 +211,22 @@ Input and output Coefficient and exponent access ------------------------------------------------------------------------------- -.. function:: int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_get_coeff_scalar_ui(gr_ptr c, const gr_mpoly_t A, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_get_coeff_scalar_fmpz(gr_ptr c, const gr_mpoly_t A, const fmpz * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_get_coeff_scalar_ui(gr_ptr c, const gr_mpoly_t A, const ulong * exp, gr_mpoly_ctx_t ctx) Sets *c* to the coefficient in *A* with exponents *exp*. -.. function:: int gr_mpoly_set_coeff_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_ui_fmpz(gr_mpoly_t A, ulong c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_si_fmpz(gr_mpoly_t A, slong c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_fmpz_fmpz(gr_mpoly_t A, const fmpz_t c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_fmpq_fmpz(gr_mpoly_t A, const fmpq_t c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_set_coeff_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_ui_fmpz(gr_mpoly_t A, ulong c, const fmpz * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_si_fmpz(gr_mpoly_t A, slong c, const fmpz * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_fmpz_fmpz(gr_mpoly_t A, const fmpz_t c, const fmpz * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_fmpq_fmpz(gr_mpoly_t A, const fmpq_t c, const fmpz * exp, gr_mpoly_ctx_t ctx) -.. function:: int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, gr_srcptr c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_ui_ui(gr_mpoly_t A, ulong c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_si_ui(gr_mpoly_t A, slong c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_fmpz_ui(gr_mpoly_t A, const fmpz_t c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_set_coeff_fmpq_ui(gr_mpoly_t A, const fmpq_t c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_set_coeff_scalar_ui(gr_mpoly_t poly, gr_srcptr c, const ulong * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_ui_ui(gr_mpoly_t A, ulong c, const ulong * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_si_ui(gr_mpoly_t A, slong c, const ulong * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_fmpz_ui(gr_mpoly_t A, const fmpz_t c, const ulong * exp, gr_mpoly_ctx_t ctx) + int gr_mpoly_set_coeff_fmpq_ui(gr_mpoly_t A, const fmpq_t c, const ulong * exp, gr_mpoly_ctx_t ctx) Sets the coefficient with exponents *exp* in *A* to the scalar *c* which must be an element of or coercible to the coefficient ring. @@ -125,30 +234,30 @@ Coefficient and exponent access Arithmetic ------------------------------------------------------------------------------- -.. function:: int gr_mpoly_neg(gr_mpoly_t A, const gr_mpoly_t B, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_neg(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx) Sets *A* to the negation of *B*. -.. function:: int gr_mpoly_add(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_add(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) Sets *A* to the difference of *B* and *C*. -.. function:: int gr_mpoly_sub(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_sub(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) Sets *A* to the difference of *B* and *C*. -.. function:: int gr_mpoly_mul(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_johnson(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_monomial(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_mul(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_johnson(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_monomial(gr_mpoly_t A, const gr_mpoly_t B, const gr_mpoly_t C, gr_mpoly_ctx_t ctx) Sets *A* to the product of *B* and *C*. The *monomial* version assumes that *C* is a monomial. -.. function:: int gr_mpoly_mul_scalar(gr_mpoly_t A, const gr_mpoly_t B, gr_srcptr c, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_si(gr_mpoly_t A, const gr_mpoly_t B, slong c, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx) - int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_mul_scalar(gr_mpoly_t A, const gr_mpoly_t B, gr_srcptr c, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_si(gr_mpoly_t A, const gr_mpoly_t B, slong c, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, gr_mpoly_ctx_t ctx) + int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, gr_mpoly_ctx_t ctx) Sets *A* to *B* multiplied by the scalar *c* which must be an element of or coercible to the coefficient ring. @@ -158,35 +267,35 @@ Container operations Mostly intended for internal use. -.. function:: void _gr_mpoly_fit_length(gr_ptr * coeffs, slong * coeffs_alloc, ulong ** exps, slong * exps_alloc, slong N, slong length, gr_ctx_t cctx) +.. function:: void _gr_mpoly_fit_length(gr_ptr * coeffs, slong * coeffs_alloc, ulong ** exps, slong * exps_alloc, slong N, slong length, gr_mpoly_ctx_t ctx) -.. function:: void gr_mpoly_fit_length(gr_mpoly_t A, slong len, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_fit_length(gr_mpoly_t A, slong len, gr_mpoly_ctx_t ctx) Ensures that *A* has space for *len* coefficients and exponents. -.. function:: void gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx) -.. function:: void gr_mpoly_fit_length_fit_bits(gr_mpoly_t A, slong len, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_fit_length_fit_bits(gr_mpoly_t A, slong len, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx) -.. function:: void gr_mpoly_fit_length_reset_bits(gr_mpoly_t A, slong len, flint_bitcnt_t bits, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_fit_length_reset_bits(gr_mpoly_t A, slong len, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx) -.. function:: void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, gr_mpoly_ctx_t ctx) -.. function:: void _gr_mpoly_push_exp_ui(gr_mpoly_t A, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void _gr_mpoly_push_exp_ui(gr_mpoly_t A, const ulong * exp, gr_mpoly_ctx_t ctx) -.. function:: int gr_mpoly_push_term_scalar_ui(gr_mpoly_t A, gr_srcptr c, const ulong * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_push_term_scalar_ui(gr_mpoly_t A, gr_srcptr c, const ulong * exp, gr_mpoly_ctx_t ctx) -.. function:: void _gr_mpoly_push_exp_fmpz(gr_mpoly_t A, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void _gr_mpoly_push_exp_fmpz(gr_mpoly_t A, const fmpz * exp, gr_mpoly_ctx_t ctx) -.. function:: int gr_mpoly_push_term_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_push_term_scalar_fmpz(gr_mpoly_t A, gr_srcptr c, const fmpz * exp, gr_mpoly_ctx_t ctx) -.. function:: void gr_mpoly_sort_terms(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_sort_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx) -.. function:: int gr_mpoly_combine_like_terms(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: int gr_mpoly_combine_like_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx) -.. function:: truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) -.. function:: void gr_mpoly_assert_canonical(const gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx) +.. function:: void gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) diff --git a/src/gr/mpoly.c b/src/gr/mpoly.c index 6571606ac0..188dc3dabd 100644 --- a/src/gr/mpoly.c +++ b/src/gr/mpoly.c @@ -9,232 +9,12 @@ (at your option) any later version. See . */ -/* Multivariate polynomials over generic rings */ - -#include #include "mpoly.h" #include "gr.h" #include "gr_mpoly.h" -#include "gr_generic.h" - - - - -int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx) -{ - gr_stream_write(out, "Ring of multivariate polynomials over "); - gr_ctx_write(out, GR_MPOLY_CCTX(ctx)); - gr_stream_write(out, " in "); - gr_stream_write_si(out, GR_MPOLY_NVARS(ctx)); - gr_stream_write(out, " variables"); - if (GR_MPOLY_MCTX(ctx)->ord == ORD_LEX) - gr_stream_write(out, ", lex order"); - else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGLEX) - gr_stream_write(out, ", deglex order"); - else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGREVLEX) - gr_stream_write(out, ", degrevlex order"); - return GR_SUCCESS; -} - -void -gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx) -{ - if (GR_MPOLY_VARS(ctx) != NULL) - { - slong i; - for (i = 0; i < GR_MPOLY_NVARS(ctx); i++) - flint_free(GR_MPOLY_VARS(ctx)[i]); - flint_free(GR_MPOLY_VARS(ctx)); - } - - mpoly_ctx_clear(GR_MPOLY_MCTX(ctx)); - flint_free(GR_MPOLY_MCTX(ctx)); -} - -int -gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s) -{ - slong i, nvars, len; - - nvars = GR_MPOLY_NVARS(ctx); - - if (GR_MPOLY_VARS(ctx) == NULL) - { - GR_MPOLY_VARS(ctx) = flint_malloc(nvars * sizeof(char *)); - for (i = 0; i < nvars; i++) - GR_MPOLY_VARS(ctx)[i] = NULL; - } - - for (i = 0; i < nvars; i++) - { - len = strlen(s[i]); - GR_MPOLY_VARS(ctx)[i] = flint_realloc(GR_MPOLY_VARS(ctx)[i], len + 1); - memcpy(GR_MPOLY_VARS(ctx)[i], s[i], len + 1); - } - - return GR_SUCCESS; -} - -truth_t -gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx) -{ - return gr_ctx_is_ring(GR_MPOLY_CCTX(ctx)); -} - -truth_t -gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx) -{ - return gr_ctx_is_zero_ring(GR_MPOLY_CCTX(ctx)); -} - -truth_t -gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx) -{ - return gr_ctx_is_commutative_ring(GR_MPOLY_CCTX(ctx)); -} - -truth_t -gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx) -{ - return gr_ctx_is_integral_domain(GR_MPOLY_CCTX(ctx)); -} - -truth_t -gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx) -{ - if (GR_MPOLY_NVARS(ctx) == 0) - return gr_ctx_is_field(GR_MPOLY_CCTX(ctx)); - else - return T_FALSE; -} - -truth_t -gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx) -{ - return gr_ctx_is_threadsafe(GR_MPOLY_CCTX(ctx)); -} - -int -gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_write_pretty(out, poly, ctx); -} - -int -gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) -{ - slong i, n; - int status = GR_SUCCESS; - - n = GR_MPOLY_NVARS(ctx); - - gr_vec_set_length(res, n, ctx); - for (i = 0; i < n; i++) - status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, ctx); - - return status; -} - -int -gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) -{ - int status; - gr_vec_t vec1; - slong i, n, m; - - /* Get generators of the element ring */ - gr_vec_init(vec1, 0, GR_MPOLY_CCTX(ctx)); - status = gr_gens_recursive(vec1, GR_MPOLY_CCTX(ctx)); - n = vec1->length; - - m = GR_MPOLY_NVARS(ctx); - - gr_vec_set_length(vec, n + m, ctx); - - /* Promote to polynomials */ - for (i = 0; i < n; i++) - status |= gr_mpoly_set_scalar(gr_vec_entry_ptr(vec, i, ctx), - gr_vec_entry_srcptr(vec1, i, GR_MPOLY_CCTX(ctx)), ctx); - - for (i = 0; i < m; i++) - status |= gr_mpoly_gen(((gr_mpoly_struct *) vec->entries) + n + i, i, ctx); - gr_vec_clear(vec1, GR_MPOLY_CCTX(ctx)); - - return status; -} - - -int _gr_mpoly_methods_initialized = 0; - -gr_static_method_table _gr_mpoly_methods; - -gr_method_tab_input _gr_mpoly_methods_input[] = -{ - {GR_METHOD_CTX_WRITE, (gr_funcptr) gr_mpoly_ctx_write}, - {GR_METHOD_CTX_CLEAR, (gr_funcptr) gr_mpoly_ctx_clear}, - {GR_METHOD_CTX_IS_RING, (gr_funcptr) gr_mpoly_ctx_is_ring}, - {GR_METHOD_CTX_IS_ZERO_RING, (gr_funcptr) gr_mpoly_ctx_is_zero_ring}, - {GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) gr_mpoly_ctx_is_commutative_ring}, - {GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) gr_mpoly_ctx_is_integral_domain}, - {GR_METHOD_CTX_IS_FIELD, (gr_funcptr) gr_mpoly_ctx_is_field}, - {GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) gr_mpoly_ctx_is_threadsafe}, - {GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) gr_mpoly_ctx_set_gen_names}, - {GR_METHOD_INIT, (gr_funcptr) gr_mpoly_init}, - {GR_METHOD_CLEAR, (gr_funcptr) gr_mpoly_clear}, - {GR_METHOD_SWAP, (gr_funcptr) gr_mpoly_swap}, - {GR_METHOD_SET_SHALLOW, (gr_funcptr) gr_mpoly_set_shallow}, - {GR_METHOD_RANDTEST, (gr_funcptr) gr_mpoly_randtest}, - {_GR_METHOD_LENGTH, (gr_funcptr) gr_mpoly_length}, - {GR_METHOD_WRITE, (gr_funcptr) gr_mpoly_write}, - {GR_METHOD_GENS, (gr_funcptr) gr_mpoly_gens}, - {GR_METHOD_GENS_RECURSIVE, (gr_funcptr) gr_mpoly_gens_recursive}, - {GR_METHOD_ZERO, (gr_funcptr) gr_mpoly_zero}, - {GR_METHOD_ONE, (gr_funcptr) gr_mpoly_one}, - {GR_METHOD_IS_ZERO, (gr_funcptr) gr_mpoly_is_zero}, - {GR_METHOD_IS_ONE, (gr_funcptr) gr_mpoly_is_one}, - {GR_METHOD_EQUAL, (gr_funcptr) gr_mpoly_equal}, - {GR_METHOD_SET, (gr_funcptr) gr_mpoly_set}, - {GR_METHOD_SET_UI, (gr_funcptr) gr_mpoly_set_ui}, - {GR_METHOD_SET_SI, (gr_funcptr) gr_mpoly_set_si}, - {GR_METHOD_SET_FMPZ, (gr_funcptr) gr_mpoly_set_fmpz}, - {GR_METHOD_SET_FMPQ, (gr_funcptr) gr_mpoly_set_fmpq}, - {GR_METHOD_SET_STR, (gr_funcptr) gr_generic_set_str_balance_additions}, - {GR_METHOD_NEG, (gr_funcptr) gr_mpoly_neg}, - {GR_METHOD_ADD, (gr_funcptr) gr_mpoly_add}, - {GR_METHOD_SUB, (gr_funcptr) gr_mpoly_sub}, - {GR_METHOD_MUL, (gr_funcptr) gr_mpoly_mul}, - {0, (gr_funcptr) NULL}, -}; - -/* todo: first arg as gr_mpoly_ctx_t */ void gr_ctx_init_gr_mpoly(gr_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord) { - ctx->which_ring = GR_CTX_GR_MPOLY; - ctx->sizeof_elem = sizeof(gr_mpoly_struct); - ctx->size_limit = WORD_MAX; - - /* by reference */ - GR_MPOLY_CCTX(ctx) = base_ring; - - /* allocated here */ - GR_MPOLY_MCTX(ctx) = flint_malloc(sizeof(mpoly_ctx_struct)); - mpoly_ctx_init(GR_MPOLY_MCTX(ctx), nvars, ord); - - GR_MPOLY_VARS(ctx) = NULL; - - ctx->methods = _gr_mpoly_methods; - - if (!_gr_mpoly_methods_initialized) - { - gr_method_tab_init(_gr_mpoly_methods, _gr_mpoly_methods_input); - _gr_mpoly_methods_initialized = 1; - } -} - -void -gr_mpoly_ctx_init_rand(gr_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars) -{ - gr_ctx_init_gr_mpoly(ctx, base_ring, n_randint(state, max_nvars + 1), mpoly_ordering_randtest(state)); + gr_mpoly_ctx_init(ctx, base_ring, nvars, ord); } diff --git a/src/gr_mpoly.h b/src/gr_mpoly.h index 8cfaa1c34a..28340e0909 100644 --- a/src/gr_mpoly.h +++ b/src/gr_mpoly.h @@ -64,6 +64,24 @@ typedef gr_mpoly_ctx_struct gr_mpoly_ctx_t[1]; void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars); void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx); +void gr_mpoly_ctx_init(gr_mpoly_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord); +void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars); + +WARN_UNUSED_RESULT int gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s); +WARN_UNUSED_RESULT int gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx); +WARN_UNUSED_RESULT int gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx); + +int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx); +void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx); + +truth_t gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx); +truth_t gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx); + + /* Memory management */ GR_MPOLY_INLINE @@ -125,6 +143,12 @@ void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, gr_mpoly_ctx_t ctx) A->length = newlen; } +GR_MPOLY_INLINE slong +gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) +{ + return x->length; +} + /* Basic manipulation */ GR_MPOLY_INLINE @@ -133,6 +157,12 @@ void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t ctx) FLINT_SWAP(gr_mpoly_struct, *A, *B); } +GR_MPOLY_INLINE void +gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + *res = *poly; +} + WARN_UNUSED_RESULT int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx); GR_MPOLY_INLINE WARN_UNUSED_RESULT @@ -175,10 +205,16 @@ void gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx); int gr_mpoly_randtest_bits(gr_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t exp_bits, gr_mpoly_ctx_t ctx); +GR_MPOLY_INLINE WARN_UNUSED_RESULT int +_gr_mpoly_randtest_default(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), ctx); +} + /* Input and output */ -/* todo: vars stored in context object */ int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, gr_mpoly_ctx_t ctx); +int gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx); int gr_mpoly_print_pretty(const gr_mpoly_t A, gr_mpoly_ctx_t ctx); /* Constants */ @@ -241,26 +277,6 @@ WARN_UNUSED_RESULT int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c WARN_UNUSED_RESULT int gr_mpoly_mul_fmpz(gr_mpoly_t A, const gr_mpoly_t B, const fmpz_t c, gr_mpoly_ctx_t ctx); WARN_UNUSED_RESULT int gr_mpoly_mul_fmpq(gr_mpoly_t A, const gr_mpoly_t B, const fmpq_t c, gr_mpoly_ctx_t ctx); -/* Todo */ - -GR_MPOLY_INLINE WARN_UNUSED_RESULT int -gr_mpoly_randtest(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx) -{ - return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), ctx); -} - -GR_MPOLY_INLINE void -gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx) -{ - *res = *poly; -} - -GR_MPOLY_INLINE slong -gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx) -{ - return x->length; -} - #ifdef __cplusplus } #endif diff --git a/src/gr_mpoly/ctx.c b/src/gr_mpoly/ctx.c new file mode 100644 index 0000000000..d8c629bbca --- /dev/null +++ b/src/gr_mpoly/ctx.c @@ -0,0 +1,229 @@ +/* + Copyright (C) 2022, 2025 Fredrik Johansson + + This file is part of FLINT. + + FLINT is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License (LGPL) as published + by the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. See . +*/ + +#include +#include "mpoly.h" +#include "gr.h" +#include "gr_generic.h" +#include "gr_mpoly.h" + +int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx) +{ + gr_stream_write(out, "Ring of multivariate polynomials over "); + gr_ctx_write(out, GR_MPOLY_CCTX(ctx)); + gr_stream_write(out, " in "); + gr_stream_write_si(out, GR_MPOLY_NVARS(ctx)); + gr_stream_write(out, " variables"); + if (GR_MPOLY_MCTX(ctx)->ord == ORD_LEX) + gr_stream_write(out, ", lex order"); + else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGLEX) + gr_stream_write(out, ", deglex order"); + else if (GR_MPOLY_MCTX(ctx)->ord == ORD_DEGREVLEX) + gr_stream_write(out, ", degrevlex order"); + return GR_SUCCESS; +} + +void +gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx) +{ + if (GR_MPOLY_VARS(ctx) != NULL) + { + slong i; + for (i = 0; i < GR_MPOLY_NVARS(ctx); i++) + flint_free(GR_MPOLY_VARS(ctx)[i]); + flint_free(GR_MPOLY_VARS(ctx)); + } + + mpoly_ctx_clear(GR_MPOLY_MCTX(ctx)); + flint_free(GR_MPOLY_MCTX(ctx)); +} + +int +gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s) +{ + slong i, nvars, len; + + nvars = GR_MPOLY_NVARS(ctx); + + if (GR_MPOLY_VARS(ctx) == NULL) + { + GR_MPOLY_VARS(ctx) = flint_malloc(nvars * sizeof(char *)); + for (i = 0; i < nvars; i++) + GR_MPOLY_VARS(ctx)[i] = NULL; + } + + for (i = 0; i < nvars; i++) + { + len = strlen(s[i]); + GR_MPOLY_VARS(ctx)[i] = flint_realloc(GR_MPOLY_VARS(ctx)[i], len + 1); + memcpy(GR_MPOLY_VARS(ctx)[i], s[i], len + 1); + } + + return GR_SUCCESS; +} + +truth_t +gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx) +{ + return gr_ctx_is_ring(GR_MPOLY_CCTX(ctx)); +} + +truth_t +gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx) +{ + return gr_ctx_is_zero_ring(GR_MPOLY_CCTX(ctx)); +} + +truth_t +gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx) +{ + return gr_ctx_is_commutative_ring(GR_MPOLY_CCTX(ctx)); +} + +truth_t +gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx) +{ + return gr_ctx_is_integral_domain(GR_MPOLY_CCTX(ctx)); +} + +truth_t +gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx) +{ + if (GR_MPOLY_NVARS(ctx) == 0) + return gr_ctx_is_field(GR_MPOLY_CCTX(ctx)); + else + return T_FALSE; +} + +truth_t +gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx) +{ + return gr_ctx_is_threadsafe(GR_MPOLY_CCTX(ctx)); +} + +int +gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx) +{ + slong i, n; + int status = GR_SUCCESS; + + n = GR_MPOLY_NVARS(ctx); + + gr_vec_set_length(res, n, ctx); + for (i = 0; i < n; i++) + status |= gr_mpoly_gen(((gr_mpoly_struct *) res->entries) + i, i, ctx); + + return status; +} + +int +gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx) +{ + int status; + gr_vec_t vec1; + slong i, n, m; + + /* Get generators of the element ring */ + gr_vec_init(vec1, 0, GR_MPOLY_CCTX(ctx)); + status = gr_gens_recursive(vec1, GR_MPOLY_CCTX(ctx)); + n = vec1->length; + + m = GR_MPOLY_NVARS(ctx); + + gr_vec_set_length(vec, n + m, ctx); + + /* Promote to polynomials */ + for (i = 0; i < n; i++) + status |= gr_mpoly_set_scalar(gr_vec_entry_ptr(vec, i, ctx), + gr_vec_entry_srcptr(vec1, i, GR_MPOLY_CCTX(ctx)), ctx); + + for (i = 0; i < m; i++) + status |= gr_mpoly_gen(((gr_mpoly_struct *) vec->entries) + n + i, i, ctx); + + gr_vec_clear(vec1, GR_MPOLY_CCTX(ctx)); + + return status; +} + + +int _gr_mpoly_methods_initialized = 0; + +gr_static_method_table _gr_mpoly_methods; + +gr_method_tab_input _gr_mpoly_methods_input[] = +{ + {GR_METHOD_CTX_WRITE, (gr_funcptr) gr_mpoly_ctx_write}, + {GR_METHOD_CTX_CLEAR, (gr_funcptr) gr_mpoly_ctx_clear}, + {GR_METHOD_CTX_IS_RING, (gr_funcptr) gr_mpoly_ctx_is_ring}, + {GR_METHOD_CTX_IS_ZERO_RING, (gr_funcptr) gr_mpoly_ctx_is_zero_ring}, + {GR_METHOD_CTX_IS_COMMUTATIVE_RING, (gr_funcptr) gr_mpoly_ctx_is_commutative_ring}, + {GR_METHOD_CTX_IS_INTEGRAL_DOMAIN, (gr_funcptr) gr_mpoly_ctx_is_integral_domain}, + {GR_METHOD_CTX_IS_FIELD, (gr_funcptr) gr_mpoly_ctx_is_field}, + {GR_METHOD_CTX_IS_THREADSAFE, (gr_funcptr) gr_mpoly_ctx_is_threadsafe}, + {GR_METHOD_CTX_SET_GEN_NAMES, (gr_funcptr) gr_mpoly_ctx_set_gen_names}, + {GR_METHOD_INIT, (gr_funcptr) gr_mpoly_init}, + {GR_METHOD_CLEAR, (gr_funcptr) gr_mpoly_clear}, + {GR_METHOD_SWAP, (gr_funcptr) gr_mpoly_swap}, + {GR_METHOD_SET_SHALLOW, (gr_funcptr) gr_mpoly_set_shallow}, + {GR_METHOD_RANDTEST, (gr_funcptr) _gr_mpoly_randtest_default}, + {_GR_METHOD_LENGTH, (gr_funcptr) gr_mpoly_length}, + {GR_METHOD_WRITE, (gr_funcptr) gr_mpoly_write}, + {GR_METHOD_GENS, (gr_funcptr) gr_mpoly_gens}, + {GR_METHOD_GENS_RECURSIVE, (gr_funcptr) gr_mpoly_gens_recursive}, + {GR_METHOD_ZERO, (gr_funcptr) gr_mpoly_zero}, + {GR_METHOD_ONE, (gr_funcptr) gr_mpoly_one}, + {GR_METHOD_IS_ZERO, (gr_funcptr) gr_mpoly_is_zero}, + {GR_METHOD_IS_ONE, (gr_funcptr) gr_mpoly_is_one}, + {GR_METHOD_EQUAL, (gr_funcptr) gr_mpoly_equal}, + {GR_METHOD_SET, (gr_funcptr) gr_mpoly_set}, + {GR_METHOD_SET_UI, (gr_funcptr) gr_mpoly_set_ui}, + {GR_METHOD_SET_SI, (gr_funcptr) gr_mpoly_set_si}, + {GR_METHOD_SET_FMPZ, (gr_funcptr) gr_mpoly_set_fmpz}, + {GR_METHOD_SET_FMPQ, (gr_funcptr) gr_mpoly_set_fmpq}, + {GR_METHOD_SET_STR, (gr_funcptr) gr_generic_set_str_balance_additions}, + {GR_METHOD_NEG, (gr_funcptr) gr_mpoly_neg}, + {GR_METHOD_ADD, (gr_funcptr) gr_mpoly_add}, + {GR_METHOD_SUB, (gr_funcptr) gr_mpoly_sub}, + {GR_METHOD_MUL, (gr_funcptr) gr_mpoly_mul}, + {0, (gr_funcptr) NULL}, +}; + +/* todo: first arg as gr_mpoly_ctx_t */ +void +gr_mpoly_ctx_init(gr_mpoly_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord) +{ + ctx->which_ring = GR_CTX_GR_MPOLY; + ctx->sizeof_elem = sizeof(gr_mpoly_struct); + ctx->size_limit = WORD_MAX; + + /* by reference */ + GR_MPOLY_CCTX(ctx) = base_ring; + + /* allocated here */ + GR_MPOLY_MCTX(ctx) = flint_malloc(sizeof(mpoly_ctx_struct)); + mpoly_ctx_init(GR_MPOLY_MCTX(ctx), nvars, ord); + + GR_MPOLY_VARS(ctx) = NULL; + + ctx->methods = _gr_mpoly_methods; + + if (!_gr_mpoly_methods_initialized) + { + gr_method_tab_init(_gr_mpoly_methods, _gr_mpoly_methods_input); + _gr_mpoly_methods_initialized = 1; + } +} + +void +gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars) +{ + gr_ctx_init_gr_mpoly(ctx, base_ring, n_randint(state, max_nvars + 1), mpoly_ordering_randtest(state)); +} diff --git a/src/gr_mpoly/write.c b/src/gr_mpoly/write.c index adf8330369..2067ddf09d 100644 --- a/src/gr_mpoly/write.c +++ b/src/gr_mpoly/write.c @@ -194,3 +194,9 @@ int gr_mpoly_print_pretty(const gr_mpoly_t A, gr_mpoly_ctx_t ctx) gr_stream_init_file(out, stdout); return gr_mpoly_write_pretty(out, A, ctx); } + +int +gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx) +{ + return gr_mpoly_write_pretty(out, poly, ctx); +}