File indexing completed on 2026-11-07 10:15:24
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef GR_MPOLY_H
0015 #define GR_MPOLY_H
0016
0017 #ifdef GR_MPOLY_INLINES_C
0018 #define GR_MPOLY_INLINE
0019 #else
0020 #define GR_MPOLY_INLINE static inline
0021 #endif
0022
0023 #include "mpoly_types.h"
0024 #include "gr_vec.h"
0025
0026 #if FLINT_WANT_ASSERT
0027 # include "mpoly.h"
0028 #endif
0029
0030 #ifdef __cplusplus
0031 extern "C" {
0032 #endif
0033
0034 typedef struct
0035 {
0036 gr_ptr coeffs;
0037 ulong * exps;
0038 slong length;
0039 flint_bitcnt_t bits;
0040 slong coeffs_alloc;
0041 slong exps_alloc;
0042 }
0043 gr_mpoly_struct;
0044
0045 typedef gr_mpoly_struct gr_mpoly_t[1];
0046
0047 typedef struct
0048 {
0049 gr_ctx_struct * cctx;
0050 mpoly_ctx_struct * mctx;
0051 char ** vars;
0052 } _gr_mpoly_ctx_struct;
0053
0054 typedef gr_ctx_struct gr_mpoly_ctx_struct;
0055 typedef gr_mpoly_ctx_struct gr_mpoly_ctx_t[1];
0056
0057 #define GR_MPOLY_MCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->mctx)
0058 #define GR_MPOLY_CCTX(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->cctx)
0059 #define GR_MPOLY_VARS(ctx) (((_gr_mpoly_ctx_struct *) (ctx->data))->vars)
0060 #define GR_MPOLY_NVARS(ctx) (GR_MPOLY_MCTX(ctx)->nvars)
0061
0062
0063
0064 void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars);
0065 void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx);
0066
0067 void gr_mpoly_ctx_init(gr_mpoly_ctx_t ctx, gr_ctx_t base_ring, slong nvars, const ordering_t ord);
0068 void gr_mpoly_ctx_init_rand(gr_mpoly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring, slong max_nvars);
0069
0070 WARN_UNUSED_RESULT int gr_mpoly_ctx_set_gen_names(gr_mpoly_ctx_t ctx, const char ** s);
0071 WARN_UNUSED_RESULT int gr_mpoly_gens(gr_vec_t res, gr_mpoly_ctx_t ctx);
0072 WARN_UNUSED_RESULT int gr_mpoly_gens_recursive(gr_vec_t vec, gr_mpoly_ctx_t ctx);
0073
0074 int gr_mpoly_ctx_write(gr_stream_t out, gr_mpoly_ctx_t ctx);
0075 void gr_mpoly_ctx_clear(gr_mpoly_ctx_t ctx);
0076
0077 truth_t gr_mpoly_ctx_is_ring(gr_mpoly_ctx_t ctx);
0078 truth_t gr_mpoly_ctx_is_zero_ring(gr_mpoly_ctx_t ctx);
0079 truth_t gr_mpoly_ctx_is_commutative_ring(gr_mpoly_ctx_t ctx);
0080 truth_t gr_mpoly_ctx_is_integral_domain(gr_mpoly_ctx_t ctx);
0081 truth_t gr_mpoly_ctx_is_field(gr_mpoly_ctx_t ctx);
0082 truth_t gr_mpoly_ctx_is_threadsafe(gr_mpoly_ctx_t ctx);
0083
0084
0085
0086
0087 GR_MPOLY_INLINE
0088 void gr_mpoly_init(gr_mpoly_t A, gr_mpoly_ctx_t FLINT_UNUSED(ctx))
0089 {
0090 A->coeffs = NULL;
0091 A->exps = NULL;
0092 A->length = 0;
0093 A->bits = MPOLY_MIN_BITS;
0094 A->coeffs_alloc = 0;
0095 A->exps_alloc = 0;
0096 }
0097
0098 void gr_mpoly_init3(gr_mpoly_t A, slong alloc, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx);
0099 void gr_mpoly_init2( gr_mpoly_t A, slong alloc, gr_mpoly_ctx_t ctx);
0100
0101 GR_MPOLY_INLINE
0102 void gr_mpoly_clear(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
0103 {
0104 _gr_vec_clear(A->coeffs, A->coeffs_alloc, GR_MPOLY_CCTX(ctx));
0105
0106 if (A->coeffs_alloc > 0)
0107 flint_free(A->coeffs);
0108 if (A->exps_alloc > 0)
0109 flint_free(A->exps);
0110 }
0111
0112 void _gr_mpoly_fit_length(
0113 gr_ptr * coeffs,
0114 slong * coeffs_alloc,
0115 ulong ** exps,
0116 slong * exps_alloc,
0117 slong N,
0118 slong length,
0119 gr_ctx_t cctx);
0120
0121 void gr_mpoly_fit_length(gr_mpoly_t A, slong len, gr_mpoly_ctx_t ctx);
0122 void gr_mpoly_fit_bits(gr_mpoly_t A, flint_bitcnt_t bits, gr_mpoly_ctx_t ctx);
0123
0124 void gr_mpoly_fit_length_fit_bits(
0125 gr_mpoly_t A,
0126 slong len,
0127 flint_bitcnt_t bits,
0128 gr_mpoly_ctx_t ctx);
0129
0130 void gr_mpoly_fit_length_reset_bits(
0131 gr_mpoly_t A,
0132 slong len,
0133 flint_bitcnt_t bits,
0134 gr_mpoly_ctx_t ctx);
0135
0136
0137 GR_MPOLY_INLINE
0138 void _gr_mpoly_set_length(gr_mpoly_t A, slong newlen, gr_mpoly_ctx_t FLINT_UNUSED(ctx))
0139 {
0140 FLINT_ASSERT(newlen <= A->coeffs_alloc);
0141 FLINT_ASSERT(mpoly_words_per_exp(A->bits, GR_MPOLY_MCTX(ctx))*newlen <= A->exps_alloc);
0142
0143 A->length = newlen;
0144 }
0145
0146 GR_MPOLY_INLINE slong
0147 gr_mpoly_length(const gr_mpoly_t x, gr_mpoly_ctx_t FLINT_UNUSED(ctx))
0148 {
0149 return x->length;
0150 }
0151
0152
0153
0154 void _gr_mpoly_normalise(gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0155
0156 GR_MPOLY_INLINE
0157 void gr_mpoly_swap(gr_mpoly_t A, gr_mpoly_t B, gr_mpoly_ctx_t FLINT_UNUSED(ctx))
0158 {
0159 FLINT_SWAP(gr_mpoly_struct, *A, *B);
0160 }
0161
0162 GR_MPOLY_INLINE void
0163 gr_mpoly_set_shallow(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t FLINT_UNUSED(ctx))
0164 {
0165 *res = *poly;
0166 }
0167
0168 WARN_UNUSED_RESULT int gr_mpoly_set(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx);
0169
0170 GR_MPOLY_INLINE WARN_UNUSED_RESULT
0171 int gr_mpoly_zero(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
0172 {
0173 _gr_mpoly_set_length(A, 0, ctx);
0174 return GR_SUCCESS;
0175 }
0176
0177 truth_t gr_mpoly_is_zero(const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0178
0179 WARN_UNUSED_RESULT int gr_mpoly_gen(gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx);
0180 truth_t gr_mpoly_is_gen(const gr_mpoly_t A, slong var, gr_mpoly_ctx_t ctx);
0181
0182 truth_t gr_mpoly_equal(const gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx);
0183
0184
0185
0186 void _gr_mpoly_push_exp_ui(gr_mpoly_t A, const ulong * exp, gr_mpoly_ctx_t ctx);
0187 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);
0188
0189 void _gr_mpoly_push_exp_fmpz(gr_mpoly_t A, const fmpz * exp, gr_mpoly_ctx_t ctx);
0190 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);
0191
0192 void gr_mpoly_sort_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0193 WARN_UNUSED_RESULT int gr_mpoly_combine_like_terms(gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0194
0195 truth_t gr_mpoly_is_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0196 void gr_mpoly_assert_canonical(const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0197
0198
0199
0200 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);
0201 int gr_mpoly_randtest_bound(gr_mpoly_t A, flint_rand_t state, slong length, ulong exp_bound, gr_mpoly_ctx_t ctx);
0202
0203 GR_MPOLY_INLINE WARN_UNUSED_RESULT int
0204 _gr_mpoly_randtest_default(gr_mpoly_t res, flint_rand_t state, gr_mpoly_ctx_t ctx)
0205 {
0206 return gr_mpoly_randtest_bits(res, state, n_randint(state, 5), 1 + n_randint(state, 3), ctx);
0207 }
0208
0209
0210
0211 int gr_mpoly_write_pretty(gr_stream_t out, const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0212 int gr_mpoly_write(gr_stream_t out, gr_mpoly_t poly, gr_mpoly_ctx_t ctx);
0213 int gr_mpoly_print_pretty(const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0214
0215
0216
0217 WARN_UNUSED_RESULT int gr_mpoly_set_other(gr_mpoly_t res, gr_srcptr A, gr_ctx_t A_ctx, gr_mpoly_ctx_t ctx);
0218
0219
0220
0221 WARN_UNUSED_RESULT int gr_mpoly_set_scalar(gr_mpoly_t A, gr_srcptr c, gr_mpoly_ctx_t ctx);
0222 WARN_UNUSED_RESULT int gr_mpoly_set_ui(gr_mpoly_t A, ulong c, gr_mpoly_ctx_t ctx);
0223 WARN_UNUSED_RESULT int gr_mpoly_set_si(gr_mpoly_t A, slong c, gr_mpoly_ctx_t ctx);
0224 WARN_UNUSED_RESULT int gr_mpoly_set_fmpz(gr_mpoly_t A, const fmpz_t c, gr_mpoly_ctx_t ctx);
0225 WARN_UNUSED_RESULT int gr_mpoly_set_fmpq(gr_mpoly_t A, const fmpq_t c, gr_mpoly_ctx_t ctx);
0226
0227 GR_MPOLY_INLINE WARN_UNUSED_RESULT
0228 int gr_mpoly_one(gr_mpoly_t A, gr_mpoly_ctx_t ctx)
0229 {
0230 return gr_mpoly_set_ui(A, 1, ctx);
0231 }
0232
0233 truth_t gr_mpoly_is_one(const gr_mpoly_t A, gr_mpoly_ctx_t ctx);
0234
0235
0236
0237 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);
0238 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);
0239
0240 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);
0241 WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_fmpz(gr_mpoly_t A, ulong c, const fmpz * exp, gr_mpoly_ctx_t ctx);
0242 WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_fmpz(gr_mpoly_t A, slong c, const fmpz * exp, gr_mpoly_ctx_t ctx);
0243 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);
0244 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);
0245
0246 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);
0247 WARN_UNUSED_RESULT int gr_mpoly_set_coeff_ui_ui(gr_mpoly_t A, ulong c, const ulong * exp, gr_mpoly_ctx_t ctx);
0248 WARN_UNUSED_RESULT int gr_mpoly_set_coeff_si_ui(gr_mpoly_t A, slong c, const ulong * exp, gr_mpoly_ctx_t ctx);
0249 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);
0250 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);
0251
0252
0253
0254 WARN_UNUSED_RESULT int gr_mpoly_neg(gr_mpoly_t A, const gr_mpoly_t B, gr_mpoly_ctx_t ctx);
0255 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);
0256 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);
0257
0258 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);
0259 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);
0260 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);
0261
0262 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);
0263 WARN_UNUSED_RESULT int gr_mpoly_mul_si(gr_mpoly_t A, const gr_mpoly_t B, slong c, gr_mpoly_ctx_t ctx);
0264 WARN_UNUSED_RESULT int gr_mpoly_mul_ui(gr_mpoly_t A, const gr_mpoly_t B, ulong c, gr_mpoly_ctx_t ctx);
0265 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);
0266 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);
0267
0268 WARN_UNUSED_RESULT int gr_mpoly_inv(gr_mpoly_t res, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx);
0269 WARN_UNUSED_RESULT int gr_mpoly_canonical_associate(gr_mpoly_t res, gr_mpoly_t u, const gr_mpoly_t poly, gr_mpoly_ctx_t ctx);
0270
0271
0272
0273 WARN_UNUSED_RESULT int gr_mpoly_derivative(gr_mpoly_t A, const gr_mpoly_t B, slong var, gr_mpoly_ctx_t ctx);
0274
0275 WARN_UNUSED_RESULT int gr_mpoly_integral(gr_mpoly_t A, const gr_mpoly_t B, slong var, gr_mpoly_ctx_t ctx);
0276
0277 #ifdef __cplusplus
0278 }
0279 #endif
0280
0281 #endif