Commits

mattip  committed db688ce

implement real, imag for all except complex

  • Participants
  • Parent commits afd8491
  • Branches numpypy-real-as-view

Comments (0)

Files changed (3)

File pypy/module/micronumpy/arrayimpl/concrete.py

                               new_shape, self)
         else:
             return None
+    
+    def get_real(self):
+        if self.dtype.is_complex_type():
+            raise NotImplementedError('waiting for astype()')
+        return SliceArray(self.start, self.get_strides(), 
+                          self.get_backstrides(), self.get_shape(), self)
+
+    def get_imag(self):
+        if self.dtype.is_complex_type():
+            raise NotImplementedError('waiting for astype()')
+        if self.dtype.is_flexible_type():
+            # numpy returns self for self.imag
+            return SliceArray(self.start, self.get_strides(), 
+                          self.get_backstrides(), self.get_shape(), self)
+   
+        strides, backstrides = support.calc_strides(self.get_shape(), self.dtype,
+                                                    self.order)
+        impl = NonWritableArray(self.get_shape(), self.dtype, self.order, strides,
+                             backstrides)
+        impl.fill(self.dtype.box(0))
+        return impl
 
     # -------------------- applevel get/setitem -----------------------
 
                                                     self.order)
         return SliceArray(0, strides, backstrides, new_shape, self)
 
+class NonWritableArray(ConcreteArray):
+    def descr_setitem(self, space, w_index, w_value):
+        raise OperationError(space.w_RuntimeError, space.wrap(
+            "array is not writable"))
+        
+
 class SliceArray(BaseConcreteArray):
     def __init__(self, start, strides, backstrides, shape, parent, dtype=None):
         self.strides = strides

File pypy/module/micronumpy/interp_numarray.py

     def descr_copy(self, space):
         return W_NDimArray(self.implementation.copy())
 
+    def descr_get_real(self, space):
+        return W_NDimArray(self.implementation.get_real())
+
+    def descr_get_imag(self, space):
+        ret = self.implementation.get_imag()
+        if ret:
+            return W_NDimArray(ret)
+        raise OperationError(space.w_NotImplementedError, 
+                    space.wrap('imag not implemented for this dtype'))
+
+    def descr_set_real(self, space, w_new_val):
+        # copy (broadcast) values into self
+        self.implementation.descr_set_real(space, w_new_val)
+
+    def descr_set_imag(self, space, w_new_val):
+        # if possible, copy (broadcast) values into self
+        self.implementation.descr_set_imag(space, w_new_val)
+
     def descr_reshape(self, space, args_w):
         """reshape(...)
         a.reshape(shape)
     descr_neg = _unaryop_impl("negative")
     descr_abs = _unaryop_impl("absolute")
     descr_invert = _unaryop_impl("invert")
-    descr_get_real = _unaryop_impl("real")
-    descr_get_imag = _unaryop_impl("imag")
 
     def descr_nonzero(self, space):
         if self.get_size() > 1:
     swapaxes = interp2app(W_NDimArray.descr_swapaxes),
     flat = GetSetProperty(W_NDimArray.descr_get_flatiter),
     item = interp2app(W_NDimArray.descr_item),
-    real = GetSetProperty(W_NDimArray.descr_get_real),
-    imag = GetSetProperty(W_NDimArray.descr_get_imag),
-
+    real = GetSetProperty(W_NDimArray.descr_get_real, 
+                          W_NDimArray.descr_set_real),
+    imag = GetSetProperty(W_NDimArray.descr_get_imag,
+                          W_NDimArray.descr_set_imag),
     __array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
 )
 

File pypy/module/micronumpy/test/test_numarray.py

         raises(RuntimeError, 'b[7] = -2')
         a = array(['abc','def'],dtype='S3')
         b = a.real
-        assert (b==a).all()
+        assert a[0] == b[0]
+        assert a[1] == b[1]
         b[1] = 'xyz'
         assert a[1] == 'xyz'
         a=array([1+1j, 2-3j])