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>If you are using numpy 1.9 or later, and a read-only view is sufficient, you can use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.diagonal.html" rel="nofollow"><code>numpy.diagonal</code></a>.  The docstring says that in some future version of numpy, <code>numpy.diagonal</code> will return a read/write view, but that doesn't help you now.  If you need a read/write view, @hpaulj's suggestion to use <code>as_strided</code> will work.  I suggest something like </p>

<pre><code>diag = as_strided(a, shape=(min(a.shape),), strides=(sum(a.strides),))
</code></pre>

<p>Be sure to read the "Notes" section of the <code>as_strided</code> docstring.</p>

python numpy