checking non-ambiguity of strides in numpy array
<p>For a numpy array <code>X</code>, the location of its element <code>X[k[0], ..., k[d-1]]</code> is offset from the location of <code>X[0,..., 0]</code> by <code>k[0]*s[0] + ... + k[d-1]*s[d-1]</code>, where <code>(s[0],...,s[d-1])</code> is the tuple representing <code>X.strides</code>.</p>

<p>As far as I understand nothing in numpy array specs requires that distinct indexes of array <code>X</code> correspond to distinct addresses in memory, the simplest instance of this being a zero value of the stride, e.g. see <a href="http://www.scipy-lectures.org/advanced/advanced_numpy/index.html#broadcasting" rel="nofollow">advanced NumPy</a> section of scipy lectures.</p>

<p>Does the numpy have a built-in predicate to test if the strides and the shape are such that distinct indexes map to distinct memory addresses? </p>

<p>If not, how does one write one, preferably so as to avoid sorting of the strides?</p>

<p>It turns out this test is already implemented in numpy, see <a href="https://github.com/numpy/numpy/blob/master/numpy/core/src/private/mem_overlap.c#L842" rel="nofollow">mem_overlap.c:842</a>.</p>

<p>The test is exposed as <code>numpy.core.multiarray_tests.internal_overlap(x)</code>. </p>

<p>Example:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; from numpy.core.multiarray_tests import internal_overlap
&gt;&gt;&gt; from numpy.lib.stride_tricks import as_strided
</code></pre>

<p>Now, create a contiguous array, and use <code>as_strided</code> to create an array with internal overlapping, and confirm this with the testing:</p>

<pre><code>&gt;&gt;&gt; x = np.arange(3*4, dtype=np.float64).reshape((3,4))
&gt;&gt;&gt; y = as_strided(x, shape=(5,4), strides=(16, 8))
&gt;&gt;&gt; y
array([[  0.,   1.,   2.,   3.],
       [  2.,   3.,   4.,   5.],
       [  4.,   5.,   6.,   7.],
       [  6.,   7.,   8.,   9.],
       [  8.,   9.,  10.,  11.]])
&gt;&gt;&gt; internal_overlap(x)
False
&gt;&gt;&gt; internal_overlap(y)
True
</code></pre>

<p>The function is optimized to quickly returns <code>False</code> for Fortran- or C- contiguous arrays. </p>

python numpy