Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Convert Sage/Python objects to real/complex intervals """ #***************************************************************************** # Copyright (C) 2018 Jeroen Demeyer <J.Demeyer@UGent.be> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from cpython.float cimport PyFloat_AS_DOUBLE from cpython.complex cimport PyComplex_RealAsDouble, PyComplex_ImagAsDouble
from sage.libs.mpfr cimport * from sage.libs.mpfi cimport *
from sage.arith.long cimport integer_check_long from sage.structure.element cimport Element, parent from ..integer cimport Integer from ..rational cimport Rational from ..real_mpfi cimport RealIntervalFieldElement, RealIntervalField_class from ..complex_interval_field import ComplexIntervalField_class from ..real_mpfr cimport RealNumber from ..real_double cimport RealDoubleElement from ..complex_number cimport ComplexNumber from ..complex_interval cimport ComplexIntervalFieldElement from ..complex_double cimport ComplexDoubleElement
from cypari2.gen cimport Gen
cdef inline int return_real(mpfi_ptr im): """ Called by ``mpfi_set_sage`` on the imaginary part when converting a real number. """
cdef int mpfi_set_sage(mpfi_ptr re, mpfi_ptr im, x, field, int base) except -1: """ Convert any object ``x`` to an MPFI interval or a pair of real and complex intervals.
INPUT:
- ``re`` -- a pre-initialized MPFI interval.
- ``im`` -- a pre-initialized MPFI interval or NULL.
- ``x`` -- any Sage or Python object to be converted to an interval.
- ``field`` -- a ``RealIntervalField`` or ``ComplexIntervalField`` of the right precision (real or complex doesn't matter).
- ``base`` -- base to use for string conversion.
OUTPUT:
- if conversion is possible: set ``re`` and ``im`` (if applicable) and return 0.
- if ``x`` is complex but ``im`` is ``NULL``: convert only if the imaginary component is 0.
- in all other cases: raise an exception. """ cdef RealIntervalFieldElement ri cdef ComplexIntervalFieldElement zi cdef ComplexNumber zn cdef ComplexDoubleElement zd cdef bytes s
# For complex numbers, interpret tuples as real/imag parts raise TypeError("tuple defining a complex number must have length 2") # Interpret entries in x as endpoints of interval raise TypeError("list defining an interval must have length 2")
cdef long value cdef int err
# Check for known types. First check for Element to reduce the # number of checks below. # Real
# Complex else: else: else: else: # not a Sage Element # Real else: mpfi_set_via_RR(re, x, field)
# Complex imag = x.imag() if im is NULL: if imag: raise TypeError(f"unable to convert complex PARI/GP element {x!r} to real interval") else: mpfi_set_via_RR(im, imag, field) mpfi_set_via_RR(re, x.real(), field) return 0 imag = PyComplex_ImagAsDouble(x) if im is NULL: if imag: raise TypeError(f"unable to convert complex number {x!r} to real interval") else: mpfi_set_d(im, imag) mpfi_set_d(re, PyComplex_RealAsDouble(x)) return 0
# No known type, try _real_mpfi_ or _complex_mpfi_ methods pass else: if not isinstance(field, ComplexIntervalField_class): field = field.complex_field() e = <ComplexIntervalFieldElement?>m(field) mpfi_swap(re, e.__re) mpfi_swap(im, e.__im) return 0
pass else:
# Finally, try converting via the corresponding RealField
cdef int mpfi_interv_sage(mpfi_ptr re, mpfi_ptr im, x, y, field, int base) except -1: """ Like ``mpfi_set_sage`` but construct the interval around ``x`` and ``y``. It is not required that ``x <= y`` or that ``x`` and ``y`` are of the same type.
INPUT: see ``mpfi_set_sage`` """ cdef long valx, valy cdef int err # General check for C long
# General case: convert both x and y to an interval and take the # union
# First handle x
# Now handle y, which requires temporary mpfi variables. cdef mpfi_t tmp1, tmp2
mpfi_init2(tmp2, prec) tmpim = tmp2
mpfi_union(im, im, tmpim) finally: mpfi_clear(tmpim)
cdef int mpfi_set_via_RR(mpfi_ptr re, x, field) except -1: """ Convert ``x`` to an MPFI interval by converting ``x`` to the appropriate real fields.
INPUT: see ``mpfi_set_sage`` """ cdef RealIntervalField_class RIF else:
|