[pypy-commit] pypy numpypy-is_contiguous: test, implement is_contiguous for C order
mattip
noreply at buildbot.pypy.org
Tue Dec 6 21:16:29 CET 2011
Author: mattip
Branch: numpypy-is_contiguous
Changeset: r50225:b8c87c4cf664
Date: 2011-12-06 22:14 +0200
http://bitbucket.org/pypy/pypy/changeset/b8c87c4cf664/
Log: test, implement is_contiguous for C order
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
@@ -155,6 +155,27 @@
last_step *= shape[i]
return steps
+def is_contiguous(arr):
+ #Only views can be non-contiguous
+ if isinstance(arr, ViewArray):
+ steps = calc_steps(arr.shape, arr.strides)
+ for i in range(1, len(steps)):
+ if steps[i] != steps[0]:
+ return False
+ return True
+
+def is_contiguous_lr(arr):
+ if arr.strides[-1] < arr.strides[0]:
+ #rl, not lr
+ return False
+ return is_contiguous(arr)
+
+def is_contiguous_rl(self):
+ if arr.strides[-1] > arr.strides[0]:
+ #lr, not rl
+ return False
+ return is_contiguous(arr)
+
#Recalculating strides. Find the steps that the iteration does for each
#dimension, given the stride and shape. Then try to create a new stride that
#fits the new shape, using those steps. If there is a shape/step mismatch
@@ -192,7 +213,8 @@
n_new_elems_used = 1
oldI = -1
n_old_elems_to_use = old_shape[-1]
- for s in new_shape[::-1]:
+ for i in range(len(new_shape) - 1, -1, -1):
+ s = new_shape[i]
new_strides.insert(0, cur_step * n_new_elems_used)
n_new_elems_used *= s
while n_new_elems_used > n_old_elems_to_use:
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
@@ -166,6 +166,23 @@
assert calc_new_strides([24], [2, 4, 3], [48, 6, 1]) is None
assert calc_new_strides([24], [2, 4, 3], [24, 6, 2]) == [2]
+ def test_contiguousC(self):
+ from pypy.module.micronumpy.interp_numarray import is_contiguous,
+ is_contiguous_rl, is_contiguous_lr
+ a = W_NDimArray(10 * 5 * 3, [10, 5, 3], MockDtype(), 'C')
+ assert is_contiguous(a) == True
+ assert is_contiguous_lr(a) == False
+ assert is_contiguous_rl(a) == True
+ b = a.descr_get_transpose(self.space)
+ assert is_contiguous(b) == True
+ assert is_contiguous_lr(b) == True
+ assert is_contiguous_rl(b) == False
+ b = a.create_slice(self.space, [(0, 10, 2, 5)])
+ assert is_contiguous(b) == False
+ assert is_contiguous_lr(b) == False
+ assert is_contiguous_rl(b) == False
+
+
class AppTestNumArray(BaseNumpyAppTest):
def test_ndarray(self):
from numpypy import ndarray, array, dtype
More information about the pypy-commit
mailing list