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

Commit

Permalink
Merge branch 'u/mkoeppe/sage_rings__real_complex__double__remove_comp…
Browse files Browse the repository at this point in the history
…ile_time_dependency_on_cypari2' of git://trac.sagemath.org/sage into u/gh-kliem/sage_rings__real_complex__double__remove_compile_time_dependency_on_cypari2
  • Loading branch information
Jonathan Kliem committed Oct 25, 2021
2 parents 056b8d4 + d641dc6 commit e43dfad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 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 @@ -728,38 +729,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 @@ -1171,10 +1140,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 @@ -2265,8 +2232,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
3 changes: 1 addition & 2 deletions src/sage/rings/real_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ gsl_set_error_handler_off()

import math, operator

from cypari2.convert cimport new_gen_from_double

import sage.rings.integer
import sage.rings.rational

Expand Down Expand Up @@ -1679,6 +1677,7 @@ cdef class RealDoubleElement(FieldElement):
sage: RDF(1.5).__pari__()
1.50000000000000
"""
from cypari2.convert import new_gen_from_double
return new_gen_from_double(self._value)


Expand Down

0 comments on commit e43dfad

Please sign in to comment.