Skip to content

Instantly share code, notes, and snippets.

@timo
Created January 17, 2012 18:03
Show Gist options
  • Save timo/1627867 to your computer and use it in GitHub Desktop.
Save timo/1627867 to your computer and use it in GitHub Desktop.
implement ravel method for numpypy
diff -r 07bd76d923fd pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py Wed Jan 18 09:32:00 2012 +1000
+++ b/pypy/module/micronumpy/interp_numarray.py Sat Jan 21 21:33:07 2012 +0100
@@ -558,6 +558,12 @@
arr.setshape(space, new_shape)
return arr
+ def descr_ravel(self, space, w_order="C"):
+ if space.str_w(w_order) != "C":
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "Ravel with other orders than C not implemented."))
+ return self.descr_reshape(space, [space.wrap(-1)])
+
def descr_tolist(self, space):
if len(self.shape) == 0:
assert isinstance(self, Scalar)
@@ -1275,6 +1281,7 @@
copy = interp2app(BaseArray.descr_copy),
reshape = interp2app(BaseArray.descr_reshape),
+ ravel = interp2app(BaseArray.descr_ravel),
tolist = interp2app(BaseArray.descr_tolist),
)
diff -r 07bd76d923fd pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py Wed Jan 18 09:32:00 2012 +1000
+++ b/pypy/module/micronumpy/test/test_numarray.py Sat Jan 21 21:33:07 2012 +0100
@@ -439,6 +439,24 @@
y = z.reshape(4, 3, 8)
assert y.shape == (4, 3, 8)
+ def test_ravel(self):
+ from _numpypy import array
+ arr = array([range(i, i + 5) for i in range(3)])
+ b = arr.ravel()
+ assert b.shape == (arr.size,)
+ assert list(b) == [0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6]
+
+ arr2 = array(range(0, 10))
+ arr2.ravel() == arr2
+
+ arr3 = array([[range(3) for i in range (3)] for k in range(2)])
+ c = arr3.ravel()
+ assert c.shape == (arr3.size,)
+ assert list(c) == [0, 1, 2, 0, 1, 2, 0, 1, 2,
+ 0, 1, 2, 0, 1, 2, 0, 1, 2]
+
+ raises(NotImplementedError, arr.ravel, "F")
+
def test_add(self):
from _numpypy import array
a = array(range(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment