Ticket #5081: 5081-numpy-types.patch

File 5081-numpy-types.patch, 4.8 KB (added by robertwb, 11 years ago)
  • sage/rings/complex_double.pyx

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1247126114 25200
    # Node ID 1214b9ee1b83c71513f3900f11bdad46434a57f6
    # Parent  03e1321834b7bc9f99aead29dcfb2bd357054f42
    Conversion of integers, reals to numpy types.
    
    diff -r 03e1321834b7 -r 1214b9ee1b83 sage/rings/complex_double.pyx
    a b  
    588588    calculations were performed with true complex numbers. This is due
    589589    to the rounding errors inherent to finite precision calculations.
    590590    """
     591    # Enable when __complex__ is used by NumPy rather than expecting a float.
     592    # __array_interface__ = {'typestr': '=c16'}
     593   
    591594    def __new__(self, real=None, imag=None):
    592595        self._parent = _CDF
    593596       
  • sage/rings/complex_number.pyx

    diff -r 03e1321834b7 -r 1214b9ee1b83 sage/rings/complex_number.pyx
    a b  
    8989        sage: loads(b.dumps()) == b
    9090        True
    9191    """
     92   
     93    # Enable when __complex__ is used by NumPy rather than expecting a float.   
     94    # __array_interface__ = {'typestr': '=c16'}
    9295
    9396    cdef ComplexNumber _new(self):
    9497        """
  • sage/rings/integer.pyx

    diff -r 03e1321834b7 -r 1214b9ee1b83 sage/rings/integer.pyx
    a b  
    102102    sage: RR = RealField(200)
    103103    sage: RR(n)
    104104    9.3908230000000000000000000000000000000000000000000000000000e6
     105
     106TESTS::
     107
     108Converts to NumPy::
     109
     110    sage: import numpy
     111    sage: numpy.array([1, 2, 3]).dtype
     112    dtype('int64')
     113    sage: numpy.array([1,2,3,0.1]).dtype
     114    dtype('float64')
     115
     116Explicitly use dtype=object for values larger than 64 bits::
     117
     118    sage: numpy.array([2^100])     
     119    Traceback (most recent call last):
     120    ...
     121    ValueError: setting an array element with a sequence.
     122    sage: numpy.array([2^100], dtype=object)
     123    array([1267650600228229401496703205376], dtype=object)
    105124"""
    106125#*****************************************************************************
    107126#       Copyright (C) 2004,2006 William Stein <wstein@gmail.com>
     
    387406        sage: Integer('012')
    388407        10
    389408    """
     409   
     410    __array_interface__ = {'typestr': '=i8'}
    390411
    391412    # todo: It would be really nice if we could avoid the __new__ call.
    392413    # It has python calling conventions, and our timing tests indicate the
  • sage/rings/real_double.pyx

    diff -r 03e1321834b7 -r 1214b9ee1b83 sage/rings/real_double.pyx
    a b  
    2424::
    2525
    2626    sage: V = RDF^10000
     27
     28TESTS::
     29
     30    Test NumPy conversions::
     31   
     32        sage: RDF(1).__array_interface__
     33        {'typestr': '=f8'}
     34        sage: import numpy
     35        sage: numpy.array([RDF.pi()]).dtype
     36        dtype('float64')
    2737"""
    2838 
    2939include '../ext/python_float.pxi'
     
    510520    calculations were performed with true real numbers. This is due to
    511521    the rounding errors inherent to finite precision calculations.
    512522    """
     523   
     524    __array_interface__ = {'typestr': '=f8'}
     525   
    513526    def __new__(self, x=None):
    514527        (<Element>self)._parent = _RDF
    515528       
  • sage/rings/real_mpfr.pyx

    diff -r 03e1321834b7 -r 1214b9ee1b83 sage/rings/real_mpfr.pyx
    a b  
    160160
    161161_re_skip_zeroes = re.compile(r'^(.+?)0*$')
    162162
     163cdef object numpy_double_interface = {'typestr': '=f8'}
     164cdef object numpy_object_interface = {'typestr': '|O'}
     165
    163166#*****************************************************************************
    164167#
    165168#       External Python access to constants
     
    938941            10.500000000000000000000000000000000000000000000000000000000
    939942        """
    940943        return "%s!%s" % (self.parent()._magma_init_(magma), self)
    941 
     944   
     945    property __array_interface__:
     946        def __get__(self):
     947            """
     948            Used for NumPy conversion.
     949           
     950            EXAMPLES::
     951
     952                sage: import numpy
     953                sage: numpy.arange(10.0)
     954                array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
     955                sage: numpy.array([1.0, 1.1, 1.2]).dtype
     956                dtype('float64')
     957                sage: numpy.array([1.000000000000000000000000000000000000]).dtype
     958                dtype('object')
     959            """
     960            if (<RealField>self._parent).__prec <= 77: # max size of repr(float)
     961                return numpy_double_interface
     962            else:
     963                return numpy_object_interface
     964
     965   
    942966    cdef _set(self, x, int base):
    943967        # This should not be called except when the number is being created.
    944968        # Real Numbers are supposed to be immutable.