Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
src/sage/rings/complex_double.pyx: Move PARI conversion functions to …
Browse files Browse the repository at this point in the history
…src/sage/libs/pari/
  • Loading branch information
Matthias Koeppe committed Oct 22, 2021
1 parent 2cc2fa7 commit d641dc6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
6 changes: 6 additions & 0 deletions src/sage/libs/pari/convert_sage_complex_double.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cypari2.gen cimport Gen
from sage.rings.complex_double cimport ComplexDoubleElement

cdef inline ComplexDoubleElement pari_to_cdf(Gen g)

cpdef Gen new_gen_from_complex_double_element(ComplexDoubleElement self)
48 changes: 48 additions & 0 deletions src/sage/libs/pari/convert_sage_complex_double.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from cysignals.signals cimport sig_on, sig_off

from sage.libs.gsl.complex cimport *

from cypari2.paridecl cimport *
from cypari2.convert cimport new_gen_from_double, new_t_COMPLEX_from_double


cdef inline ComplexDoubleElement pari_to_cdf(Gen g):
"""
Create a CDF element from a PARI ``gen``.
EXAMPLES::
sage: CDF(pari("Pi"))
3.141592653589793
sage: CDF(pari("1 + I/2"))
1.0 + 0.5*I
TESTS:
Check that we handle PARI errors gracefully, see :trac:`17329`::
sage: CDF(-151.386325246 + 992.34771962*I).zeta()
Traceback (most recent call last):
...
PariError: overflow in t_REAL->double conversion
sage: CDF(pari(x^2 + 5))
Traceback (most recent call last):
...
PariError: incorrect type in gtofp (t_POL)
"""
cdef ComplexDoubleElement z = ComplexDoubleElement.__new__(ComplexDoubleElement)
sig_on()
if typ(g.g) == t_COMPLEX:
z._complex = gsl_complex_rect(gtodouble(gel(g.g, 1)), gtodouble(gel(g.g, 2)))
else:
z._complex = gsl_complex_rect(gtodouble(g.g), 0.0)
sig_off()
return z


cpdef Gen new_gen_from_complex_double_element(ComplexDoubleElement self):

if not self._complex.imag:
return new_gen_from_double(self._complex.real)
else:
return new_t_COMPLEX_from_double(self._complex.real, self._complex.imag)
51 changes: 8 additions & 43 deletions src/sage/rings/complex_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ AUTHORS:

import operator
from cpython.object cimport Py_NE
from cysignals.signals cimport sig_on, sig_off

from sage.misc.randstate cimport randstate, current_randstate

from cypari2.paridecl cimport *

from sage.libs.gsl.complex cimport *

cdef extern from "<complex.h>":
Expand All @@ -92,8 +89,12 @@ from sage.structure.richcmp cimport rich_to_bool
from sage.categories.morphism cimport Morphism
from sage.structure.coerce cimport is_numpy_type

from cypari2.gen cimport Gen as pari_gen
from cypari2.convert cimport new_gen_from_double, new_t_COMPLEX_from_double
try:
from cypari2.gen import Gen as pari_gen
from cypari2.convert_sage_complex_double import pari_to_cdf

except ImportError:
pari_gen = ()

from . import complex_mpfr

Expand Down Expand Up @@ -718,38 +719,6 @@ def is_ComplexDoubleElement(x):
"""
return isinstance(x, ComplexDoubleElement)

cdef inline ComplexDoubleElement pari_to_cdf(pari_gen g):
"""
Create a CDF element from a PARI ``gen``.
EXAMPLES::
sage: CDF(pari("Pi"))
3.141592653589793
sage: CDF(pari("1 + I/2"))
1.0 + 0.5*I
TESTS:
Check that we handle PARI errors gracefully, see :trac:`17329`::
sage: CDF(-151.386325246 + 992.34771962*I).zeta()
Traceback (most recent call last):
...
PariError: overflow in t_REAL->double conversion
sage: CDF(pari(x^2 + 5))
Traceback (most recent call last):
...
PariError: incorrect type in gtofp (t_POL)
"""
cdef ComplexDoubleElement z = ComplexDoubleElement.__new__(ComplexDoubleElement)
sig_on()
if typ(g.g) == t_COMPLEX:
z._complex = gsl_complex_rect(gtodouble(gel(g.g, 1)), gtodouble(gel(g.g, 2)))
else:
z._complex = gsl_complex_rect(gtodouble(g.g), 0.0)
sig_off()
return z

cdef class ComplexDoubleElement(FieldElement):
"""
Expand Down Expand Up @@ -1161,10 +1130,8 @@ cdef class ComplexDoubleElement(FieldElement):
sage: pari(CDF(I))
1.00000000000000*I
"""
if not self._complex.imag:
return new_gen_from_double(self._complex.real)
else:
return new_t_COMPLEX_from_double(self._complex.real, self._complex.imag)
from sage.libs.pari.convert_sage_complex_double import new_gen_from_complex_double_element
return new_gen_from_complex_double_element(self)

def __mpc__(self):
"""
Expand Down Expand Up @@ -2255,8 +2222,6 @@ cdef class ComplexDoubleElement(FieldElement):
sage: eta(z)
0.7420487758365647 + 0.1988313702299107*I
"""
cdef GEN a, b, c, y, t

if self._complex.imag <= 0:
raise ValueError("value must be in the upper half plane")

Expand Down

0 comments on commit d641dc6

Please sign in to comment.