forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get/set_str methods for gr_poly (flintlib#2238)
* Add get/set_str methods for gr_poly
- Loading branch information
1 parent
90f5c30
commit 2ddde79
Showing
7 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright (C) 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "gr_vec.h" | ||
#include "gr_poly.h" | ||
|
||
int | ||
gr_poly_set_str(gr_poly_t res, const char * s, const char * x, gr_ctx_t ctx) | ||
{ | ||
gr_ctx_t rctx; | ||
int status = GR_SUCCESS; | ||
gr_ctx_init_gr_poly(rctx, ctx); | ||
status |= gr_ctx_set_gen_name(rctx, x); | ||
status |= gr_set_str(res, s, rctx); | ||
gr_ctx_clear(rctx); | ||
return status; | ||
} | ||
|
||
int | ||
_gr_poly_set_str(gr_ptr res, const char * s, const char * x, slong len, gr_ctx_t ctx) | ||
{ | ||
gr_poly_t t; | ||
int status; | ||
gr_poly_init(t, ctx); | ||
status = gr_poly_set_str(t, s, x, ctx); | ||
|
||
if (status == GR_SUCCESS) | ||
{ | ||
if (t->length <= len) | ||
{ | ||
_gr_vec_swap(res, t->coeffs, t->length, ctx); | ||
status |= _gr_vec_zero(GR_ENTRY(res, t->length, ctx->sizeof_elem), len - t->length, ctx); | ||
/* make sure t is a valid polynomial before clearing | ||
just so that we don't strike the debug assertion in gr_poly_clear */ | ||
_gr_poly_set_length(t, 0, ctx); | ||
} | ||
else | ||
{ | ||
status |= _gr_vec_zero(res, len, ctx); | ||
status = GR_UNABLE; | ||
} | ||
} | ||
|
||
gr_poly_clear(t, ctx); | ||
return status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright (C) 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "test_helpers.h" | ||
#include "ulong_extras.h" | ||
#include "fmpz.h" | ||
#include "gr_vec.h" | ||
#include "gr_poly.h" | ||
|
||
TEST_FUNCTION_START(gr_poly_set_str, state) | ||
{ | ||
slong iter; | ||
|
||
for (iter = 0; iter < 1000 * flint_test_multiplier(); iter++) | ||
{ | ||
int status = GR_SUCCESS; | ||
gr_ctx_t ctx; | ||
gr_poly_t f, g; | ||
slong glen; | ||
char * s; | ||
|
||
gr_ctx_init_random(ctx, state); | ||
gr_poly_init(f, ctx); | ||
gr_poly_init(g, ctx); | ||
|
||
status |= gr_poly_randtest(f, state, 1 + n_randint(state, 10), ctx); | ||
status |= gr_poly_randtest(g, state, 1 + n_randint(state, 10), ctx); | ||
|
||
if (n_randint(state, 2)) | ||
{ | ||
status |= gr_poly_get_str(&s, f, "XX", ctx); | ||
status |= gr_poly_set_str(g, s, "XX", ctx); | ||
glen = -1; | ||
|
||
if ((ctx->which_ring == GR_CTX_FMPZ || ctx->which_ring == GR_CTX_FMPQ) && | ||
status != GR_SUCCESS) | ||
{ | ||
flint_printf("FAIL: unable over ZZ or QQ\n\n"); | ||
flint_printf("f = %{gr_poly}\n\n", f, ctx); | ||
flint_printf("s = %s\n\n", s); | ||
flint_abort(); | ||
} | ||
} | ||
else | ||
{ | ||
status |= _gr_poly_get_str(&s, f->coeffs, f->length, "XX", ctx); | ||
glen = n_randint(state, 10); | ||
gr_poly_fit_length(g, glen, ctx); | ||
status |= _gr_poly_set_str(g->coeffs, s, "XX", glen, ctx); | ||
_gr_poly_set_length(g, glen, ctx); | ||
_gr_poly_normalise(g, ctx); | ||
} | ||
|
||
if (status == GR_SUCCESS && gr_poly_equal(f, g, ctx) == T_FALSE) | ||
{ | ||
flint_printf("FAIL: get_str, set_str roundtrip\n\n"); | ||
flint_printf("f = %{gr_poly}\n\n", f, ctx); | ||
flint_printf("s = %s\n\n", s); | ||
flint_printf("glen = %wd\n\n", glen); | ||
flint_printf("g = %{gr_poly}\n\n", g, ctx); | ||
flint_abort(); | ||
} | ||
|
||
flint_free(s); | ||
|
||
gr_poly_clear(f, ctx); | ||
gr_poly_clear(g, ctx); | ||
gr_ctx_clear(ctx); | ||
} | ||
|
||
TEST_FUNCTION_END(state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters