Skip to content

Commit f7f0c1e

Browse files
committed
feat: gr_mpoly allow setting generator names
1 parent f9f7a6a commit f7f0c1e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/flint/types/_gr.pxd

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cimport cython
2+
cimport libc.stdlib
23

34
from flint.flintlib.types.flint cimport (
45
slong,
@@ -79,7 +80,7 @@ from flint.flintlib.functions.gr_domains cimport (
7980

8081
gr_ctx_init_gr_poly,
8182
gr_ctx_init_gr_mpoly,
82-
gr_ctx_init_fmpz_mpoly_q,
83+
# gr_ctx_init_fmpz_mpoly_q,
8384
# gr_ctx_init_series_mod_gr_poly,
8485
gr_ctx_init_gr_series,
8586
)
@@ -95,6 +96,7 @@ from flint.flintlib.functions.gr cimport (
9596
gr_gen,
9697
gr_gens,
9798
gr_gens_recursive,
99+
gr_ctx_set_gen_names,
98100

99101
gr_i,
100102
gr_pos_inf,
@@ -781,19 +783,41 @@ cdef class gr_gr_poly_ctx(gr_poly_ctx):
781783
cdef class gr_gr_mpoly_ctx(gr_mpoly_ctx):
782784
cdef gr_ctx base_ctx
783785
cdef ordering_t _order
786+
cdef tuple _names
784787
cdef slong _nvars
785788

786789
@staticmethod
787-
cdef inline gr_gr_mpoly_ctx _new(gr_ctx base_ctx, slong nvars, Ordering order):
790+
cdef inline gr_gr_mpoly_ctx _new(gr_ctx base_ctx, tuple names, Ordering order):
788791
cdef gr_gr_mpoly_ctx ctx
789792
cdef ordering_t ord_c
793+
cdef slong nvars
794+
cdef const char **names_c
795+
cdef int status
796+
797+
nvars = len(names)
798+
names_b = [name.encode('utf-8') for name in names]
799+
790800
ord_c = ordering_py_to_c(order)
791801
ctx = gr_gr_mpoly_ctx.__new__(gr_gr_mpoly_ctx)
792802
gr_ctx_init_gr_mpoly(ctx.ctx_t, base_ctx.ctx_t, nvars, ord_c)
793803
ctx._init = True
794804
ctx.base_ctx = base_ctx
795805
ctx._order = ord_c
796806
ctx._nvars = nvars
807+
ctx._names = names
808+
809+
names_c = <const char **>libc.stdlib.malloc(nvars * sizeof(const char *))
810+
if names_c == NULL:
811+
raise MemoryError("Failed to allocate memory for generator names")
812+
try:
813+
for i in range(nvars):
814+
names_c[i] = names_b[i]
815+
status = gr_ctx_set_gen_names(ctx.ctx_t, names_c)
816+
finally:
817+
libc.stdlib.free(names_c)
818+
if status != GR_SUCCESS:
819+
raise MemoryError("Failed to set generator names")
820+
797821
return ctx
798822

799823

src/flint/types/_gr.pyx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,22 @@ cdef class gr_gr_poly_ctx(gr_poly_ctx):
538538
cdef class gr_gr_mpoly_ctx(gr_mpoly_ctx):
539539

540540
@staticmethod
541-
def new(base_ring, nvars, order=None) -> gr_gr_mpoly_ctx:
541+
def new(base_ring, names, order=None) -> gr_gr_mpoly_ctx:
542542
if order is None:
543543
order = Ordering.lex
544-
return gr_gr_mpoly_ctx._new(base_ring, nvars, order)
544+
return gr_gr_mpoly_ctx._new(base_ring, tuple(names), order)
545545

546546
def __repr__(self):
547-
return f"gr_gr_mpoly_ctx({self.base_ring}, {self.nvars}, {self.order})"
547+
return f"gr_gr_mpoly_ctx({self.base_ring}, {self.names}, {self.order})"
548548

549549
@property
550550
def base_ring(self):
551551
return self.base_ctx
552552

553+
@property
554+
def names(self):
555+
return self._names
556+
553557
@property
554558
def nvars(self):
555559
return self._nvars

0 commit comments

Comments
 (0)