[pypy-commit] pypy matrixmath-reshape-merge: add app-level reshape with docstrings

mattip noreply at buildbot.pypy.org
Sat Dec 3 20:50:49 CET 2011


Author: mattip
Branch: matrixmath-reshape-merge
Changeset: r50095:25cacab6ecdb
Date: 2011-12-03 21:49 +0200
http://bitbucket.org/pypy/pypy/changeset/25cacab6ecdb/

Log:	add app-level reshape with docstrings

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
@@ -61,4 +61,5 @@
         'inf': 'app_numpy.inf',
         'e': 'app_numpy.e',
         'arange': 'app_numpy.arange',
+        'reshape': 'app_numpy.reshape',
     }
diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -36,3 +36,39 @@
         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)
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
@@ -881,8 +881,17 @@
                          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)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1218,3 +1218,14 @@
         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)


More information about the pypy-commit mailing list