|
| 1 | +/* |
| 2 | + Copyright (C) 2025 Andrii Yanovets |
| 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 "test_helpers.h" |
| 13 | +#include "calcium.h" |
| 14 | +#include "fmpz_mod_mpoly.h" |
| 15 | +#include "fmpz_mpoly.h" |
| 16 | +#include "mpoly.h" |
| 17 | + |
| 18 | +TEST_FUNCTION_START(fmpz_mod_mpoly_from_fmpz_mpoly, state) |
| 19 | +{ |
| 20 | + slong iter; |
| 21 | + |
| 22 | + for (iter = 0; iter < 10000 * 0.1 * flint_test_multiplier(); iter++) |
| 23 | + { |
| 24 | + fmpz_mpoly_ctx_t ctx; |
| 25 | + fmpz_mod_mpoly_ctx_t ctxm; |
| 26 | + fmpz_mod_mpoly_t A, B; |
| 27 | + fmpz_mpoly_t C, D; |
| 28 | + slong nvars, i; |
| 29 | + fmpz_t m, c; |
| 30 | + |
| 31 | + nvars = 1 + n_randint(state, 3); |
| 32 | + fmpz_init(m); |
| 33 | + fmpz_init(c); |
| 34 | + fmpz_randtest_unsigned(m, state, n_randint(state, 2) ? 4 : n_randint(state, 90)); |
| 35 | + fmpz_nextprime(m, m, 0); |
| 36 | + fmpz_mod_mpoly_ctx_init(ctxm, nvars, ORD_LEX, m); |
| 37 | + fmpz_mpoly_ctx_init(ctx, nvars, ORD_LEX); |
| 38 | + |
| 39 | + fmpz_mod_mpoly_init(A, ctxm); |
| 40 | + fmpz_mod_mpoly_init(B, ctxm); |
| 41 | + fmpz_mpoly_init(C, ctx); |
| 42 | + fmpz_mpoly_init(D, ctx); |
| 43 | + |
| 44 | + fmpz ** exp = (fmpz **) flint_malloc(ctx->minfo->nvars*sizeof(fmpz *)); |
| 45 | + |
| 46 | + for (i = 0; i < ctx->minfo->nvars; i++) |
| 47 | + { |
| 48 | + exp[i] = (fmpz *) flint_malloc(sizeof(fmpz)); |
| 49 | + fmpz_init(exp[i]); |
| 50 | + } |
| 51 | + |
| 52 | + // flint_printf("iter %wd %wd %d %{fmpz}\n\n", iter, nvars, ctx->minfo->ord, m); |
| 53 | + // printf("--------------------------------------------------------------------\n"); |
| 54 | + |
| 55 | + |
| 56 | + fmpz_mpoly_randtest_bound(C, state, 1 + n_randint(state, 4), 1 + n_randint(state, 4), 1 + n_randint(state, 4), ctx); |
| 57 | + |
| 58 | + fmpz_mod_mpoly_resize(B, C->length, ctxm); |
| 59 | + |
| 60 | + for (i = 0; i< C->length; i++) |
| 61 | + { |
| 62 | + fmpz_mpoly_get_term_coeff_fmpz(c, C, i, ctx); |
| 63 | + fmpz_mpoly_get_term_exp_fmpz(exp, C, i, ctx); |
| 64 | + |
| 65 | + fmpz_mod_mpoly_set_term_exp_fmpz(B, i, exp, ctxm); |
| 66 | + fmpz_mod_mpoly_set_term_coeff_fmpz(B, i, c, ctxm); |
| 67 | + } |
| 68 | + |
| 69 | + fmpz_mod_mpoly_combine_like_terms(B, ctxm); |
| 70 | + fmpz_mod_mpoly_from_fmpz_mpoly(A, C, ctxm, ctx); |
| 71 | + |
| 72 | + if (!fmpz_mod_mpoly_equal(A, B, ctxm)) |
| 73 | + { |
| 74 | + flint_printf("FAIL\n\n"); |
| 75 | + mpoly_ordering_print(ctx->minfo->ord); printf("\n"); |
| 76 | + flint_printf("C = "); fmpz_mpoly_print_pretty(C, NULL, ctx); flint_printf("\n"); |
| 77 | + flint_printf("A = "); fmpz_mod_mpoly_print_pretty(A, NULL, ctxm); flint_printf("\n"); |
| 78 | + flint_printf("B = "); fmpz_mod_mpoly_print_pretty(B, NULL, ctxm); flint_printf("\n"); |
| 79 | + flint_abort(); |
| 80 | + } |
| 81 | + |
| 82 | + fmpz_mpoly_resize(C, A->length, ctx); |
| 83 | + |
| 84 | + for (i = 0; i< A->length; i++) |
| 85 | + { |
| 86 | + fmpz_mod_mpoly_get_term_coeff_fmpz(c, A, i, ctxm); |
| 87 | + fmpz_mod_mpoly_get_term_exp_fmpz(exp, A, i, ctxm); |
| 88 | + |
| 89 | + fmpz_mpoly_set_term_exp_fmpz(C, i, exp, ctx); |
| 90 | + fmpz_mpoly_set_term_coeff_fmpz(C, i, c, ctx); |
| 91 | + } |
| 92 | + |
| 93 | + fmpz_mod_mpoly_to_fmpz_mpoly(D, A, ctx, ctxm); |
| 94 | + |
| 95 | + if (!fmpz_mpoly_equal(C, D, ctx)) |
| 96 | + { |
| 97 | + flint_printf("FAIL\n\n"); |
| 98 | + mpoly_ordering_print(ctx->minfo->ord); printf("\n"); |
| 99 | + flint_printf("C = "); fmpz_mpoly_print_pretty(C, NULL, ctx); flint_printf("\n"); |
| 100 | + flint_printf("D = "); fmpz_mpoly_print_pretty(D, NULL, ctx); flint_printf("\n"); |
| 101 | + flint_printf("A = "); fmpz_mod_mpoly_print_pretty(A, NULL, ctxm); flint_printf("\n"); |
| 102 | + flint_abort(); |
| 103 | + } |
| 104 | + |
| 105 | + for (i = 0; i < ctx->minfo->nvars; i++) |
| 106 | + { |
| 107 | + fmpz_clear(exp[i]); |
| 108 | + flint_free(exp[i]); |
| 109 | + } |
| 110 | + flint_free(exp); |
| 111 | + |
| 112 | + fmpz_mod_mpoly_clear(A, ctxm); |
| 113 | + fmpz_mod_mpoly_clear(B, ctxm); |
| 114 | + fmpz_mpoly_clear(C, ctx); |
| 115 | + fmpz_mpoly_clear(D, ctx); |
| 116 | + |
| 117 | + fmpz_mod_mpoly_ctx_clear(ctxm); |
| 118 | + fmpz_mpoly_ctx_clear(ctx); |
| 119 | + |
| 120 | + fmpz_clear(m); |
| 121 | + fmpz_clear(c); |
| 122 | + } |
| 123 | + |
| 124 | + TEST_FUNCTION_END(state); |
| 125 | +} |
0 commit comments