Can one form a view into diagonal of ndarray in numpy
<p>Simple slices form views into the parent array. The strides of the view is generically the multiple of the strides of the parent array. </p>

<p>Given 2d parent array with strides <code>(s0, s1)</code>, the 1D array with strides <code>(s0+s1)</code> gives the view in the diagonal of the parent array. </p>

<p>Is there a way to create such a view in top-level Python/numpy? Thank you in advance.</p>

<p>With <code>as_strided</code> I can do what you want:</p>

<pre><code>In [298]: X=np.eye(5)
In [299]: X.strides
Out[299]: (40, 8)
In [300]: np.lib.stride_tricks.as_strided(X,shape=(5,),strides=(48,))
Out[300]: array([ 1.,  1.,  1.,  1.,  1.])
</code></pre>

<p>though some would argue the <code>as_strided</code> is a step closer to the 'guts' than most of <code>numpy</code> Python code.</p>

<p>I can do the same striding by indexing on the flattened array:</p>

<pre><code>In [311]: X.ravel()[::6]
Out[311]: array([ 1.,  2.,  3.,  4.,  5.]) 
</code></pre>

<p>(here the <code>X</code> values were changed by a <code>view</code> test).</p>

python numpy