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

Commit

Permalink
Improve conversion of complex numbers to PARI
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Mar 5, 2014
1 parent ccab985 commit 988c1f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/sage/rings/complex_double.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1054,21 +1054,28 @@ cdef class ComplexDoubleElement(FieldElement):

cdef GEN _gen(self):
cdef GEN y
y = cgetg(3, t_COMPLEX) # allocate space for a complex number
set_gel(y, 1, pari.double_to_GEN(self._complex.dat[0]))
set_gel(y, 2, pari.double_to_GEN(self._complex.dat[1]))
if self._complex.dat[1] == 0:
# Return t_REAL
y = pari.double_to_GEN(self._complex.dat[0])
else:
# Return t_COMPLEX
y = cgetg(3, t_COMPLEX)
set_gel(y, 1, pari.double_to_GEN(self._complex.dat[0]))
set_gel(y, 2, pari.double_to_GEN(self._complex.dat[1]))
return y

def _pari_(self):
"""
Return PARI version of ``self``.
Return PARI version of ``self``, as ``t_COMPLEX`` or ``t_REAL``.
EXAMPLES::
sage: CDF(1,2)._pari_()
1.00000000000000 + 2.00000000000000*I
sage: pari(CDF(1,2))
1.00000000000000 + 2.00000000000000*I
sage: pari(CDF(2.0))
2.00000000000000
"""
pari_catch_sig_on()
return pari.new_gen(self._gen())
Expand Down
12 changes: 11 additions & 1 deletion src/sage/rings/complex_number.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ cdef class ComplexNumber(sage.structure.element.FieldElement):

def _pari_(self):
r"""
Coerces ``self`` into a Pari ``complex`` object.
Coerces ``self`` into a PARI ``t_COMPLEX`` object,
or a ``t_REAL`` if ``self`` is real.
EXAMPLES:
Expand All @@ -553,13 +554,22 @@ cdef class ComplexNumber(sage.structure.element.FieldElement):
sage: a = ComplexNumber(2,1)
sage: pari(a)
2.00000000000000 + 1.00000000000000*I
sage: pari(a).type()
't_COMPLEX'
sage: type(pari(a))
<type 'sage.libs.pari.gen.gen'>
sage: a._pari_()
2.00000000000000 + 1.00000000000000*I
sage: type(a._pari_())
<type 'sage.libs.pari.gen.gen'>
sage: a = CC(pi)
sage: pari(a)
3.14159265358979
sage: pari(a).type()
't_REAL'
"""
if self.is_real():
return self.real()._pari_()
return sage.libs.pari.all.pari.complex(self.real()._pari_(), self.imag()._pari_())

def _mpmath_(self, prec=None, rounding=None):
Expand Down

0 comments on commit 988c1f8

Please sign in to comment.