[pypy-commit] pypy scalar_get_set: test, add set_real, set_imag for scalars. Also add composite() to types.ComplexType to create complex from real, imag boxes
mattip
noreply at buildbot.pypy.org
Wed Mar 20 00:43:35 CET 2013
Author: Matti Picus <matti.picus at gmail.com>
Branch: scalar_get_set
Changeset: r62514:bc351ea348e1
Date: 2013-03-19 16:41 -0700
http://bitbucket.org/pypy/pypy/changeset/bc351ea348e1/
Log: test, add set_real, set_imag for scalars. Also add composite() to
types.ComplexType to create complex from real, imag boxes
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -82,6 +82,10 @@
return SliceArray(self.start, strides, backstrides,
self.get_shape(), self, orig_array)
+ def set_real(self, space, orig_array, w_value):
+ tmp = self.get_real(orig_array)
+ tmp.setslice(space, convert_to_array(space, w_value))
+
def get_imag(self, orig_array):
strides = self.get_strides()
backstrides = self.get_backstrides()
@@ -98,6 +102,10 @@
impl.fill(self.dtype.box(0))
return impl
+ def set_imag(self, space, orig_array, w_value):
+ tmp = self.get_imag(orig_array)
+ tmp.setslice(space, convert_to_array(space, w_value))
+
# -------------------- applevel get/setitem -----------------------
@jit.unroll_safe
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -1,6 +1,6 @@
from pypy.module.micronumpy.arrayimpl import base
-from pypy.module.micronumpy.base import W_NDimArray
+from pypy.module.micronumpy.base import W_NDimArray, convert_to_array
from pypy.module.micronumpy import support
from pypy.interpreter.error import OperationError
@@ -68,6 +68,24 @@
return scalar
return self
+ def set_real(self, space, orig_array, w_val):
+ w_arr = convert_to_array(space, w_val)
+ dtype = self.dtype.float_type or self.dtype
+ if len(w_arr.get_shape()) > 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "could not broadcast input array from shape " +
+ "(%s) into shape ()" % (
+ ','.join([str(x) for x in w_arr.get_shape()],))))
+ if self.dtype.is_complex_type():
+ #imag = dtype.itemtype.unbox(self.value.convert_imag_to(dtype))
+ #val = dtype.itemtype.unbox(w_arr.get_scalar_value().
+ # convert_to(dtype))
+ #self.value = self.dtype.box_complex(val, imag)
+ self.value = self.dtype.itemtype.composite(w_arr.get_scalar_value().convert_to(dtype),
+ self.value.convert_imag_to(dtype))
+ else:
+ self.value = w_arr.get_scalar_value()
+
def get_imag(self, orig_array):
if self.dtype.is_complex_type():
scalar = Scalar(self.dtype.float_type)
@@ -80,6 +98,25 @@
scalar.value = scalar.dtype.itemtype.box(0)
return scalar
+ def set_imag(self, space, orig_array, w_val):
+ #Only called on complex dtype
+ assert self.dtype.is_complex_type()
+ w_arr = convert_to_array(space, w_val)
+ dtype = self.dtype.float_type
+ if len(w_arr.get_shape()) > 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "could not broadcast input array from shape " +
+ "(%s) into shape ()" % (
+ ','.join([str(x) for x in w_arr.get_shape()],))))
+ #real = dtype.itemtype.unbox(self.value.convert_real_to(dtype))
+ #val = dtype.itemtype.unbox(w_arr.get_scalar_value().
+ # convert_to(dtype))
+ #self.value = self.dtype.box_complex(real, val)
+ self.value = self.dtype.itemtype.composite(
+ self.value.convert_real_to(dtype),
+ w_arr.get_scalar_value(),
+ )
+
def descr_getitem(self, space, _, w_idx):
raise OperationError(space.w_IndexError,
space.wrap("scalars cannot be indexed"))
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
@@ -71,7 +71,6 @@
def box_complex(self, real, imag):
return self.itemtype.box_complex(real, imag)
-
def coerce(self, space, w_item):
return self.itemtype.coerce(space, self, w_item)
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -272,16 +272,14 @@
def descr_set_real(self, space, w_value):
# copy (broadcast) values into self
- tmp = self.implementation.get_real(self)
- tmp.setslice(space, convert_to_array(space, w_value))
+ self.implementation.set_real(space, self, w_value)
def descr_set_imag(self, space, w_value):
# if possible, copy (broadcast) values into self
if not self.get_dtype().is_complex_type():
raise OperationError(space.w_TypeError,
space.wrap('array does not have imaginary part to set'))
- tmp = self.implementation.get_imag(self)
- tmp.setslice(space, convert_to_array(space, w_value))
+ self.implementation.set_imag(space, self, w_value)
def descr_reshape(self, space, args_w):
"""reshape(...)
diff --git a/pypy/module/micronumpy/test/test_complex.py b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -520,16 +520,22 @@
assert imag(0.0) == 0.0
a = array([complex(3.0, 4.0)])
b = a.real
+ b[0] = 1024
+ assert a[0].real == 1024
assert b.dtype == dtype(float)
a = array(complex(3.0, 4.0))
b = a.real
assert b == array(3)
+ a.real = 1024
+ assert a.real == 1024
assert a.imag == array(4)
assert b.dtype == dtype(float)
a = array(4.0)
b = a.imag
assert b == 0
assert b.dtype == dtype(float)
+ raises(TypeError, 'a.imag = 1024')
+ raises(ValueError, 'a.real = [1, 3]')
a = array('abc')
assert str(a.real) == 'abc'
# numpy imag for flexible types returns self
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
@@ -1150,6 +1150,14 @@
return rfloat.NAN, rfloat.NAN
return rfloat.INFINITY, rfloat.INFINITY
+ @specialize.argtype(1)
+ def composite(self, v1, v2):
+ assert isinstance(v1, self.ComponentBoxType)
+ assert isinstance(v2, self.ComponentBoxType)
+ real = v1.value
+ imag = v2.value
+ return self.box_complex(real, imag)
+
@complex_unary_op
def pos(self, v):
return v
More information about the pypy-commit
mailing list