|
| 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 | +#ifndef GR_ORE_POLY_H |
| 13 | +#define GR_ORE_POLY_H |
| 14 | + |
| 15 | +#ifdef GR_ORE_POLY_INLINES_C |
| 16 | +#define GR_ORE_POLY_INLINE |
| 17 | +#else |
| 18 | +#define GR_ORE_POLY_INLINE static inline |
| 19 | +#endif |
| 20 | + |
| 21 | +#include "ore_poly_types.h" |
| 22 | +#include "gr.h" |
| 23 | + |
| 24 | +#ifdef __cplusplus |
| 25 | +extern "C" { |
| 26 | +#endif |
| 27 | + |
| 28 | +/* Compatible with gr_poly_struct */ |
| 29 | +typedef struct |
| 30 | +{ |
| 31 | + gr_ptr coeffs; |
| 32 | + slong alloc; |
| 33 | + slong length; |
| 34 | +} |
| 35 | +gr_ore_poly_struct; |
| 36 | + |
| 37 | +typedef gr_ore_poly_struct gr_ore_poly_t[1]; |
| 38 | + |
| 39 | +/* Compatible with polynomial_ctx_t */ |
| 40 | +typedef struct |
| 41 | +{ |
| 42 | + gr_ctx_struct * base_ring; |
| 43 | + slong degree_limit; |
| 44 | + char * var; |
| 45 | + ore_operator_t operator; |
| 46 | + /* TODO: Function pointers to \sigma, \delta. */ |
| 47 | +} |
| 48 | +_gr_ore_poly_ctx_struct; |
| 49 | + |
| 50 | +typedef gr_ctx_struct gr_ore_poly_ctx_struct; |
| 51 | + |
| 52 | +typedef gr_ore_poly_ctx_struct gr_ore_poly_ctx_t[1]; |
| 53 | + |
| 54 | +#define GR_ORE_POLY_CTX(ring_ctx) ((_gr_ore_poly_ctx_struct *)((ring_ctx))) |
| 55 | +#define GR_ORE_POLY_ELEM_CTX(ring_ctx) (GR_ORE_POLY_CTX(ring_ctx)->base_ring) |
| 56 | + |
| 57 | +/* Context object */ |
| 58 | + |
| 59 | +void gr_ore_poly_ctx_init(gr_ore_poly_ctx_t ctx, gr_ctx_t base_ring, const ore_operator_t operator); |
| 60 | +void gr_ore_poly_ctx_clear(gr_ore_poly_ctx_t ctx); |
| 61 | + |
| 62 | +void gr_ore_poly_ctx_init_rand(gr_ore_poly_ctx_t ctx, flint_rand_t state, gr_ctx_t base_ring); |
| 63 | + |
| 64 | +WARN_UNUSED_RESULT int _gr_ore_poly_ctx_set_gen_name(gr_ctx_t ctx, const char * s); |
| 65 | +WARN_UNUSED_RESULT int _gr_ore_poly_ctx_set_gen_names(gr_ctx_t ctx, const char ** s); |
| 66 | +WARN_UNUSED_RESULT int gr_ore_poly_gens_recursive(gr_vec_t vec, gr_ore_poly_ctx_t ctx); |
| 67 | + |
| 68 | +int gr_ore_poly_ctx_write(gr_stream_t out, gr_ore_poly_ctx_t ctx); |
| 69 | + |
| 70 | +truth_t gr_ore_poly_ctx_is_ring(gr_ore_poly_ctx_t ctx); |
| 71 | +truth_t gr_ore_poly_ctx_is_zero_ring(gr_ore_poly_ctx_t ctx); |
| 72 | +truth_t gr_ore_poly_ctx_is_commutative_ring(gr_ore_poly_ctx_t ctx); |
| 73 | +truth_t gr_ore_poly_ctx_is_integral_domain(gr_ore_poly_ctx_t ctx); |
| 74 | +truth_t gr_ore_poly_ctx_is_threadsafe(gr_ore_poly_ctx_t ctx); |
| 75 | + |
| 76 | +/* Memory management */ |
| 77 | + |
| 78 | +void gr_ore_poly_init(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 79 | +void gr_ore_poly_init2(gr_ore_poly_t poly, slong len, gr_ore_poly_ctx_t ctx); |
| 80 | +void gr_ore_poly_clear(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 81 | + |
| 82 | +GR_ORE_POLY_INLINE gr_ptr |
| 83 | +gr_ore_poly_entry_ptr(gr_ore_poly_t poly, slong i, gr_ore_poly_ctx_t ctx) |
| 84 | +{ |
| 85 | + return GR_ENTRY(poly->coeffs, i, GR_ORE_POLY_ELEM_CTX(ctx)->sizeof_elem); |
| 86 | +} |
| 87 | + |
| 88 | +GR_ORE_POLY_INLINE gr_srcptr |
| 89 | +gr_ore_poly_entry_srcptr(const gr_ore_poly_t poly, slong i, gr_ore_poly_ctx_t ctx) |
| 90 | +{ |
| 91 | + return GR_ENTRY(poly->coeffs, i, GR_ORE_POLY_ELEM_CTX(ctx)->sizeof_elem); |
| 92 | +} |
| 93 | + |
| 94 | +GR_ORE_POLY_INLINE slong gr_ore_poly_length(const gr_ore_poly_t poly, gr_ore_poly_ctx_t FLINT_UNUSED(ctx)) |
| 95 | +{ |
| 96 | + return poly->length; |
| 97 | +} |
| 98 | + |
| 99 | +GR_ORE_POLY_INLINE void |
| 100 | +gr_ore_poly_swap(gr_ore_poly_t poly1, gr_ore_poly_t poly2, gr_ore_poly_ctx_t FLINT_UNUSED(ctx)) |
| 101 | +{ |
| 102 | + FLINT_SWAP(gr_ore_poly_struct, *poly1, *poly2); |
| 103 | +} |
| 104 | + |
| 105 | +GR_ORE_POLY_INLINE void |
| 106 | +gr_ore_poly_set_shallow(gr_ore_poly_t res, const gr_ore_poly_t x, const gr_ore_poly_ctx_t ctx) |
| 107 | +{ |
| 108 | + *res = *x; |
| 109 | +} |
| 110 | + |
| 111 | +void gr_ore_poly_fit_length(gr_ore_poly_t poly, slong len, gr_ore_poly_ctx_t ctx); |
| 112 | +void _gr_ore_poly_set_length(gr_ore_poly_t poly, slong len, gr_ore_poly_ctx_t ctx); |
| 113 | +void _gr_ore_poly_normalise(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 114 | + |
| 115 | +/* Basic manipulation */ |
| 116 | + |
| 117 | +WARN_UNUSED_RESULT int gr_ore_poly_set(gr_ore_poly_t res, const gr_ore_poly_t src, gr_ore_poly_ctx_t ctx); |
| 118 | + |
| 119 | +WARN_UNUSED_RESULT int gr_ore_poly_truncate(gr_ore_poly_t poly, const gr_ore_poly_t src, slong newlen, gr_ore_poly_ctx_t ctx); |
| 120 | + |
| 121 | +GR_ORE_POLY_INLINE WARN_UNUSED_RESULT int |
| 122 | +gr_ore_poly_zero(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx) |
| 123 | +{ |
| 124 | + _gr_ore_poly_set_length(poly, 0, ctx); |
| 125 | + return GR_SUCCESS; |
| 126 | +} |
| 127 | + |
| 128 | +WARN_UNUSED_RESULT int gr_ore_poly_one(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 129 | +WARN_UNUSED_RESULT int gr_ore_poly_neg_one(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 130 | +WARN_UNUSED_RESULT int gr_ore_poly_gen(gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 131 | + |
| 132 | +truth_t _gr_ore_poly_equal(gr_srcptr poly1, slong len1, gr_srcptr poly2, slong len2, gr_ore_poly_ctx_t ctx); |
| 133 | +truth_t gr_ore_poly_equal(const gr_ore_poly_t poly1, const gr_ore_poly_t poly2, gr_ore_poly_ctx_t ctx); |
| 134 | + |
| 135 | +truth_t gr_ore_poly_is_zero(const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 136 | +truth_t gr_ore_poly_is_one(const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 137 | +truth_t gr_ore_poly_is_gen(const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 138 | +truth_t gr_ore_poly_is_scalar(const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 139 | + |
| 140 | +/* Input and output */ |
| 141 | + |
| 142 | +int _gr_ore_poly_write(gr_stream_t out, gr_srcptr poly, slong len, const char * x, gr_ore_poly_ctx_t ctx); |
| 143 | +int gr_ore_poly_write(gr_stream_t out, const gr_ore_poly_t poly, const char * x, gr_ore_poly_ctx_t ctx); |
| 144 | +int _gr_ore_poly_get_str(char ** res, gr_srcptr f, slong len, const char * x, gr_ore_poly_ctx_t ctx); |
| 145 | +int gr_ore_poly_get_str(char ** res, const gr_ore_poly_t poly, const char * x, gr_ore_poly_ctx_t ctx); |
| 146 | +int _gr_ore_poly_set_str(gr_ptr res, const char * s, const char * x, slong len, gr_ore_poly_ctx_t ctx); |
| 147 | +int gr_ore_poly_set_str(gr_ore_poly_t res, const char * s, const char * x, gr_ore_poly_ctx_t ctx); |
| 148 | +int gr_ore_poly_print(const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 149 | + |
| 150 | +/* Random generation */ |
| 151 | + |
| 152 | +int gr_ore_poly_randtest(gr_ore_poly_t poly, flint_rand_t state, slong len, gr_ore_poly_ctx_t ctx); |
| 153 | + |
| 154 | +GR_ORE_POLY_INLINE WARN_UNUSED_RESULT int |
| 155 | +_gr_ore_poly_randtest_default(gr_ore_poly_t res, flint_rand_t state, gr_ore_poly_ctx_t ctx) |
| 156 | +{ |
| 157 | + return gr_ore_poly_randtest(res, state, n_randint(state, 5), ctx); |
| 158 | +} |
| 159 | + |
| 160 | +/* Constants */ |
| 161 | + |
| 162 | +WARN_UNUSED_RESULT int gr_ore_poly_set_scalar(gr_ore_poly_t poly, gr_srcptr x, gr_ore_poly_ctx_t ctx); |
| 163 | +WARN_UNUSED_RESULT int gr_ore_poly_set_si(gr_ore_poly_t poly, slong x, gr_ore_poly_ctx_t ctx); |
| 164 | +WARN_UNUSED_RESULT int gr_ore_poly_set_ui(gr_ore_poly_t poly, ulong x, gr_ore_poly_ctx_t ctx); |
| 165 | +WARN_UNUSED_RESULT int gr_ore_poly_set_fmpz(gr_ore_poly_t poly, const fmpz_t x, gr_ore_poly_ctx_t ctx); |
| 166 | +WARN_UNUSED_RESULT int gr_ore_poly_set_fmpq(gr_ore_poly_t poly, const fmpq_t x, gr_ore_poly_ctx_t ctx); |
| 167 | + |
| 168 | +/* Arithmetic */ |
| 169 | + |
| 170 | +WARN_UNUSED_RESULT int gr_ore_poly_neg(gr_ore_poly_t res, const gr_ore_poly_t src, gr_ore_poly_ctx_t ctx); |
| 171 | + |
| 172 | +WARN_UNUSED_RESULT int _gr_ore_poly_add(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, slong len2, gr_ore_poly_ctx_t ctx); |
| 173 | +WARN_UNUSED_RESULT int gr_ore_poly_add(gr_ore_poly_t res, const gr_ore_poly_t poly1, const gr_ore_poly_t poly2, gr_ore_poly_ctx_t ctx); |
| 174 | + |
| 175 | +WARN_UNUSED_RESULT int _gr_ore_poly_sub(gr_ptr res, gr_srcptr poly1, slong len1, gr_srcptr poly2, slong len2, gr_ore_poly_ctx_t ctx); |
| 176 | +WARN_UNUSED_RESULT int gr_ore_poly_sub(gr_ore_poly_t res, const gr_ore_poly_t poly1, const gr_ore_poly_t poly2, gr_ore_poly_ctx_t ctx); |
| 177 | + |
| 178 | +WARN_UNUSED_RESULT int gr_ore_poly_add_scalar(gr_ore_poly_t res, const gr_ore_poly_t poly, gr_srcptr c, gr_ore_poly_ctx_t ctx); |
| 179 | +WARN_UNUSED_RESULT int gr_ore_poly_add_ui(gr_ore_poly_t res, const gr_ore_poly_t poly, ulong c, gr_ore_poly_ctx_t ctx); |
| 180 | +WARN_UNUSED_RESULT int gr_ore_poly_add_si(gr_ore_poly_t res, const gr_ore_poly_t poly, slong c, gr_ore_poly_ctx_t ctx); |
| 181 | +WARN_UNUSED_RESULT int gr_ore_poly_add_fmpz(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpz_t c, gr_ore_poly_ctx_t ctx); |
| 182 | +WARN_UNUSED_RESULT int gr_ore_poly_add_fmpq(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpq_t c, gr_ore_poly_ctx_t ctx); |
| 183 | + |
| 184 | +WARN_UNUSED_RESULT int gr_ore_poly_sub_scalar(gr_ore_poly_t res, const gr_ore_poly_t poly, gr_srcptr c, gr_ore_poly_ctx_t ctx); |
| 185 | +WARN_UNUSED_RESULT int gr_ore_poly_sub_ui(gr_ore_poly_t res, const gr_ore_poly_t poly, ulong c, gr_ore_poly_ctx_t ctx); |
| 186 | +WARN_UNUSED_RESULT int gr_ore_poly_sub_si(gr_ore_poly_t res, const gr_ore_poly_t poly, slong c, gr_ore_poly_ctx_t ctx); |
| 187 | +WARN_UNUSED_RESULT int gr_ore_poly_sub_fmpz(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpz_t c, gr_ore_poly_ctx_t ctx); |
| 188 | +WARN_UNUSED_RESULT int gr_ore_poly_sub_fmpq(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpq_t c, gr_ore_poly_ctx_t ctx); |
| 189 | + |
| 190 | +WARN_UNUSED_RESULT int gr_ore_poly_scalar_mul(gr_ore_poly_t res, gr_srcptr c, const gr_ore_poly_t poly, gr_ore_poly_ctx_t ctx); |
| 191 | +WARN_UNUSED_RESULT int gr_ore_poly_mul_ui(gr_ore_poly_t res, const gr_ore_poly_t poly, ulong c, gr_ore_poly_ctx_t ctx); |
| 192 | +WARN_UNUSED_RESULT int gr_ore_poly_mul_si(gr_ore_poly_t res, const gr_ore_poly_t poly, slong c, gr_ore_poly_ctx_t ctx); |
| 193 | +WARN_UNUSED_RESULT int gr_ore_poly_mul_fmpz(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpz_t c, gr_ore_poly_ctx_t ctx); |
| 194 | +WARN_UNUSED_RESULT int gr_ore_poly_mul_fmpq(gr_ore_poly_t res, const gr_ore_poly_t poly, const fmpq_t c, gr_ore_poly_ctx_t ctx); |
| 195 | + |
| 196 | +/* Conversion */ |
| 197 | + |
| 198 | +// WARN_UNUSED_RESULT int gr_ore_poly_to_gr_poly_mat(gr_mat_t res, const gr_ore_poly_t ore_poly, gr_ore_poly_ctx_t ctx); |
| 199 | +// WARN_UNUSED_RESULT int gr_poly_mat_to_gr_ore_poly(gr_ore_poly_t res, const gr_mat_t mat, gr_ore_poly_ctx_t ctx); |
| 200 | + |
| 201 | +#ifdef __cplusplus |
| 202 | +} |
| 203 | +#endif |
| 204 | + |
| 205 | +#endif |
0 commit comments