Author: Antonio Cuni <[email protected]>
Branch:
Changeset: r61083:d10570215445
Date: 2013-02-11 11:50 +0100
http://bitbucket.org/pypy/pypy/changeset/d10570215445/
Log: implement numpypy.vstack (by copying it from numpy source)
diff --git a/lib_pypy/numpypy/core/shape_base.py
b/lib_pypy/numpypy/core/shape_base.py
--- a/lib_pypy/numpypy/core/shape_base.py
+++ b/lib_pypy/numpypy/core/shape_base.py
@@ -1,3 +1,4 @@
+import _numpypy
from numeric import array, asanyarray, newaxis
def atleast_1d(*arys):
@@ -170,3 +171,54 @@
return res[0]
else:
return res
+
+def vstack(tup):
+ """
+ Stack arrays in sequence vertically (row wise).
+
+ Take a sequence of arrays and stack them vertically to make a single
+ array. Rebuild arrays divided by `vsplit`.
+
+ Parameters
+ ----------
+ tup : sequence of ndarrays
+ Tuple containing arrays to be stacked. The arrays must have the same
+ shape along all but the first axis.
+
+ Returns
+ -------
+ stacked : ndarray
+ The array formed by stacking the given arrays.
+
+ See Also
+ --------
+ hstack : Stack arrays in sequence horizontally (column wise).
+ dstack : Stack arrays in sequence depth wise (along third dimension).
+ concatenate : Join a sequence of arrays together.
+ vsplit : Split array into a list of multiple sub-arrays vertically.
+
+ Notes
+ -----
+ Equivalent to ``np.concatenate(tup, axis=0)`` if `tup` contains arrays that
+ are at least 2-dimensional.
+
+ Examples
+ --------
+ >>> a = np.array([1, 2, 3])
+ >>> b = np.array([2, 3, 4])
+ >>> np.vstack((a,b))
+ array([[1, 2, 3],
+ [2, 3, 4]])
+
+ >>> a = np.array([[1], [2], [3]])
+ >>> b = np.array([[2], [3], [4]])
+ >>> np.vstack((a,b))
+ array([[1],
+ [2],
+ [3],
+ [2],
+ [3],
+ [4]])
+
+ """
+ return _numpypy.concatenate(map(atleast_2d,tup),0)
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_shape_base.py
b/pypy/module/test_lib_pypy/numpypy/core/test_shape_base.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_shape_base.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_shape_base.py
@@ -60,3 +60,22 @@
a = np.atleast_3d([[[1, 2]]])
assert np.array_equal(a, [[[1, 2]]])
assert a.shape == (1, 1, 2)
+
+ def test_vstack(self):
+ import numpypy as np
+
+ a = np.array([1, 2, 3])
+ b = np.array([2, 3, 4])
+ c = np.vstack((a,b))
+ assert np.array_equal(c, [[1, 2, 3],
+ [2, 3, 4]])
+
+ a = np.array([[1], [2], [3]])
+ b = np.array([[2], [3], [4]])
+ c = np.vstack((a,b))
+ assert np.array_equal(c, [[1],
+ [2],
+ [3],
+ [2],
+ [3],
+ [4]])
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit