Commits

mattip  committed 9bef652

add app-level reshape with docstrings

  • Participants
  • Parent commits bafc6b1
  • Branches matrixmath-reshape-merge

Comments (0)

Files changed (4)

File pypy/module/micronumpy/__init__.py

         'inf': 'app_numpy.inf',
         'e': 'app_numpy.e',
         'arange': 'app_numpy.arange',
+        'reshape': 'app_numpy.reshape',
     }

File pypy/module/micronumpy/app_numpy.py

         j += 1
         i += step
     return arr
+
+def reshape(a, shape):
+    '''reshape(a, newshape)
+    Gives a new shape to an array without changing its data.
+    
+    Parameters
+    ----------
+    a : array_like
+        Array to be reshaped.
+    newshape : int or tuple of ints
+        The new shape should be compatible with the original shape. If
+        an integer, then the result will be a 1-D array of that length.
+        One shape dimension can be -1. In this case, the value is inferred
+        from the length of the array and remaining dimensions.
+    
+    Returns
+    -------
+    reshaped_array : ndarray
+        This will be a new view object if possible; otherwise, it will
+        be a copy.
+    
+    
+    See Also
+    --------
+    ndarray.reshape : Equivalent method.
+    
+    Notes
+    -----
+    
+    It is not always possible to change the shape of an array without
+    copying the data. If you want an error to be raise if the data is copied,
+    you should assign the new shape to the shape attribute of the array
+'''
+    if not hasattr(a, 'reshape'):
+        a = numpypy.array(a)
+    return a.reshape(shape)

File pypy/module/micronumpy/interp_numarray.py

                          shape[:])
 
     def descr_reshape(self, space, w_args):
-        """Return a reshaped view into the original array's data
-        """
+        """reshape(...)
+    a.reshape(shape)
+    
+    Returns an array containing the same data with a new shape.
+    
+    Refer to `%s.reshape` for full documentation.
+    
+    See Also
+    --------
+    numpy.reshape : equivalent function
+""" % 'numpypy'
         concrete = self.get_concrete()
         new_shape = get_shape_from_iterable(space, 
                                             concrete.find_size(), w_args)

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

         a = arange(0, 0.8, 0.1)
         assert len(a) == 8
         assert arange(False, True, True).dtype is dtype(int)
+
+
+class AppTestRanges(BaseNumpyAppTest):
+    def test_app_reshape(self):
+        from numpypy import arange, array, dtype, reshape
+        a = arange(12)
+        b = reshape(a, (3, 4))
+        assert b.shape == (3, 4)
+        a = range(12)
+        b = reshape(a, (3, 4))
+        assert b.shape == (3, 4)