Can you write numpy slicing generically?
<p>I want to do something like</p>

<pre><code>x[i, :, :] = (rhs[i, :, :]-diag[i] * x[i+1, :, :])/diag[i]
</code></pre>

<p>where x and rhs are 3D numpy arrays of size (T,L,S). diag is a 1D array of size T.</p>

<p>This will broadcast properly. </p>

<p>But now I'd like to write a similar function to work on 2D arrays or some other number of dimensions. How can I write this generically so that it will work on any array that has first dimension of size T. I don't want to duplicate code with just a different number of colons since there are a lot of these kinds of lines in the function. </p>

<pre><code>x[i] = (rhs[i] - diag[i] * x[i+1])/diag[i]
</code></pre>

<p>Those colons are completely unnecessary.</p>

python numpy