Author: Jasper Schulz <jasper.sch...@student.hpi.uni-potsdam.de> Branch: numpypy-complex2 Changeset: r55776:9b031aa06b64 Date: 2012-06-23 12:53 +0200 http://bitbucket.org/pypy/pypy/changeset/9b031aa06b64/
Log: Started work on complex-types for numpypy diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py --- a/pypy/module/micronumpy/__init__.py +++ b/pypy/module/micronumpy/__init__.py @@ -78,6 +78,8 @@ 'str_': 'interp_boxes.W_StringBox', 'unicode_': 'interp_boxes.W_UnicodeBox', 'void': 'interp_boxes.W_VoidBox', + 'complexfloating': 'interp_boxes.W_ComplexFloatingBox', + 'complex128': 'interp_boxes.W_Complex128Box' } # ufuncs diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py --- a/pypy/module/micronumpy/compile.py +++ b/pypy/module/micronumpy/compile.py @@ -56,7 +56,8 @@ w_slice = "slice" w_str = "str" w_unicode = "unicode" - + w_complex = "complex" + def __init__(self): """NOT_RPYTHON""" self.fromcache = InternalSpaceCache(self).getorbuild diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py --- a/pypy/module/micronumpy/interp_boxes.py +++ b/pypy/module/micronumpy/interp_boxes.py @@ -1,11 +1,12 @@ from pypy.interpreter.baseobjspace import Wrappable from pypy.interpreter.error import operationerrfmt, OperationError from pypy.interpreter.gateway import interp2app, unwrap_spec -from pypy.interpreter.typedef import TypeDef +from pypy.interpreter.typedef import TypeDef, GetSetProperty from pypy.objspace.std.floattype import float_typedef from pypy.objspace.std.stringtype import str_typedef from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object from pypy.objspace.std.inttype import int_typedef +from pypy.objspace.std.complextype import complex_typedef from pypy.rlib.rarithmetic import LONG_BIT from pypy.tool.sourcetools import func_with_new_name @@ -221,6 +222,7 @@ def descr_index(space, self): return space.index(self.item(space)) + class W_VoidBox(W_FlexibleBox): @unwrap_spec(item=str) def descr_getitem(self, space, item): @@ -268,6 +270,24 @@ # arr.storage[i] = arg[i] return W_UnicodeBox(arr, 0, arr.dtype) + +class W_ComplexFloatingBox(W_InexactBox): + _attrs_ = () + +class W_Complex128Box(W_ComplexFloatingBox): + descr__new__, _get_dtype = new_dtype_getter("complex128") + + def __init__(self, real, imag): + self.real = real + self.imag = imag + + def descr_get_real(self, space): + return space.wrap(self.real) + + def descr_get_imag(self, space): + return space.wrap(self.imag) + + W_GenericBox.typedef = TypeDef("generic", __module__ = "numpypy", @@ -450,3 +470,13 @@ __new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func), ) +W_ComplexFloatingBox.typedef = TypeDef("complexfloating", W_InexactBox.typedef, + __module__ = "numpypy", +) + +W_Complex128Box.typedef = TypeDef("complex128", (W_ComplexFloatingBox.typedef, complex_typedef), + __module__ = "numpypy", + __new__ = interp2app(W_Complex128Box.descr__new__.im_func), + real = GetSetProperty(W_Complex128Box.descr_get_real), + imag = GetSetProperty(W_Complex128Box.descr_get_imag), +) \ No newline at end of file diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py --- a/pypy/module/micronumpy/interp_dtype.py +++ b/pypy/module/micronumpy/interp_dtype.py @@ -387,6 +387,16 @@ alternate_constructors=[space.w_float], aliases=["float"], ) + self.w_complex128dtype = W_Dtype( + types.Complex128(), + num=15, + kind=FLOATINGLTR, + name="complex128", + char="c", + w_box_type = space.gettypefor(interp_boxes.W_Complex128Box), + alternate_constructors=[space.w_complex], + aliases=["complex"], + ) self.w_stringdtype = W_Dtype( types.StringType(1), num=18, @@ -420,8 +430,8 @@ self.w_int16dtype, self.w_uint16dtype, self.w_int32dtype, self.w_uint32dtype, self.w_longdtype, self.w_ulongdtype, self.w_int64dtype, self.w_uint64dtype, - self.w_float32dtype, - self.w_float64dtype, self.w_stringdtype, self.w_unicodedtype, + self.w_float32dtype, self.w_float64dtype, self.w_complex128dtype, + self.w_stringdtype, self.w_unicodedtype, self.w_voiddtype, ] self.float_dtypes_by_num_bytes = sorted( diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py --- a/pypy/module/micronumpy/test/test_dtypes.py +++ b/pypy/module/micronumpy/test/test_dtypes.py @@ -421,6 +421,29 @@ assert numpy.float64('23.4') == numpy.float64(23.4) raises(ValueError, numpy.float64, '23.2df') + def test_complex_floating(self): + import _numpypy as numpy + + assert numpy.complexfloating.__mro__ == (numpy.complexfloating, + numpy.inexact, numpy.number, numpy.generic, object) + + def test_complex(self): + import _numpypy as numpy + + assert numpy.complex128.__mro__ == (numpy.complex128, + numpy.complexfloating, numpy.inexact, numpy.number, numpy.generic, + complex, object) + + c = numpy.complex128(complex(1, 2)) + assert c.real == 1 + assert c.imag == 2 + assert repr(c) == '(1+2j)' + + def test_complex_dtype(self): + import _numpypy as numpy + + assert numpy.dtype(complex) is numpy.dtype("complex") + def test_subclass_type(self): import _numpypy as numpy diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py --- a/pypy/module/micronumpy/types.py +++ b/pypy/module/micronumpy/types.py @@ -5,6 +5,7 @@ from pypy.interpreter.error import OperationError from pypy.module.micronumpy import interp_boxes from pypy.objspace.std.floatobject import float2string +from pypy.objspace.std.complexobject import W_ComplexObject, str_format from pypy.rlib import rfloat, libffi, clibffi from pypy.rlib.objectmodel import specialize, we_are_translated from pypy.rlib.rarithmetic import widen, byteswap @@ -918,6 +919,30 @@ BoxType = interp_boxes.W_Float64Box format_code = "d" +class Complex128(BaseType): + _attrs_ = () + + T = rffi.CHAR + BoxType = interp_boxes.W_Complex128Box + + def get_element_size(self): + return 2 * rffi.sizeof(rffi.DOUBLE) + + def coerce_subtype(self, space, w_subtype, w_item): + real, imag = space.unpackcomplex(w_item) + w_obj = space.allocate_instance(self.BoxType, w_subtype) + assert isinstance(w_obj, self.BoxType) + w_obj.__init__(real, imag) + return w_obj + + def str_format(self, box): + real_str = str_format(box.real) + imag_str = str_format(box.imag) + return ''.join(['(', real_str, '+', imag_str, 'j', ')']) + + +NonNativeComplex128 = Complex128 + class BaseStringType(object): _mixin_ = True _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit