Skip to content

Commit 9b00a9d

Browse files
Merge pull request #257 from Tom-Hubrecht/array-api
Add some methods defined in the Array-API
2 parents e50cda6 + 2bcdaa9 commit 9b00a9d

File tree

1 file changed

+72
-22
lines changed

1 file changed

+72
-22
lines changed

src/flint/types/_gr.pyx

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,47 @@ cdef class gr_ctx(flint_ctx):
779779
def max(self, x, y) -> gr:
780780
return self._max(self(x), self(y))
781781
782+
###
783+
# Array-API wrappers
784+
785+
def divide(self, x, y) -> gr:
786+
return self.div(x, y)
787+
788+
def greater(self, x, y):
789+
return self.gt(x, y)
790+
791+
def greater_equal(self, x, y):
792+
return self.ge(x, y)
793+
794+
def less(self, x, y):
795+
return self.lt(x, y)
796+
797+
def less_equal(self, x, y):
798+
return self.le(x, y)
799+
800+
def imag(self, x):
801+
return self.im(x)
802+
803+
def real(self, x):
804+
return self.re(x)
805+
806+
def maximum(self, x, y):
807+
return self.max(x, y)
808+
809+
def minimum(self, x, y):
810+
return self.min(x, y)
811+
812+
def multiply(self, x, y):
813+
return self.mul(x, y)
814+
815+
def negative(self, x):
816+
return self.neg(x)
817+
818+
def not_equal(self, x, y):
819+
eq = self.equal(x, y)
820+
if eq is None:
821+
return None
822+
return not eq
782823
783824
cdef class gr_scalar_ctx(gr_ctx):
784825
"""Base class for all scalar contexts."""
@@ -1661,6 +1702,18 @@ cdef class gr(flint_scalar):
16611702
def __repr__(self):
16621703
return self.ctx.to_str(self)
16631704

1705+
def parent(self) -> gr_ctx:
1706+
"""
1707+
Return the parent context.
1708+
1709+
>>> from flint.types._gr import gr_complex_acb_ctx
1710+
>>> acb = gr_complex_acb_ctx.new(53)
1711+
>>> x = acb("pi")
1712+
>>> x.parent()
1713+
gr_complex_acb_ctx(53)
1714+
"""
1715+
return self.ctx
1716+
16641717
def is_zero(self):
16651718
"""Return whether the element is zero (may return ``None``).
16661719
@@ -1838,10 +1891,7 @@ cdef class gr(flint_scalar):
18381891
return NotImplemented
18391892

18401893
def __pow__(self, other) -> gr:
1841-
if isinstance(other, int):
1842-
return self._pow_si(other)
1843-
else:
1844-
return NotImplemented
1894+
return self.ctx.pow(self, other)
18451895

18461896
def is_square(self):
18471897
"""Return whether the element is a square (may return ``None``).
@@ -1854,7 +1904,7 @@ cdef class gr(flint_scalar):
18541904
>>> Q(4).sqrt()
18551905
2
18561906
"""
1857-
return truth_to_py(self._is_square())
1907+
return truth_to_py(self.ctx.is_square(self))
18581908

18591909
def sqrt(self):
18601910
"""Return the square root of the element if it exists.
@@ -1863,7 +1913,7 @@ cdef class gr(flint_scalar):
18631913
>>> Z(4).sqrt()
18641914
2
18651915
"""
1866-
return self._sqrt()
1916+
return self.ctx.sqrt(self)
18671917

18681918
def rsqrt(self):
18691919
"""Return the reciprocal square root of the element if it exists.
@@ -1872,7 +1922,7 @@ cdef class gr(flint_scalar):
18721922
>>> Q(4).rsqrt()
18731923
1/2
18741924
"""
1875-
return self._rsqrt()
1925+
return self.ctx.rsqrt(self)
18761926

18771927
def gcd(self, other):
18781928
"""Return the greatest common divisor of two elements.
@@ -1902,7 +1952,7 @@ cdef class gr(flint_scalar):
19021952
other_gr = other
19031953
if not self.ctx == other_gr.ctx:
19041954
raise TypeError("gcd of gr with different contexts.")
1905-
return self._lcm(other_gr)
1955+
return self.ctx.lcm(self, other_gr)
19061956

19071957
def factor(self):
19081958
"""Return the factorization of the element.
@@ -1911,7 +1961,7 @@ cdef class gr(flint_scalar):
19111961
>>> Z(12).factor()
19121962
(1, [(2, 2), (3, 1)])
19131963
"""
1914-
return self._factor()
1964+
return self.ctx.factor(self)
19151965

19161966
def numer(self) -> gr:
19171967
"""Return the numerator of the element.
@@ -1925,7 +1975,7 @@ cdef class gr(flint_scalar):
19251975

19261976
See also :meth:`denom`.
19271977
"""
1928-
return self._numerator()
1978+
return self.ctx.numerator(self)
19291979

19301980
def denom(self) -> gr:
19311981
"""Return the denominator of the element.
@@ -1939,21 +1989,21 @@ cdef class gr(flint_scalar):
19391989

19401990
See also :meth:`numer`.
19411991
"""
1942-
return self._denominator()
1992+
return self.ctx.denominator(self)
19431993

19441994
def __floor__(self) -> gr:
1945-
return self._floor()
1995+
return self.ctx.floor(self)
19461996

19471997
def __ceil__(self) -> gr:
1948-
return self._ceil()
1998+
return self.ctx.ceil(self)
19491999

19502000
def __trunc__(self) -> gr:
1951-
return self._trunc()
2001+
return self.ctx.trunc(self)
19522002

19532003
def __round__(self, ndigits: int = 0) -> gr:
19542004
if ndigits != 0:
19552005
raise NotImplementedError("Rounding to a specific number of digits is not supported")
1956-
return self._nint()
2006+
return self.ctx.nint(self)
19572007

19582008
# def __int__(self) -> int:
19592009
# return self._floor().to_int()
@@ -1962,7 +2012,7 @@ cdef class gr(flint_scalar):
19622012
# return ...
19632013

19642014
def __abs__(self) -> gr:
1965-
return self._abs()
2015+
return self.ctx.abs(self)
19662016

19672017
def conjugate(self) -> gr:
19682018
"""Return complex conjugate of the element.
@@ -1972,7 +2022,7 @@ cdef class gr(flint_scalar):
19722022
>>> (1 + I).conjugate()
19732023
(1-I)
19742024
"""
1975-
return self._conj()
2025+
return self.ctx.conj(self)
19762026

19772027
@property
19782028
def real(self) -> gr:
@@ -1983,7 +2033,7 @@ cdef class gr(flint_scalar):
19832033
>>> (1 + I).real
19842034
1
19852035
"""
1986-
return self._re()
2036+
return self.ctx.re(self)
19872037

19882038
@property
19892039
def imag(self) -> gr:
@@ -1994,7 +2044,7 @@ cdef class gr(flint_scalar):
19942044
>>> (1 + I).imag
19952045
1
19962046
"""
1997-
return self._im()
2047+
return self.ctx.im(self)
19982048

19992049
# XXX: Return -1, 0, 1 as int?
20002050
def sgn(self) -> gr:
@@ -2008,7 +2058,7 @@ cdef class gr(flint_scalar):
20082058
>>> Q(0).sgn()
20092059
0
20102060
"""
2011-
return self._sgn()
2061+
return self.ctx.sgn(self)
20122062

20132063
def csgn(self) -> gr:
20142064
"""Return the complex sign of the element.
@@ -2018,7 +2068,7 @@ cdef class gr(flint_scalar):
20182068
>>> (1 + C.i()).csgn() # doctest: +SKIP
20192069
1
20202070
"""
2021-
return self._csgn()
2071+
return self.ctx.csgn(self)
20222072

20232073
def arg(self) -> gr:
20242074
"""Return the argument of the element.
@@ -2028,4 +2078,4 @@ cdef class gr(flint_scalar):
20282078
>>> (1 + C.i()).arg()
20292079
[0.785 +/- 6.45e-4]
20302080
"""
2031-
return self._arg()
2081+
return self.ctx.arg(self)

0 commit comments

Comments
 (0)