Skip to content

Commit e47b2a3

Browse files
committed
Add context methods for neg, add, sub and mul
1 parent 8613fd3 commit e47b2a3

File tree

2 files changed

+198
-1
lines changed

2 files changed

+198
-1
lines changed

src/flint/types/_gr.pxd

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@ from flint.flintlib.functions.gr cimport (
119119
gr_neg,
120120
gr_add,
121121
gr_add_si,
122+
gr_add_other,
123+
gr_other_add,
122124
gr_sub,
123125
gr_sub_si,
126+
gr_sub_other,
127+
gr_other_sub,
124128
gr_mul,
125129
gr_mul_si,
130+
gr_mul_other,
131+
gr_other_mul,
126132
gr_inv,
127133
gr_div,
128134
gr_div_si,
@@ -390,6 +396,123 @@ cdef class gr_ctx(flint_ctx):
390396
cdef inline truth_t _is_neg_one(self, gr x):
391397
return gr_is_neg_one(x.pval, self.ctx_t)
392398

399+
@cython.final
400+
cdef inline gr _neg(self, gr x):
401+
cdef int err
402+
cdef gr res = self.new_gr()
403+
err = gr_neg(res.pval, x.pval, self.ctx_t)
404+
if err != GR_SUCCESS:
405+
raise self._error(err, "Cannot negate x in this context")
406+
return res
407+
408+
@cython.final
409+
cdef inline gr _add(self, gr x, gr y):
410+
cdef int err
411+
cdef gr res = self.new_gr()
412+
err = gr_add(res.pval, x.pval, y.pval, self.ctx_t)
413+
if err != GR_SUCCESS:
414+
raise self._error(err, "Cannot add x and y in this context")
415+
return res
416+
417+
@cython.final
418+
cdef inline gr _add_other(self, gr x, gr y):
419+
cdef int err
420+
cdef gr res = self.new_gr()
421+
err = gr_add_other(res.pval, x.pval, y.pval, y.ctx.ctx_t, self.ctx_t)
422+
if err != GR_SUCCESS:
423+
raise self._error(err, "Cannot add x and y in this context")
424+
return res
425+
426+
@cython.final
427+
cdef inline gr _other_add(self, gr x, gr y):
428+
cdef int err
429+
cdef gr res = self.new_gr()
430+
err = gr_other_add(res.pval, x.pval, x.ctx.ctx_t, y.pval, self.ctx_t)
431+
if err != GR_SUCCESS:
432+
raise self._error(err, "Cannot add x and y in this context")
433+
return res
434+
435+
@cython.final
436+
cdef inline gr _add_si(self, gr x, slong y):
437+
cdef int err
438+
cdef gr res = self.new_gr()
439+
err = gr_add_si(res.pval, x.pval, y, self.ctx_t)
440+
if err != GR_SUCCESS:
441+
raise self._error(err, "Cannot add x and y in this context")
442+
return res
443+
444+
@cython.final
445+
cdef inline gr _sub(self, gr x, gr y):
446+
cdef int err
447+
cdef gr res = self.new_gr()
448+
err = gr_sub(res.pval, x.pval, y.pval, self.ctx_t)
449+
if err != GR_SUCCESS:
450+
raise self._error(err, "Cannot sub x and y in this context")
451+
return res
452+
453+
@cython.final
454+
cdef inline gr _sub_other(self, gr x, gr y):
455+
cdef int err
456+
cdef gr res = self.new_gr()
457+
err = gr_sub_other(res.pval, x.pval, y.pval, y.ctx.ctx_t, self.ctx_t)
458+
if err != GR_SUCCESS:
459+
raise self._error(err, "Cannot sub x and y in this context")
460+
return res
461+
462+
@cython.final
463+
cdef inline gr _other_sub(self, gr x, gr y):
464+
cdef int err
465+
cdef gr res = self.new_gr()
466+
err = gr_other_sub(res.pval, x.pval, x.ctx.ctx_t, y.pval, self.ctx_t)
467+
if err != GR_SUCCESS:
468+
raise self._error(err, "Cannot sub x and y in this context")
469+
return res
470+
471+
@cython.final
472+
cdef inline gr _sub_si(self, gr x, slong y):
473+
cdef int err
474+
cdef gr res = self.new_gr()
475+
err = gr_sub_si(res.pval, x.pval, y, self.ctx_t)
476+
if err != GR_SUCCESS:
477+
raise self._error(err, "Cannot sub x and y in this context")
478+
return res
479+
480+
@cython.final
481+
cdef inline gr _mul(self, gr x, gr y):
482+
cdef int err
483+
cdef gr res = self.new_gr()
484+
err = gr_mul(res.pval, x.pval, y.pval, self.ctx_t)
485+
if err != GR_SUCCESS:
486+
raise self._error(err, "Cannot mul x and y in this context")
487+
return res
488+
489+
@cython.final
490+
cdef inline gr _mul_other(self, gr x, gr y):
491+
cdef int err
492+
cdef gr res = self.new_gr()
493+
err = gr_mul_other(res.pval, x.pval, y.pval, y.ctx.ctx_t, self.ctx_t)
494+
if err != GR_SUCCESS:
495+
raise self._error(err, "Cannot mul x and y in this context")
496+
return res
497+
498+
@cython.final
499+
cdef inline gr _other_mul(self, gr x, gr y):
500+
cdef int err
501+
cdef gr res = self.new_gr()
502+
err = gr_other_mul(res.pval, x.pval, x.ctx.ctx_t, y.pval, self.ctx_t)
503+
if err != GR_SUCCESS:
504+
raise self._error(err, "Cannot mul x and y in this context")
505+
return res
506+
507+
@cython.final
508+
cdef inline gr _mul_si(self, gr x, slong y):
509+
cdef int err
510+
cdef gr res = self.new_gr()
511+
err = gr_mul_si(res.pval, x.pval, y, self.ctx_t)
512+
if err != GR_SUCCESS:
513+
raise self._error(err, "Cannot mul x and y in this context")
514+
return res
515+
393516
# @cython.final
394517
# cdef inline list _gens_recursive(self):
395518
# cdef int err
@@ -919,7 +1042,7 @@ cdef class gr_series_ctx(gr_ctx):
9191042
@cython.no_gc
9201043
cdef class gr(flint_scalar):
9211044
cdef gr_ptr pval
922-
cdef gr_ctx ctx
1045+
cdef public gr_ctx ctx
9231046
cdef bint _init
9241047

9251048
@cython.final

src/flint/types/_gr.pyx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,80 @@ cdef class gr_ctx(flint_ctx):
397397
"""
398398
return truth_to_py(self._is_neg_one(x))
399399

400+
def neg(self, x):
401+
"""
402+
Returns `-x`.
403+
404+
>>> from flint.types._gr import gr_complex_acb_ctx, gr_real_arb_ctx
405+
>>> arb = gr_real_arb_ctx.new(53); acb = gr_complex_acb_ctx.new(106)
406+
>>> c = acb("2 + I").sqrt(); c
407+
([1.4553466902253548081226618397097 +/- 3.48e-32] + [0.3435607497225124641385657439146 +/- 5.23e-32]*I)
408+
>>> arb.neg(c)
409+
[-1.455346690225355 +/- 1.92e-16]
410+
"""
411+
return self._neg(x)
412+
413+
def add(self, x, y) -> gr:
414+
"""
415+
Returns `x + y`
416+
417+
"""
418+
if isinstance(x, gr) and isinstance(y, gr):
419+
if x.ctx == self and y.ctx == self:
420+
return self._add(x, y)
421+
if x.ctx == self:
422+
return self._add_other(x, y)
423+
if y.ctx == self:
424+
return self._other_add(x, y)
425+
426+
if isinstance(x, gr) and x.ctx == self and type(y) is int:
427+
try:
428+
return self._add_si(x, y)
429+
except OverflowError:
430+
pass
431+
432+
# NOTE: By default, convert everything to the required
433+
# context before performing the operation
434+
return self._add(self(x), self(y))
435+
436+
def sub(self, x, y):
437+
if isinstance(x, gr) and isinstance(y, gr):
438+
if x.ctx == self and y.ctx == self:
439+
return self._sub(x, y)
440+
if x.ctx == self:
441+
return self._sub_other(x, y)
442+
if y.ctx == self:
443+
return self._other_sub(x, y)
444+
445+
if isinstance(x, gr) and x.ctx == self and type(y) is int:
446+
try:
447+
return self._sub_si(x, y)
448+
except OverflowError:
449+
pass
450+
451+
# NOTE: By default, convert everything to the required
452+
# context before performing the operation
453+
return self._sub(self(x), self(y))
454+
455+
def mul(self, x, y):
456+
if isinstance(x, gr) and isinstance(y, gr):
457+
if x.ctx == self and y.ctx == self:
458+
return self._mul(x, y)
459+
if x.ctx == self:
460+
return self._mul_other(x, y)
461+
if y.ctx == self:
462+
return self._other_mul(x, y)
463+
464+
if isinstance(x, gr) and x.ctx == self and type(y) is int:
465+
try:
466+
return self._mul_si(x, y)
467+
except OverflowError:
468+
pass
469+
470+
# NOTE: By default, convert everything to the required
471+
# context before performing the operation
472+
return self._mul(self(x), self(y))
473+
400474
# def gens_recursive(self) -> list[gr]:
401475
# """Return all generators of the domain
402476

0 commit comments

Comments
 (0)