Skip to content

Commit 4799931

Browse files
Merge pull request #2439 from rburing/gr_mpoly_normalise
Add `_gr_mpoly_normalise` to remove zero coefficients
2 parents af05484 + 212fff6 commit 4799931

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

doc/source/gr_mpoly.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ Memory management
134134
Basic manipulation
135135
-------------------------------------------------------------------------------
136136

137+
.. function:: void _gr_mpoly_normalise(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
138+
139+
Removes provably zero coefficients from ``A`` and updates the length.
140+
If all coefficients are zero, the length is set to zero. This function
141+
is mainly used internally, as all functions guarantee normalisation.
142+
137143
.. function:: void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t ctx)
138144

139145
Swaps *A* and *B* efficiently.

src/gr_mpoly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t ctx)
151151

152152
/* Basic manipulation */
153153

154+
void _gr_mpoly_normalise(gr_mpoly_t A, gr_mpoly_ctx_t ctx);
155+
154156
GR_MPOLY_INLINE
155157
void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t ctx)
156158
{

src/gr_mpoly/normalise.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright (C) 2025 Ricardo Buring
3+
4+
This file is part of FLINT.
5+
6+
FLINT is free software: you can redistribute it and/or modify it under
7+
the terms of the GNU Lesser General Public License (LGPL) as published
8+
by the Free Software Foundation; either version 3 of the License, or
9+
(at your option) any later version. See <https://www.gnu.org/licenses/>.
10+
*/
11+
12+
#include "mpoly.h"
13+
#include "gr_mpoly.h"
14+
15+
/*
16+
remove zero coefficients
17+
*/
18+
void
19+
_gr_mpoly_normalise(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
20+
{
21+
mpoly_ctx_struct * mctx = GR_MPOLY_MCTX(ctx);
22+
gr_ctx_struct * cctx = GR_MPOLY_CCTX(ctx);
23+
gr_method_swap_op swap = GR_SWAP_OP(cctx, SWAP);
24+
gr_method_unary_predicate is_zero = GR_UNARY_PREDICATE(cctx, IS_ZERO);
25+
slong in, out, N;
26+
slong sz = cctx->sizeof_elem;
27+
28+
N = mpoly_words_per_exp(A->bits, mctx);
29+
30+
out = -WORD(1);
31+
32+
for (in = 0; in < A->length; in++)
33+
{
34+
FLINT_ASSERT(in > out);
35+
36+
if (out < 0 || is_zero(GR_ENTRY(A->coeffs, out, sz), cctx) != T_TRUE)
37+
out++;
38+
39+
if (out != in)
40+
{
41+
mpoly_monomial_set(A->exps + N*out, A->exps + N*in, N);
42+
swap(GR_ENTRY(A->coeffs, out, sz), GR_ENTRY(A->coeffs, in, sz), cctx);
43+
}
44+
}
45+
46+
if (out < 0 || is_zero(GR_ENTRY(A->coeffs, out, sz), cctx) != T_TRUE)
47+
out++;
48+
49+
A->length = out;
50+
}

src/gr_mpoly/set_other.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ _gr_mpoly_set_gr_mpoly_other(gr_mpoly_t res, const gr_mpoly_t A, gr_mpoly_ctx_t
9999
/* there may be zero coefficients; remove them
100100
(todo: do inline in first loop) */
101101
/* if (cctx != A_cctx) */
102-
status = gr_mpoly_combine_like_terms(res, ctx);
102+
_gr_mpoly_normalise(res, ctx);
103103
}
104104
else
105105
{
@@ -188,7 +188,7 @@ _gr_mpoly_set_gr_mpoly_other(gr_mpoly_t res, const gr_mpoly_t A, gr_mpoly_ctx_t
188188
/* term order may be different */
189189
gr_mpoly_sort_terms(res, ctx);
190190
/* todo: combine zero checks with main loop */
191-
status |= gr_mpoly_combine_like_terms(res, ctx);
191+
_gr_mpoly_normalise(res, ctx);
192192

193193
flint_free(A_exps);
194194
flint_free(exps);

0 commit comments

Comments
 (0)